0
点赞
收藏
分享

微信扫一扫

Android 手机对于Arduino蓝牙控制解决方案

目录

1.什么是Ribbo❤️❤️❤️

2.eureka自带Ribbon ❤️❤️❤️

3. RestTemplate❤️❤️❤️

 4.IRule❤️❤️❤️

 5.负载均衡算法❤️❤️❤️


1.什么是Ribbo

2.eureka自带Ribbon 

3. RestTemplate

  @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Integer id) {
        log.info("进入查询功能成功");
        return restTemplate.getForObject(PAYMENT_URL + "/payment/selectById/" + id, CommonResult.class);
    }

    @GetMapping("/payment/get2/{id}")
    public CommonResult<Payment> getPayment2(@PathVariable("id") Integer id) {
        log.info("进入查询功能成功");
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/selectById/" + id, CommonResult.class);
        if (entity.getStatusCode().is2xxSuccessful()){
            log.info(entity.getStatusCode()+"\t"+entity.getHeaders());
            return entity.getBody();
        }else {
            return new CommonResult<>(404,"查询失败~");
        }
    }

 4.IRule

4.1轮询策略

4.2如何替换 

1.创建配置类

2.创建IRule对象 

@Configuration
public class MySelfRule {
    @Bean
    public IRule myRule(){
        return new RandomRule();//定义为随机
    }
}

 3.添加@RibbonClient

@SpringBootApplication
@EnableDiscoveryClient
@RibbonClient(name = "COULD-PAYMENT-SERVICE",configuration = MySelfRule.class)
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class);
    }
}

 5.负载均衡算法

5.1原理

 5.2手写轮询

  • 自定义轮询算法
@Component
public class MyLb implements LoadBalance {

    private AtomicInteger atomicInteger = new AtomicInteger(0);

    public final int getAndIncrement() {
        int current;
        int next;
        do {
            current = this.atomicInteger.get();
            next = current >= Integer.MAX_VALUE ? 0 : current + 1;
        } while (!this.atomicInteger.compareAndSet(current, next));
        System.out.println("next:" + next);
        return next;
    }

    @Override
    public ServiceInstance instances(List<ServiceInstance> serviceInstance) {
        int index = getAndIncrement() % serviceInstance.size();

        return serviceInstance.get(index);
    }
}
  • 80服务使用自定义轮询
    //自己的负载均衡算法
    @Autowired
    private LoadBalance loadBalance;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/payment/lb")
    public String getPaymentLb() {
        List<ServiceInstance> instances = discoveryClient.getInstances("COULD-PAYMENT-SERVICE");
        if (instances == null || instances.size() < 0) {
            return null;
        }
        ServiceInstance serviceInstance=loadBalance.instances(instances);
        URI uri = serviceInstance.getUri();
        return restTemplate.getForObject(uri+"/payment/lb",String.class);
    }

举报

相关推荐

0 条评论