一、Eureka Server
- 创建一个Spring项目
- 在创建项目的过程中选择Spring Cloud Discovery–>Eureka Server
- 配置Spring 的Pom.xml配置文件
1.Pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
spring-cloud-starter-netflix-eureka-server
spring-cloud-starter-eureka-server 两者的区别:
springCloud更新换代比较快:建议1.5的用spring-cloud-starter-eureka-server , 2.0以上的用spring-cloud-starter-netflix-eureka-server
2.代码中添加注解:@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaApplication.class, args);
}
}
3.默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,在application.properties添加以下配置:
spring.application.name=Eureka Server
server.port=7001
eureka.client.register-with-eureka=false #表示是否将自己注册到Eureka Server,默认为true
eureka.client.fetch-registry=false #表示是否从Eureka Server获取注册信息,默认为true
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/ #设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔
application.yml形式:
eureka:
client:
fetch-registry:
register-with-eureka: false
serviceUrl:
defaultZone: http://localhost:${server.port}/eureka/
server:
port: 7001
spring:
application:
name: Eureka Server
4.访问地址:http://localhost:7001/
二、Eureka Client
1.创建一个SpringBoot工程,引入依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
2.实现/add请求处理接口,通过DiscoveryClient对象,在日志中打印出服务实例的相关内容
@RestController
public class ComputeController {
private final Logger logger = Logger.getLogger(getClass());
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/add" ,method = RequestMethod.GET)
public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
ServiceInstance instance = client.getLocalServiceInstance();
Integer r = a + b;
logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
return r;
}
}
3.启动代码中添加@EnableDiscoveryClient注解,该注解能激活Eureka中的DiscoveryClient实现,才能实现Controller中对服务信息的输出。
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaApplication.class, args);
}
}
4.在application.properties添加以下配置:
spring.application.name=Eureka Client #可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。
server.port=8001 #端口
eureka.client.serviceUrl.defaultZone=http://localhost:7001/eureka/ #属性对应服务注册中心的配置内容,指定服务注册中心的位置
三、可以设置Client手动下线:
如果你的eureka客户端是是一个spring boot应用,可以通过调用以下代码通知注册中心下线。 DiscoveryManager.getInstance().shutdownComponent();
@RestController
public class HelloController {
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String index() {
java.util.List<ServiceInstance> instances = client.getInstances("hello-service");
return "Hello World";
}
@RequestMapping(value = "/offline", method = RequestMethod.GET)
public void offLine(){
DiscoveryManager.getInstance().shutdownComponent();
}
}
鸣谢:https://www.jianshu.com/p/f2de1d1d1002