1. 场景描述
前几天介绍了下springcloud的Eureka注册中心(springcloud-注册中心快速构建),今天结合springboot-web介绍下eureka客户端服务注册。
2. 解决问题
2.1 新建eureka客户端项目
2.1.1 new->project
2.1.2 项目名称改一下
2.1.3 依赖包选择
(1)选择web的starter,等下跑测试使用。
(2)注册中心客户端依赖包选择
next-》finish
2.2 项目介绍
三个类,就可以跑起来了。
2.2.1 pomx文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.spc</groupId>
<artifactId>eurekaclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eurekaclient</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
说明:就两个starter,一个web的starter;一个eureka-client的。
2.2.2 启动类
package com.spc.eurekaclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@EnableEurekaClient
public class EurekaclientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaclientApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/")
public String home() {
return "i'm 软件老王,欢迎光临,服务端口号为:" + port;
}
}
说明:
(1) 启动类上有三个标签,@SpringBootApplication、@RestController、@EnableEurekaClient。
(2)使用springmvc的标签 @RequestMapping("/"),从request中拿到一个端口号打印下。
2.2.3 application.yml
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
healthcheck:
enabled: true
instance:
lease-expiration-duration-in-seconds: 30
lease-renewal-interval-in-seconds: 10
registry-fetch-interval-seconds: 5
server:
port: 9001
spring:
application:
name: client
重点说明:
defaultZone: http://localhost:8761/eureka/ ,这个地址是eureka注册中心的地址(springcloud-注册中k速构建),其他的几个可有可无,其他几个是为了能快速看到注册信息设置的心跳时间,可忽略。
2.3 效果图
本地通过不同端口号启动eureka注册中心和客户端服务注册(springcloud启动多个实例)