SpringCloud(2) 服务注册和发现Eureka Server

一、简介

EureKa在Spring Cloud全家桶中担任着服务的注册与发现的落地实现。Netflix在设计EureKa时遵循着AP原则,它基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移,功能类似于Dubbo的注册中心Zookeeper。

官方文档:http://cloud.spring.io/spring-cloud-netflix/single/spring-cloud-netflix.html#spring-cloud-eureka-server

二、实现原理

EureKa采用C-S的设计架构,即包括了Eureka Server(服务端),EureKa client(客户端)。
1.EureKa Server 提供服务注册,各个节点启动后,在EureKa server中进行注册;

2 EureKa Client 是一个Java客户端,用于和服务端进行交互,同时客户端也是一个内置的默认使用轮询负载均衡算法的负载均衡器。在应用启动后,会向Eueka Server发送心跳(默认30秒)。如果EureKa Server在多个心跳周期内没有接受到某个节点的心跳,EureKa Server将会从服务注册表中将这个服务移出(默认90秒)。

三、SpringCloud Eureka的使用步骤

1、Eureka Server端(服务端)

1.1 pom.xml

1 <dependency>
2         <groupId>org.springframework.cloud</groupId>
3         <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
4 </dependency>

1.2 application.yml

 1 server:
 2   port: 8761
 3 
 4 eureka:
 5   instance:
 6     #单机hostname: localhost
 7     hostname: localhost
 8   client:
 9     registerWithEureka: false #false表示不向注册中心注册自己
10     fetchRegistry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
11     serviceUrl:
12       #Eureka高复用时设置其他的Eureka之间通信
13       #defaultZone: http://eureka7003.com:7003/eureka/,http://eureka7004.com:7004/eureka/
14       #单机设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
15       #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
16       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

1.3 程序类主类

1 @SpringBootApplication
2 @EnableEurekaServer
3 public class EurekaServerApplication {
4 
5     public static void main(String[] args) {
6         SpringApplication.run(EurekaServerApplication.class, args);
7     }
8 
9 }

加注解 @EnableEurekaServer

1.4 观察结果 http://127.0.0.1:8761/

2、Eureka Client端

使用eureka客户端 官方文档:http://cloud.spring.io/spring-cloud-netflix/single/spring-cloud-netflix.html#netflix-eureka-client-starter

2.1 pom.xml

1 <dependency>
2         <groupId>org.springframework.cloud</groupId>
3         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
4 </dependency>

2.2 application.yml

server:
  port: 8771

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: product-service

2.3 架构及程序

2.3.1 项目架构:

2.3.2 主程序类

1 @SpringBootApplication
2 public class ProductServiceApplication {
3 
4     public static void main(String[] args) {
5         SpringApplication.run(ProductServiceApplication.class, args);
6     }
7 
8 }

2.3.3 controller

 1 @RestController
 2 @RequestMapping("/api/v1/product")
 3 public class ProductController {
 4 
 5     @Value("${server.port}")
 6     private String port;
 7 
 8     @Autowired
 9     private ProductService productService;
10 
11     /**
12      * 获取所有商品列表
13      * @return
14      */
15     @RequestMapping("list")
16     public Object list(){
17         return productService.listProduct();
18     }
19 
20     /**
21      * 根据id查找商品详情
22      * @param id
23      * @return
24      */
25     @RequestMapping("find")
26     public Object findById(@RequestParam("id") int id){
27         Product product = productService.findById(id);
28         Product result = new Product();
29         BeanUtils.copyProperties(product, result);
30         result.setName(result.getName() + " data from port=" + port);
31         return result;
32     }
33 
34 }

2.3.4 domain

 1 public class Product implements Serializable {
 2 
 3     public Product(){}
 4 
 5     public Product(int id, String name, int price, int store){
 6         this.id = id;
 7         this.name = name;
 8         this.price = price;
 9         this.store = store;
10     }
11 
12     private int id;
13 
14     /**
15      * 商品名称
16      */
17     private String name;
18 
19     /**
20      * 价格
21      */
22     private int price;
23 
24     /**
25      * 库存
26      */
27     private int store;
28 
29     public int getId() {
30         return id;
31     }
32 
33     public void setId(int id) {
34         this.id = id;
35     }
36 
37     public String getName() {
38         return name;
39     }
40 
41     public void setName(String name) {
42         this.name = name;
43     }
44 
45     public int getPrice() {
46         return price;
47     }
48 
49     public void setPrice(int price) {
50         this.price = price;
51     }
52 
53     public int getStore() {
54         return store;
55     }
56 
57     public void setStore(int store) {
58         this.store = store;
59     }
60 }

2.3.5 service

1 public interface ProductService {
2 
3     List<Product> listProduct();
4 
5     Product findById(int id);
6 
7 }
 1 @Service
 2 public class ProductServiceImpl implements ProductService {
 3 
 4     private static final Map<Integer, Product> daoMap = new HashMap<Integer, Product>();
 5 
 6     static {
 7         Product p1 = new Product(1, "iphone1", 1999, 10);
 8         Product p2 = new Product(2, "iphone2", 2999, 10);
 9         Product p3 = new Product(3, "iphone3", 3999, 109);
10         Product p4 = new Product(4, "iphone4", 4999, 190);
11         Product p5 = new Product(5, "iphone5", 5999, 210);
12         Product p6 = new Product(6, "iphone6", 6999, 120);
13         Product p7 = new Product(7, "iphone7", 7999, 10);
14 
15         daoMap.put(p1.getId(), p1);
16         daoMap.put(p2.getId(), p2);
17         daoMap.put(p3.getId(), p3);
18         daoMap.put(p4.getId(), p4);
19         daoMap.put(p5.getId(), p5);
20         daoMap.put(p6.getId(), p6);
21         daoMap.put(p7.getId(), p7);
22 
23     }
24 
25     @Override
26     public List<Product> listProduct() {
27         Collection<Product> collection = daoMap.values();
28         List<Product> list = new ArrayList<>(collection);
29         return list;
30     }
31 
32     @Override
33     public Product findById(int id) {
34         return daoMap.get(id);
35     }
36 }

2.4 开启多个Eureka Client

 

 

 

 

 

 

 

 

 

注:eureka管理后台出现一串红色字体:是警告,说明有服务上线率低

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

关闭检查方法:eureka服务端配置文件加入
server:
  enable-self-preservation: false

 

默认情况下,如果EurekaServer在一 定时间内没有接收到某 个微服务实例的心跳,EurekaServer将会注销该实例(默认90秒)。但是当网络分区故障发生时,微服务与EurekaServer之间无法正常通信,以上行为可能变得非常危险了,因为微服务本身其实是健康的,此时本不应该注销这个微服务。Eureka通过 “自我保护模式”来解决这个问题一当EurekaServer节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。一旦进 入该模式,EurekaServer就会保护服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。当网络故障恢复后,该Eureka Server节点会自动退出自我保护模式。在自我保护模式中,Eureka Server会保护服务注册表中的信息,不再注销任何服务实例。当它收到的心跳数重新恢复到阈值以上时,该Eureka Server节点就会自动退出自我保护模式。它的设计哲学就是宁可保留错误的服务注册信息,也不盲目注销任何可能健康的服务实例。综上,自我保护模式是一 种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留) ,也不盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮、稳定。在Spring Cloud中,可以使用eureka.server.enable-self-preservation = false禁用自我保护模式。在生产环境中,自我保护模式一般不会关闭,且默认是开启状态true

 

posted @ 2019-04-13 15:43  platycoden  阅读(528)  评论(0编辑  收藏  举报