Loading

五、Eureka注册与发现(二)集群

3、eureka集群构建

clipboard


1)Eureka Server集群环境 7001 7002 构建步骤

① application.yml

互相注册,相互守望

server:
  port: 7001
eureka:
  instance:
    hostname: eureka7001.com
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      #集群版
      defaultZone: http://eureka7002.com:7002/eureka/
      
      
server:
  port: 7002
eureka:
  instance:
    hostname: eureka7002.com
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/


2)支付微服务8001微服务发布到上面eureka集群

①、yml文件配置

server:
  port: 8001

spring:
  application:
    name: cloud-provider-hystrix-payment

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka


3)订单服务80微服务发布到上面eureka集群

server:
  port: 80

spring:
  application:
    name: cloud-consumer-order

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka


4)打开eureka注册中心,查看服务的注册情况

clipboard

clipboard


4、支付微服务集群配置

增加支付服务 payment:8002

clipboard


①、pom文件和payment8001一致

②、yml

server:
  port: 8002

spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/springcloud2?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: houchen

mybatis:
  mapperLocations: classpath:mapper/*.xml        #指定sql映射文件的位置
  type-aliases-package: com.atguigu.springcloud.entity    # 所有Entity别名类所在包
eureka:
  client:
    register-with-eureka: true #表示是否将自己注册到EurekaServer
    fetch-registry: true #是否从eurekaServer抓取已有的注册信息,默认为true,单点无所谓,集群必须设置weitrue才能配合ribbon使用负载均衡
    service-url:
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka

③、主启动类

@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8002 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8002.class,args);
    }
}

④、payment8001 8002 业务类controller

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")   //标记本次order调用payment使用的是哪个端口的服务
    private String serverPort;

    @PostMapping(value = "/payment/create")
    public CommonResult create(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        if(result>0){
            return new CommonResult(200,"插入数据成功 O(∩_∩)O哈哈~"+serverPort);
        }else{
            return new CommonResult(444,"插入数据失败 port:"+serverPort,null);
        }
    }


    // {id}代表者路径占位符
    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("查询数据===热部署后"+payment);
        if(payment!=null){
            return new CommonResult(200,"数据查询成功 port:"+serverPort,payment);
        }else{
            return new CommonResult(444,"数据查询失败 port:"+serverPort,null);
        }
    }

}


查看eureka:发现有两个服务提供者

clipboard


⑤、order80动态选择 payment集群服务中的一台进行访问

在order80中,远程服务的地址不能写死,需要动态写payment服务的spring.application.name

@RestController
@Slf4j
public class OrderController {

    //private static final String PAYMANT_URL ="http://localhost:8001";
    private static final String PAYMANT_URL ="http://CLOUD-PAYMENT-SERVICE";

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/create")
    public CommonResult create(Payment payment){
        return restTemplate.postForObject(PAYMANT_URL+"/payment/create",payment,CommonResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        return restTemplate.getForObject(PAYMANT_URL+"/payment/get/"+id,CommonResult.class);
    }

}


此时访问 http://localhost/consumer/payment/get/1 会报错,因为order80并不知道要调用哪个payment服务

clipboard

此时,要给 RestTemplate开启负载均衡功能

添加配置类:

@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}


访问同一个url ,会在8001 8002之间轮询

clipboard

clipboard


5、微服务信息完善

主机名称的修改 + 偏向ip

eureka:
  instance:
    instance-id: payment8001  
    prefer-ip-address: true    //eureka中的路径会显示ip信息

clipboard

clipboard


6、服务发现Discovery

通过discovey可以获取注册进eureka所有服务的信息

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @Resource
    private DiscoveryClient discoveryClient;


    @GetMapping("/payment/discovery")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();

        for(String service : services){
            log.info("element:",service);
        }

        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;

    }
}

clipboard


7、Eureka自我保护理论知识

概括:某时刻,某一个微服务不可用了,Eureka不会立即清理,依旧会对该服务的信息进行保存

1)为什么会产生eureka的自我保护机制?

为了防止EurekaClient(服务提供者)可以正常运行,但是与EurekaServer网络拥挤的情况下,

EurekaServer不会将EurekaClient服务剔除


2)什么是Eureka的自我保护进制

默认情况下,如果Eureka在一定时间内没有收到某个微服务实例的心跳,EurekaServer将会注销该实例(默认90秒)。但是当网络分区发生故障(延时,卡顿,拥挤),微服务与Eureka之间无法正常通信,那么以上行为就变得非常危险了。 因为此时微服务本身是健康的,此时本不应该注销这个微服务。Eureka通过“自我保护模式来解决这个问题” ——

当EurekaServer节点在短时间内丢失过多客户端时,那么这个节点就会进入自我保护模式


3)关闭eureka的自我保护机制

eureka客户端

clipboard

# Eureka客户端向服务器发送心跳的时间间隔
lease-renewal-interval-in-seconds: 1
# Eureka服务端在收到最后一次心跳后等待的时间上限,单位为秒,超时则剔除服务
lease-expiration-duration-in-seconds: 3


eureka服务端

clipboard

server:
  # 关闭eureka的自我保护机制
  enable-self-preservation: false
  # 每隔2秒,扫描所有的不可用服务并剔除
  eviction-interval-timer-in-ms: 2000


8、Eureka停更说明

2020 年,eureka已经停更了!

clipboard

posted @ 2021-05-28 00:47  青岑  阅读(120)  评论(0编辑  收藏  举报