zoukankan      html  css  js  c++  java
  • SC Alibaba20211019 Nacos 手写LoadBalancer LoadBalancerClient

    1、provider

     

    2、 手写LoadBalancer

     

     

     

     

     

     

     

    3、实例

                   1)、provider2004

     

     

           2)、consumer2018    手写LoadBalancer   

             i)、pom.xml

             

      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
      </dependency>

           ii)、application.properties

          # 应用名称
      spring.application.name=consumer2018
      # 应用服务 WEB 访问端口
      server.port=2018
      # Nacos帮助文档: https://nacos.io/zh-cn/docs/concepts.html
      # Nacos认证信息
      spring.cloud.nacos.discovery.username=nacos
      spring.cloud.nacos.discovery.password=nacos
      # Nacos 服务发现与注册配置,其中子属性 server-addr 指定 Nacos 服务器主机和端口
      spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
      # 注册到 nacos 的指定 namespace,默认为 public
      spring.cloud.nacos.discovery.namespace=public

           iii)、LoadBalancer 

        public interface LoadBalancer {

          ServiceInstance getSingleAddres(List<ServiceInstance> serviceInstances);
        }

           iiii)、RotationLoadBalancer 


      @Component
      public class RotationLoadBalancer implements LoadBalancer{

      private AtomicInteger atomicInteger=new AtomicInteger(0);

        @Override
        public ServiceInstance getSingleAddres(List<ServiceInstance> serviceInstances) {
          int index=atomicInteger.incrementAndGet()%serviceInstances.size();
          return serviceInstances.get(index);
        }
      }

            iiiii)、Consumer2018Application 

      @SpringBootApplication
      public class Consumer2018Application {

        public static void main(String[] args) {
          SpringApplication.run(Consumer2018Application.class, args);
        }

        @Bean
        public RestTemplate restTemplate(){
        return new RestTemplate();
        }
      }

           iiiiii)、OrderService

         

      @RestController
      public class OrderService {
        @Autowired
        private DiscoveryClient discoveryClient;
        @Autowired
        private RestTemplate restTemplate;

        @Autowired
        private LoadBalancer loadBalancer;

                   @Resource
              private LoadBalancerClient loadBalancerClient;

        @RequestMapping("/orderToMember")
        public String orderToMember(){
          List<ServiceInstance> instances=discoveryClient.getInstances("provider2004");
          ServiceInstance serviceInstance=instances.get(0);
          String rpcMemberUrl2=serviceInstance.getUri().toString();
          String result2=restTemplate.getForObject(rpcMemberUrl2+"/hello/123131321312",String.class);

          String rpcMemberUrl=loadBalancer.getSingleAddres(instances).getUri().toString();
          String result=restTemplate.getForObject(rpcMemberUrl+"/hello/123131321312",String.class);

           ServiceInstance serviceInstance1=loadBalancerClient.choose("provider2004");
                String rpcMemberUrl3=serviceInstance1.getUri().toString()+"/hello/123131321312";

          return "调用provider2004 返回结果:"+result;
        }
      }

     

    参考:https://www.cnblogs.com/songjilong/p/12752115.html

    转https://www.cnblogs.com/liujinhui/p/15195448.html

  • 相关阅读:
    Nubia Z5S官方4.4 UI2.0音频Audio部分简单分析(也适用于其它8974/8064机型)以及降低破音出现几率的方法
    import MySQLdb UserWarning
    Visual Studio 2013 Update2
    cocos2d 重写顶点着色语言
    tomcat之组成结构
    4、Android Activity的生命周期 Activity的生命周期
    学习中遇到的c++问题,持续更新
    Android开发之发送邮件功能的实现(源码分享)
    Java 类型, Hibernate 映射类型及 SQL 类型之间的相应关系
    QT内label控件通过opencv显示图像
  • 原文地址:https://www.cnblogs.com/smallfa/p/15427392.html
Copyright © 2011-2022 走看看