Dict.CN 在线词典, 英语学习, 在线翻译 ------------- MyGitee 朱秋贵内科诊所 My腾云code

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

posted @ 2021-10-20 09:07  cn2024  阅读(422)  评论(0编辑  收藏  举报