赞
踩
目录
Spring Cloud入门+深入(三)-Eureka服务注册与发现(一)
Eureka集群构建原理说明
问题:微服务RPC远程服务调用最核心的是什么?
高可用,如果注册中心只有一个,它出故障了那就导致整个微服务环境不可用。单点故障。
解决方案:搭建Eureka注册中心集群,实现负载均衡+故障容错。
实现方式:相互注册,相互守望。如下图
如果在一台机器上搭建集群,需要修改hosts配置文件,防止重名。
C:\Windows\System32\drivers\etc路径下的hosts文件
- 127.0.0.1 eureka7001.com
- 127.0.0.1 eureka7002.com
1.1、EurekaServer端cloud-eureka-server7001修改
yml修改
- server:
- port: 7001
-
- eureka:
- instance:
- hostname: eureka7001.com #eureka服务端的实例名字
- client:
- register-with-eureka: false #false 表示不向注册中心注册自己
- fetch-registry: false #false 表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
- service-url:
- defaultZone: http://eureka7002.com:7002/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
1.2、EurekaServer端cloud-eureka-server7002新建
pom和cloud-eureka-server7001一样
主启动类内容和cloud-eureka-server7001一样
yml
- server:
- port: 7002
-
- eureka:
- instance:
- hostname: eureka7002.com #eureka服务端的实例名字
- client:
- register-with-eureka: false #false 表示不向注册中心注册自己
- fetch-registry: false #false 表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
- service-url:
- defaultZone: http://eureka7001.com:7001/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
启动cloud-eureka-server7001和cloud-eureka-server7002
上面验证集群Eureka构建成功
1.3、EurekaClient端cloud-provider-payment8001
修改yml
- service-url:
- #defaultZone: http://localhost:7001/eureka
- defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
1.4、EurekaClient端cloud-consumer-order80
修改yml
- service-url:
- #defaultZone: http://localhost:7001/eureka
- defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
先要启动EurekaServer,7001/7002服务
再要启动服务提供者provider,8001服务
再要启动消费者,80
1.5、EurekaClient端支付服务集群搭建--新建cloud-provider-payment8002
参考cloud-provider-payment8001,新建cloud-provider-payment8002,端口号改为8002
修改8001/8002的Controller,增加端口进行区别:
- package com.lwz.springcloud.controller;
-
- import com.lwz.springcloud.entities.CommonResult;
- import com.lwz.springcloud.entities.Payment;
- import com.lwz.springcloud.service.PaymentService;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.*;
-
- import javax.annotation.Resource;
-
- @RestController
- @Slf4j
- public class PaymentController {
-
- @Resource
- private PaymentService paymentService;
-
- @Value("${server.port}")
- private String serverPort;
-
- @PostMapping(value = "/payment/create")
- public CommonResult create(@RequestBody Payment payment){
- int result = paymentService.create(payment);
- log.info("*****插入结果:"+result);
- if (result>0){ //成功
- return new CommonResult(200,"插入数据库成功,serverPort:"+serverPort,result);
- }else {
- return new CommonResult(444,"插入数据库失败",null);
- }
- }
- @GetMapping(value = "/payment/get/{id}")
- public CommonResult getPaymentById(@PathVariable("id") Long id){
- Payment payment = paymentService.getPaymentById(id);
- log.info("*****查询结果:"+payment);
- if (payment!=null){ //说明有数据,能查询成功
- int a=1;
- return new CommonResult(200,"查询成功,serverPort"+serverPort,payment);
- }else {
- return new CommonResult(444,"没有对应记录,查询ID:"+id,null);
- }
- }
- }
都完事后进行修改cloud-consumer-order80服务的Controller
- //public static final String PAYMENT_URL = "http://localhost:8001";
- public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
修改cloud-consumer-order80服务的config @LoadBalanced //赋予RestTemplate负载均衡的能力
- package com.lwz.springcloud.config;
-
- import org.springframework.cloud.client.loadbalancer.LoadBalanced;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.client.RestTemplate;
-
- @Configuration
- public class ApplicationContextConfig {
-
- @Bean
- @LoadBalanced //赋予RestTemplate负载均衡的能力
- public RestTemplate getRestTemplate(){
- return new RestTemplate();
- }
-
- }
先要启动EurekaServer,7001/7002服务
再要启动服务提供者provider,8001,8002服务
再要启动消费者,80
连续访问:http://localhost/consumer/payment/get/31
结果切换:
{"code":200,"message":"查询成功,serverPort:8001","data":{"id":31,"serial":"张无忌001"}}
{"code":200,"message":"查询成功,serverPort:8002","data":{"id":31,"serial":"张无忌001"}}
到此架构图如下:
Eureka网页服务主机名修改,和IP显示配置
实现:
pom
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
yml
- eureka:
- instance:
- instance-id: payment8001
- prefer-ip-address: true #访问路径可以显示IP
全yml
- eureka:
- client:
- register-with-eureka: true
- fetchRegistry: true
- service-url:
- #defaultZone: http://localhost:7001/eureka
- defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
- instance:
- instance-id: payment8002
- prefer-ip-address: true #访问路径可以显示IP
最终效果:
服务发现Discovery:对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息。
修改cloud-provider-payment8001的Controller
- @Autowired
- private DiscoveryClient discoveryClient;
-
- @GetMapping(value = "/payment/discovery")
- public Object discovery(){
- List<String> services = discoveryClient.getServices();
- for (String element : services) {
- log.info("***** element:"+element);
- }
- List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
- for (ServiceInstance instance : instances) {
- log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
- }
- return this.discoveryClient;
- }
-
8001主启动类 增加注解 @EnableDiscoveryClient
重启:
访问:http://localhost:8001/payment/discovery
结果:{"discoveryClients":[{"services":["cloud-payment-service","cloud-order-service"],"order":0},{"services":[],"order":0}],"services":["cloud-payment-service","cloud-order-service"],"order":0}
Eureka网页出现如下红色醒目字迹,代表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自我保护机制?
Eureka的自我保护机制主要是为了网络异常时保持高可用设计的,当在Eureka中注册的微服务超过设定是时间内(默认90秒)没有向Eureka服务端发送心跳,该微服务会进入自我保护模式。在自我保护模式中,Eureka会保护服务注册表中的信息,不会注销任何服务实例,直至收到的心跳数恢复至阈值以上,该微服务退出自我保护模式。
自我保护机制理解:Eureka的设计哲学是宁可保留错误的服务信息,也不盲目注销可能健康的服务。所以异常的服务不会被注销,而是进入了自我保护模式。
解释:某时刻某一个微服务不可用了,Eureka不会立刻清理,依旧会对该微服务的信息进行保存
怎么禁止自我保护?
yml
- eureka:
- server:
- enable-self-preservation: false # 关闭自我保护保证不可用服务被删除
- eviction-interval-timer-in-ms: 2000 #修改时间间隔
eureakeClient端
yml
- eureka:
- instance:
- lease-renewal-interval-in-seconds: 1 # Eureka客户端发送心跳的时间间隔,单位为秒 (默认30秒)
- lease-expiration-duration-in-seconds: 90 # Eureka服务端在收到最后一次心跳等待时间上限,单位为秒 (默认90秒)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。