文章目录
简介
Eureka是Netflix定制小程序开发开发的服务发现,定制小程序开发本身是一个基于REST的服务,定制小程序开发主要用于定位运行在AWS定制小程序开发域中的中间层服务,定制小程序开发以达到负载均衡和中间层服务故障转移的目的。
SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。
Eureka包含两个组件:Eureka Server和Eureka Client。
- Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。
- Eureka Client是一个java客户端,用于简化与Eureka Server的交互,客户端同时也就是一个内置的、使用轮询(round-robin)负载算法的负载均衡器。
在应用启动后,将会向Eureka Server发送心跳,默认周期为30秒,如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务中把这个服务节点移除。
服务注册
现在存在两个,一个服务提供者,一个服务消费者,现在我们就来把两个微服务注册到Eureka中。
创建Eureka注册中心
Eureka本身也是以一个服务的形式存在的,所以接下来直接创建一个Module,并引入Eureka服务端的依赖包。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency>
- 1
- 2
- 3
- 4
Eureka的主启动类添加注解@EnableEurekaServer
配置Eureka端口号及地址
在application.yml中添加如下配置:
server: port: 7001eureka: instance: hostname: localhost client: register-with-eureka: false # 表示不向注册中心注册自己 fetch-registry: false service-url: defaultZone: http://localhost:7001/eureka/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
启动Eureka服务并访问
启动Eureka服务并访问:http://localhost:7001/
进入Eureka主页
引入Eureka Client包并开启注解
在两个微服务的pom.xml中引入Eureka的客户端依赖包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>
- 1
- 2
- 3
- 4
然后在两个微服务的主启动类上添加注解 @EnableEurekaClient
添加注册配置
在提供者和消费者微服务的配置文件中添加配置:
eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:7001/eureka
- 1
- 2
- 3
- 4
- 5
- 6
启动微服务并访问Eureka主页查看
启动提供者和消费者微服务,并重新访问http://localhost:7001/
注册成功!!!
服务发现
对于注册进Eureka里面的服务,可以通过Discovery来获得这些服务的信息。
获取DiscoveryClient
@Autowiredprivate DiscoveryClient discoveryClient;
- 1
- 2
获取服务列表
List<String> services = discoveryClient.getServices();for(String service:services){ System.out.println(service);}
- 1
- 2
- 3
- 4
根据服务获取实例列表
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());}
- 1
- 2
- 3
- 4
Zookeeper服务注册
添加依赖包
<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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
添加配置
spring: cloud: zookeeper: connect-string: 192.168.83.129:2181
- 1
- 2
- 3
- 4
主启动类注解
@SpringBootApplication@EnableDiscoveryClientpublic class PaymentMain8004 { public static void main(String[] args) { SpringApplication.run(PaymentMain8004.class, args); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Consul服务注册
添加依赖包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId></dependency>
- 1
- 2
- 3
- 4
添加配置
spring: application: name: consul-provider-payment cloud: consul: host: localhost port: 8500 discovery: service-name: ${spring.application.name}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
主启动类注解
@SpringBootApplication@EnableDiscoveryClientpublic class PaymentMain8006 { public static void main(String[] args) { SpringApplication.run(PaymentMain8006.class, args); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
3个注册中心的比较
实现语言 | CAP | 服务健康检查 | 接口 | |
---|---|---|---|---|
Eureka | Java | AP | 可配支持 | HTTP |
Consul | Go | CP | 支持 | HTTP/DNS |
Zookeeper | Java | CP | 支持 | 客户端 |