• 【SpringCloud】服务提供者集群与服务发现Discovery(三)


      阅读本章,请先阅读【SpringCloud】快速入门(一)

      本章使用Demo,是在SpringCloud单机版的基础上,扩充的。

    服务提供者集群

      既然SpringCloud的是微服务结构,那么对于同一种服务,当然不可能只有一个节点,需要部署多个节点

      架构图如下:

      

      由上可以看出存在多个同一种服务提供者(Service Provider)

      搭建服务提供者集群

      1、参考:【SpringCloud】SpringCloud 快速入门(一)搭建单机版的:Eureka Server、Service Provider、Service Consumer

      2、根据支付模块服务提供者(test-springcloud-provider-payment8001),在父工程中,同样新建一个支付模块的服务提供者(test-springcloud-provider-payment8002)

        服务8001与8002的配置中,除端口外,其他都相同,且spring.application.name应用名称必须相同,表明2个服务是同一种服务

        服务8002配置文件如下:

     1 # 端口
     2 server:
     3   port: 8002
     4 
     5 spring:
     6   application:
     7     name: cloud-payment-service
     8   #   数据源基本配置
     9   datasource:
    10     driver-class-name: com.mysql.cj.jdbc.Driver
    11     url: jdbc:mysql://localhost:3306/test_springcloud?allowPublicKeyRetrieval=true&useSSL=true
    12     username: admin
    13     password: 123456
    14 
    15 eureka:
    16   client:
    17     # 表示将自己注册进Eureka Server默认为true
    18     register-with-eureka: true
    19     # 是否从Eureka Server抓去已有的注册信息,默认是true
    20     fetch-registry: true
    21     # 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
    22     service-url:
    23       defaultZone: http://localhost:8761/eureka
    24 
    25 mybatis:
    26   mapperLocations: classpath:mapper/*Mapper.xml
    27   # 所有entity别名类所在的包
    28   type-aliases-pachage: com.test.springcloud.entities
    View Code

      3、重新启动项目,打开Eureka查看,发现有2个支付服务

        

      4、使用订单模块消费者调用支付服务

        消费者部分代码模块如下:

     1 @Configuration
     2 public class AppConfig {
     3 
     4     /**
     5      * 注入restTemplate,请用请求rest接口
     6      * @return
     7      */
     8     @Bean
     9     // 标注此注解后,RestTemplate就具有了客户端负载均衡能力
    10     // 负载均衡技术依赖于的是Ribbon组件~
    11     // RestTemplate都塞入一个loadBalancerInterceptor 让其具备有负载均衡的能力
    12     @LoadBalanced
    13     public RestTemplate restTemplate(){
    14         return new RestTemplate();
    15     }
    16 }
    17 
    18 
    19 @RestController
    20 @Slf4j
    21 public class OrderController {
    22 
    23     public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
    24 
    25     @Autowired
    26     private RestTemplate restTemplate;
    27 
    28     @GetMapping("/consumer/payment/get/{id}")
    29     public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
    30         return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
    31     }
    32 
    33     ...
    34 }

        由上可以,通过配置向容器中注入了RestTemplate对象,而RestTemplate又被标注此@LoadBalanced注解后,RestTemplate就具有了客户端负载均衡能力,也就是说RestTemplate会轮流调用服务的各个节点,到达均衡的目的

      5、测试调用,启动项目,使用地址:http://localhost:8000/consumer/payment/get/1,进行访问,可以看到已到达负载均衡的目的

        

    服务发现Discovery

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

      案例如下:

      1、在微服务中使用@EnableDiscoveryClient注解,启用服务发现

     1 // Eureka客户端
     2 @EnableEurekaClient
     3 // 启用服务发现
     4 @EnableDiscoveryClient
     5 @SpringBootApplication
     6 public class PaymentMain8001 {
     7     public static void main(String[] args) {
     8         SpringApplication.run(PaymentMain8001.class, args);
     9     }
    10 }

      2、编辑Controller,启用服务发现之后,它会自动向容器注入DiscoveryClient(服务发现客户端)

        通过调用DiscoveryClient的getServices方法,从注册中心获取服务列表

        通过调用DiscoveryClient的getInstances方法,从注册中心获取服务实例集

     1 public class PaymentController {
     2 
     3     @Autowired
     4     private DiscoveryClient discoveryClient;
     5 
     6     @GetMapping(value = "/payment/discovery")
     7     public Object discovery(){
     8         // 获取服务列表
     9         List<String> services = discoveryClient.getServices();
    10         for (String element : services) {
    11             log.info("=====element:" + element);
    12         }
    13         
    14         // 获取服务实例集
    15         List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
    16         for (ServiceInstance instance : instances) {
    17             log.info("服务发现" + "
    "
    18                     + "getServiceId === " + instance.getServiceId() + "
    "
    19                     + "getHost === " + instance.getHost() + "
    "
    20                     + "getPort === " + instance.getPort() + "
    "
    21                     + "getUri === " + instance.getUri() );
    22         }
    23 
    24         return  this.discoveryClient;
    25     }
    26 
    27

       3、启用服务,使用地址:http://localhost:8001/payment/discovery,进行访问

        页面结果:

        

        后台日志:

        

        由上可知,通过DiscoveryClient能获取到注册进eureka里面的微服务信息。

  • 相关阅读:
    yii框架入门学习笔记二 根据数据表创建model类
    mysql数据库操纵类升级版
    yii框架入门学习笔记三 完善登陆功能
    java发送http的get、post请求
    Please ensure that adb is correctly located at 'D:\androidsdkwindows\platf
    CodeIgniter框架入门教程——第一课 Hello World!
    C#正则表达式应用
    common lisp 笔记2 a cd ripper
    javascript的String函数
    修改linux下sudo设置
  • 原文地址:https://www.cnblogs.com/h--d/p/12641240.html
Copyright © 2020-2023  润新知