Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。
SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。
Eureka包含两个组件:Eureka Server和Eureka Client。
在应用启动后,将会向Eureka Server发送心跳,默认周期为30秒,如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移除。
现在存在两个微服务,一个服务提供者,一个服务消费者,现在我们就来把两个微服务注册到Eureka中。
Eureka本身也是以一个服务的形式存在的,所以接下来直接创建一个Module,并引入Eureka服务端的依赖包。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
Eureka的主启动类添加注解@EnableEurekaServer
在application.yml中添加如下配置:
server:
port: 7001
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false # 表示不向注册中心注册自己
fetch-registry: false
service-url:
defaultZone: http://localhost:7001/eureka/
启动Eureka服务并访问:http://localhost:7001/
进入Eureka主页
在两个微服务的pom.xml中引入Eureka的客户端依赖包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
然后在两个微服务的主启动类上添加注解 @EnableEurekaClient
在提供者和消费者微服务的配置文件中添加配置:
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
启动提供者和消费者微服务,并重新访问http://localhost:7001/
注册成功!!!
对于注册进Eureka里面的服务,可以通过Discovery来获得这些服务的信息。
获取DiscoveryClient
@Autowired
private DiscoveryClient discoveryClient;
获取服务列表
List<String> services = discoveryClient.getServices();
for(String service:services){
System.out.println(service);
}
根据服务获取实例列表
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
System.out.println(instance.getInstanceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
}
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
spring:
cloud:
zookeeper:
connect-string: 192.168.83.129:2181
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class, args);
}
}
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
spring:
application:
name: consul-provider-payment
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8006 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8006.class, args);
}
}
实现语言 | CAP | 服务健康检查 | 接口 | |
---|---|---|---|---|
Eureka | Java | AP | 可配支持 | HTTP |
Consul | Go | CP | 支持 | HTTP/DNS |
Zookeeper | Java | CP | 支持 | 客户端 |
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://wang11.blog.csdn.net/article/details/125777028
内容来源于网络,如有侵权,请联系作者删除!