批里批里 (゜-゜)つ🍺 干杯~|

七つ一旋桜

园龄:4年2个月粉丝:6关注:3

2021-10-03 00:07阅读: 58评论: 0推荐: 0

SpringCloud学习笔记(三) 服务注册中心

Eureka服务注册与发现

新建Eureka模块

在传统的rpc远程调用框架中,管理每个服务与服务之间的依赖关系比较复杂,所以需要服务治理,,管理服务与服务之间的依赖关系,实现服务调用、负载均衡、容错和服务的注册与发现等

  1. 新建项目

    image-20211002194120769

  2. 修改pom文件

    <dependencies>
            <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
    
            <dependency>
                <groupId>zjc</groupId>
                <artifactId>CommonComponent</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web  -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
    
            <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </dependency>
    
        </dependencies>
    
  3. 配置yml文件

    server:
      port: 7001
    
    eureka:
      instance:
        hostname: localhost  #eureka服务端的实例名字
      client:
        register-with-eureka: false    #表识不向注册中心注册自己
        fetch-registry: false   #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
        service-url:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/    #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
    
    
  4. 配置主启动类

    package zjc;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer // 添加这个注解指定启动类为服务注册中心
    public class EurekaMain7001 {
    	public static void main(String[] args) {
    		SpringApplication.run(EurekaMain7001.class, args);
    	}
    }
    
  5. 启动项目

    访问localhost:7001后可以看到如下页面

    image-20211002195633401

注册子模块进入eureka注册中心

将提供者注册进注册中心

修改cloud-provider-8001模块的配置使其能够注册进eureka

  1. 子模块加入eureka依赖(和服务中心的依赖不一样

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    
  2. 子模块yml添加eureka配置

    eureka:
      client:
        register-with-eureka: true # 表示是否将自己注册进注册中心,默认为true
        fetch-registry: true # 是否从注册中心抓取已有的注册信息,默认为true,单节点可以不设置,集群必须设置才能配合ribbon使用负载均衡
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka
    
  3. 启动类添加注解配置

    启动类添加如下注解

    @EnableEurekaClient // 添加这个注解指定启动类为客户端
    
  4. 启动项目

    先后启动eureka模块和provider模块

    访问localhost:7001

    image-20211002201403012

    可以看到provider模块已经被注册进eureka注册中心

将消费者注册进服务注册中心

修改cloud-consumer-server80模块的相关配置

  1. 修改pom.xml配置

    添加eureka相关依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    
  2. 修改yml配置文件

    添加如下内容

    spring:
      application:
        name: cloud-order-service
    
    eureka:
      client:
        register-with-eureka: true
        fetchRegistry: true
        service-url:
          defaultZone: http://localhost:7001/eureka
    
  3. 添加主启动类的注解

    @EnableEurekaClient
    
  4. 启动项目

    访问localhost:7001

    看到消费者也被注册

    image-20211002203626077

eureka集群

每个服务注册中心之间相互注册相互守望

第二个服务注册中心

使用switchhost工具修改端口映射

因为有多个服务注册中心,为了避免服务注册中心的hostname重名,使用switchhost工具对端口进行映射

  1. 下载switchhosts

    下载地址:SwitchHosts 网盘地址

  2. 修改映射

    以管理员权限启动switchhosts

    新建映射方案

    image-20211002211739406

    在映射方案中添加以下内容

    image-20211002211825541

    image-20211002232851618

新建注册中心

  1. 根据cloud-eureka-server7001模块新建cloud-eureka-server7002模块

    image-20211002210058645

  2. 修改cloud-eureka-server7001模块的配置文件

    server:
      port: 7001
    
    eureka:
      instance:
        hostname: eureka7001.com  #eureka服务端的实例名字
      client:
        register-with-eureka: false  
        fetch-registry: false  
        service-url:
          defaultZone: http://eureka7002.com:7002/eureka/    #相互注册相互守望
    
  3. 修改cloud-eureka-server7002的配置文件

    server:
      port: 7002
    
    eureka:
      instance:
        hostname: eureka7002.com  #eureka服务端的实例名字
      client:
        register-with-eureka: false   
        fetch-registry: false  
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka/    #相互注册相互守望
    
  4. 启动项目

    依次启动两个服务注册中心

    看到如下结果说明启动成功

    image-20211002213155667

    image-20211002213233912

将子模块注册进eureka集群

修改cloud-consumer-order80模块和cloud-provider-payment8001的yml配置文件

eureka:
  client:
    register-with-eureka: true # 表示是否将自己注册进注册中心,默认为true
    fetch-registry: true # 是否从注册中心抓取已有的注册信息,默认为true,单节点可以不设置,集群必须设置才能配合ribbon使用负载均衡
    service-url:
      #defaultZone: http://eureka7001.com:7001/eureka # 单机版
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka  #集群版

依次启动四个服务

image-20211002214456937

访问页面,弹道如下结果表示服务注册成功

image-20211002214343805

将提供者集群注册进服务注册中心

第二个提供者

  1. 根据cloud-provider-payment8001模块新建cloud-provider-payment8002模块

    image-20211002220132419

  2. 修改yml配置

    除了端口映射其他和cloud-provider-payment8001模块一样

    server:
      port: 8002
    
  3. 修改两个提供者的controller

    image-20211002220523439

    添加以下两行代码

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

    并修改返回结果集

    image-20211002220943655

    这样就能从返回结果得知返回结果是从集群中的哪个提供者响应的

  4. 启动负载均衡

    修改cloud-consumer-order80模块的controller

    将ip地址改为服务提供者的服务名

    //	public static final String PAYMENT_URL = "http://localhost:8001";
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
    

    修改config,添加@LoadBalanced注解

    image-20211002222824592

  5. 启动项目

    访问localhost/consumer/payment/get/1

    image-20211002223054058image-20211002223142260

    可以同时访问到来自8001端口和8002端口的结果集,说明负载均衡开启成功

修改主机名

修改提供者的yml配置

eureka:
  client:
    register-with-eureka: true # 表示是否将自己注册进注册中心,默认为true
    fetch-registry: true # 是否从注册中心抓取已有的注册信息,默认为true,单节点可以不设置,集群必须设置才能配合ribbon使用负载均衡
    service-url:
      #defaultZone: http://eureka7001.com:7001/eureka # 单机版
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka  #集群版
  instance:
    instance-id: payment8001 # 修改主机名
  	prefer-ip-address: true # 访问路径显示ip地址

可以看到主机名发生了变化

image-20211002225221260

使用actuator健康检查得到如下结果说明服务正常

image-20211002225408729

服务发现Discovery

提供者将自己的基础服务信息提交给服务注册中心

  1. 修改服务提供者的controller

    添加以下内容

    @Resource
    private DiscoveryClient discoveryClient;
    
    @GetMapping("/payment/discovery")
    public Object discovery() {
        // 方式一 得到服务清单列表
        List<String> services = discoveryClient.getServices();
        services.forEach(i -> log.info("{}", i));
    
        // 方式二 得到服务实例
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        instances.forEach(i ->
                          log.info("{}\t{}\t{}\t{}", i.getServiceId(), i.getHost(), i.getPort(), i.getUri())
                         );
    
        return this.discoveryClient;
    }
    
  2. 修改启动类

    添加@EnableDiscoveryClient注解

  3. 启动项目

    访问localhost:8001/payment/discovery得到如下结果

    image-20211002233440815

    可以看到DiscoveryClient使用了两种方式输出了CLOUD-PAYMENT-SERVICE的相关信息

    image-20211002233817275

Eureka的自我保护

保护机制:

  • 某时刻某一微服务不可用了,eureka不会立即清理,而是依旧会保留微服务的信息
  • client会定时向server发送心跳包,如果server一定时间内没有收到某个服务心跳包,就会把这个服务清除
  • 如果在指定时间内server没有收到client的心跳包,server会暂时保留服务

关闭自我保护机制

  1. 修改cloud-eureka-server7001模块的yml配置

    添加以下内容

    server:
    	enable-self-preservation: false # 关闭自我保护
    	eviction-interval-timer-in-ms: 2000 # 心跳检测间隔为2秒
    

    image-20211003000022534

  2. 修改客户端的yml配置

    添加以下内容

    lease-renewal-interval-in-seconds: 1 # 客户端向服务端发送心跳的时间间隔(默认30秒)
    lease-expiration-duration-in-seconds: 2 # 服务端最后一次检测客户端心跳的时间间隔(默认90秒)
    

    image-20211003000310748

本文作者:七つ一旋桜

本文链接:https://www.cnblogs.com/poifa/p/15363108.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   七つ一旋桜  阅读(58)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起