• 1. 微服务之Eureka服务注册发现


    1. Eureka的作用

    在这里插入图片描述
    1. 消费者该如何获取服务提供者具体信息?

    • 服务提供者启动时向 eureka 注册自己的信息
    • eureka 保存这些信息
    • 消费者根据服务名称向 eureka 拉取提供者信息

    2. 如果有多个服务提供者,消费者该如何选择?

    • 服务消费者利用负载均衡算法,从服务列表中挑选一个

    3. 消费者如何感知服务提供者健康状态?

    • 服务提供者会每隔 30s 向EurekaServer 发送心跳请求,报告健康状态
    • eureka 会更新记录服务列表信息,心跳不正常会被剔除
    • 消费者就可以拉取到最新的信息
    2. 服务之间的远程调用方法
    1. 在 SpringbootApplication 运行类中添加 RestTemplate 对象
    @SpringBootApplication
    public class xxxApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(xxxApplication.class, args);
        }
    
        @Bean
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    1. 调用方法 restTemplate.getForObject
    String url = "http://xxx" ;
    User user = restTemplate.getForObject(url,User.class);
    
    • 1
    • 2
    3. Eureka 的使用步骤
    3.1. 搭建 EurekaServer 服务

    新建一个模块,在 pom.xml 中引入依赖

    <dependency>
    	<groupId>org.springframework.cloudgroupId>
    	<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    添加 application.yml 文件,添加配置

    server:
      port: 10086
    spring:
      application:
        name: eurekaserver
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:10086/eureka/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    编写启动类,添加 @EnableEurekaServer 注解开启EurekaServer自动装配

    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekaApplication.class,args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    3.2 服务模块注册到 EurekaServer 中 - 服务提供者

    在 pom.xml 中引入依赖

    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    在 application.yml 中进行配置

    spring:
      application:
        name: user-service
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:10086/eureka/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    3.3 服务模块通过 EurekaServer 拉取其它服务 - 服务消费者

    先进行注册,参照 3.2
    改调用地址

    String url = "http://服务提供者name/xxx/" + order.getUserId();
    User user = restTemplate.getForObject(url,User.class);
    
    • 1
    • 2

    在启动类中的 RestTemplate 添加负载均衡注解 @LoadBalanced

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在调用某服务时若存在多个实例模块,则要采用负载均衡策略

    3.4 扩展

    在 idea 中对一个模块添加多个运行对象
    在这里插入图片描述
    在这里插入图片描述

    3.5 EurekaServer运行结果

    在这里插入图片描述

  • 相关阅读:
    megahit源码迁移解析
    FS4059C ESOP8 5V升压充电12.6V三串锂电池充电IC
    让我们拥抱DataV,感受数据可视化的魅力
    Spring MVC的请求处理逻辑
    信息系统项目管理师---第十一章项目风险管理历年考题
    工作中总结的30个常用Linux指令,实在记不住就别硬记了,看这篇就够了
    035、目标检测-物体和数据集
    小学生python编程---忍者大战
    【LeetCode】16.最接近的三数之和
    P1830 轰炸III
  • 原文地址:https://blog.csdn.net/qq_46656580/article/details/127771386