微服务SpringCloud开弓之Eureka服务注册与发现《三》

1、Eureka基础知识

  • 什么是服务治理

在这里插入图片描述

  • 什么是服务注册

在这里插入图片描述

在这里插入图片描述

  • Eureka两组件

在这里插入图片描述

2、单机Eureka构建步骤

  • IDEA生成EurekaServer端服务注册中心

    • 建Module

      cloud-eureka-server7001

    • 改POM

        <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
      
    • 写YML

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

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

    测试

    • http://localhost:7001/
      在这里插入图片描述
  • IDEA生成Eurekaclient端注册到服务端

    • 配置Pom

         <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
              </dependency>
      <dependency>
      
    • yam

      eureka:
        client:
          #false表示不向注册中心注册自己
          register-with-eureka: true
          #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
          fetch-registry: true
          service-url:
            #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
           defaultZone: http://localhost:7001/eureka
      
    • 主启动

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

      1、先启动EurekaServer
      ​2、http://localhost:7001/
      3、测试
      在这里插入图片描述

    ​ 4、自我保护机制

在这里插入图片描述

注意

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

yam的配置注意缩进以及空格问题

3、集群Eureka构建步骤

在这里插入图片描述

解决办法: 搭建Eureka注册中心集群,实现负载均衡+故障容错

互相注册,互相守望

  • 新建cloud-eureka-server7002

    参考cloud-eureka-server7001

  • 改POM

       <dependencies>
            <!--eureka-server-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
            <!--        引用自定义的api通用包,可以使用Payment支付Entity-->
            <dependency>
                <groupId>com.atguigu.springcloud</groupId>
                <artifactId>cloud-api-common</artifactId>
                <version>${project.version}</version>
            </dependency>
            <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>
            <!--一般为通用配置-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
  • 修改映射配置

    1、修改映射配置添加hosts文件

    ​ 127.0.0.1 eureka7001.com

    ​ 127.0.0.1 eureka7002.com

    2、刷新hosts文件

    ​ ipconfig /flushdns

  • 配yml

    7001中的yaml

    server:
      port: 7001
    spring:
      application:
        name: cloud-eureka-service
    eureka:
      instance:
        # eureka服务端的实例名称
        hostname: eureka7001.com
      client:
        # false表示不向注册中心注册自己
        register-with-eureka: false
        # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要检索服务
        fetch-registry: false
        service-url:
          # 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
          defaultZone: http://eureka7002.com:7002/eureka/
    

    7002中的yaml

    server:
      port: 7002
    spring:
      application:
        name: cloud-eureka-service2
    eureka:
      instance:
        hostname:  eureka7002.com
      client:
        register-with-eureka: false
        fetch-registry: false
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka/
    

    主启动

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

    将支付服务8001微服务发布到上面2台Eureka集群配置中

    • yaml
    
    eureka:
      client:
        #false表示不向注册中心注册自己
        register-with-eureka: true
        #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
        fetch-registry: true
        service-url:
          #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
    #      defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
    

    将订单服务80微服务发布到上面2台Eureka集群配置中

    • yaml
    server:
      port: 80
    
    spring:
      application:
        name:  Cloud-order-service
    eureka:
      client:
        #false表示不向注册中心注册自己
        register-with-eureka: true
        #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
        fetch-registry: true
        service-url:
          #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
          #      defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
    

    测试01

    • 先要启动EurekaServer,7001/7002服务
    • 再要启动服务提供者provider,8001
    • 再要启动消费者,80
    • http://localhost/consumer/payment/get/31

4、支付服务提供者8001集群环境搭建

  • 新建cloud-provider-payment8002

    参考cloud-provider-payment8001

  • 改POM

    与cloud-provider-payment8001相同

       <dependencies>
            <!--        eureka-client-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>com.atguigu.springcloud</groupId>
                <artifactId>cloud-api-common</artifactId>
                <version>${project.version}</version>
            </dependency>
            <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>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
            </dependency>
            <!--mysql-connector-java-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
  • YAML

    eureka:
      client:
        #false表示不向注册中心注册自己
        register-with-eureka: true
        #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
        fetch-registry: true
        service-url:
          #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
    #      defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
    
  • 主启动

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

    和8001一样

  • 修改8001/8002的controller

在这里插入图片描述

负载均衡

  • 订单服务访问地址不能写死

在这里插入图片描述

在这里插入图片描述

  • 使用@LoadBalanced注解赋予RestTemplate负载均衡的能力

    @Configuration
    public class ApplicationContextConfig {
    
        @Bean
        @LoadBalanced  //使用@LoadBalanced注解赋予RestTemplate负载均衡的能力
        public RestTemplate getRestTemplate(){
            return  new RestTemplate();
        }
    }
    
  • ApplicationContextBean

    Ribbon的负载均衡功能

  • 测试

在这里插入图片描述

5、actuator微服务信息完善

  • 主机名称:服务名称修改以及没有IP提示

    当前问题

在这里插入图片描述

修改cloud-provoder-payment8001cloud-provoder-payment8001

eureka:
  client:
    #false表示不向注册中心注册自己
    register-with-eureka: true
    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    fetch-registry: true
    service-url:
      #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
#      defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  instance:
    instance-id: payment8001\8002
    prefer-ip-address: true

效果

在这里插入图片描述

6、服务发现Discovery

对于注册eureka里面的微服务,可以通过服务发现来获得该服务的信息

  • 修改cloud-provider-payment8001的Controller

    import org.springframework.cloud.client.discovery.DiscoveryClient;
    @Resource
        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-PROVIDER-SERVICE");
            for (ServiceInstance instance : instances) {
                log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
            }
            return this.discoveryClient;
        }
    
  • 8001的启动类

    @SpringBootApplication
    @EnableEurekaClient
    @EnableDiscoveryClient
    public class PaymentMain8001 {
       public static void main(String[] args) {
           SpringApplication.run(PaymentMain8001.class,args);
       }
    }
    
  • 自测

    在这里插入图片描述

  • 结果
    在这里插入图片描述

6、eureka自我保护

  • 故障现象

在这里插入图片描述

  • 原因

在这里插入图片描述

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

属于CAP里面的AP分支

  • 怎么禁止自我保护

    注册中心eurekaServer端7001

    • 出产默认,自我保护机制是开启的

      eureka.server.enable-self-preservation=true

    • 使用eureka.server.enable-self-preservation=false 可以禁用自我保护模式

在这里插入图片描述

自我保护机制关闭后的结果:

在这里插入图片描述

生产者客户端eurekaClient端8001

  • 默认

    eureka.instance.lease-renewal-interval-in-seconds=30
    eureka.instance.lease-expiration-duration-in-seconds=90
    
  • 配置

在这里插入图片描述

  • 测试

在这里插入图片描述
欢迎转载和分享,记得点赞呀

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值