当前位置:首页 > IT技术 > 数据库 > 正文

SC Alibaba20211019 Nacos 手写LoadBalancer
2021-10-22 17:00:02

1、provider

SC Alibaba20211019 Nacos         手写LoadBalancer_ide

 

 SC Alibaba20211019 Nacos         手写LoadBalancer_返回结果_02

 

 

 

 

 

 

2、 手写LoadBalancer

SC Alibaba20211019 Nacos         手写LoadBalancer_帮助文档_03

 

 SC Alibaba20211019 Nacos         手写LoadBalancer_返回结果_04

 

 SC Alibaba20211019 Nacos         手写LoadBalancer_ide_05

 

 SC Alibaba20211019 Nacos         手写LoadBalancer_ide_06

 

 SC Alibaba20211019 Nacos         手写LoadBalancer_html_07

 

 SC Alibaba20211019 Nacos         手写LoadBalancer_html_08

 

 SC Alibaba20211019 Nacos         手写LoadBalancer_帮助文档_09

 

 SC Alibaba20211019 Nacos         手写LoadBalancer_帮助文档_10

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3、实例

               1)、provider2004

SC Alibaba20211019 Nacos         手写LoadBalancer_ide_11

 

 SC Alibaba20211019 Nacos         手写LoadBalancer_ide_12

 

 SC Alibaba20211019 Nacos         手写LoadBalancer_返回结果_13

 

 

 

       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;

    @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);

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

    }

  }

 

SC Alibaba20211019 Nacos         手写LoadBalancer_返回结果_14

 

 SC Alibaba20211019 Nacos         手写LoadBalancer_ide_15

 

本文摘自 :https://blog.51cto.com/s

开通会员,享受整站包年服务立即开通 >