springcloud-基于RestTemplate+Ribbon的服务调用

1.依赖

consul依赖中包含ribbon依赖无需额外引入

2.基于discovery Client形式调用

    @RequestMapping("/user/showProductMsg")
    public List<ServiceInstance> showProductMsg() {
        //discovery Client形式调用
        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("productservices");
        for (ServiceInstance serviceInstance : serviceInstances) {
            System.out.println(serviceInstance.getHost());
            System.out.println(serviceInstance.getPort());
        }
        return serviceInstances;
    }

 

[{"instanceId":"productservices-9001","serviceId":"productservices","host":"DESKTOP-VQE7HEH","port":9001,"secure":false,"metadata":{"secure":"false"},"uri":"http://DESKTOP-VQE7HEH:9001","scheme":null},{"instanceId":"productservices-9003","serviceId":"productservices","host":"DESKTOP-VQE7HEH","port":9003,"secure":false,"metadata":{"secure":"false"},"uri":"http://DESKTOP-VQE7HEH:9003","scheme":null}]

 可以获取服务列表,无负载均衡

3.基于loadBalance Client形式调用

    @RequestMapping("/user/showProductMsg")
    public ServiceInstance showProductMsg() {
        //loadBalance Client形式调用
        ServiceInstance serviceInstance = loadBalancerClient.choose("productservices");
            System.out.println(serviceInstance.getHost());
            System.out.println(serviceInstance.getPort());
        return serviceInstance;
    }

 

{"serviceId":"productservices","server":{"host":"DESKTOP-VQE7HEH","port":9003,"scheme":null,"id":"DESKTOP-VQE7HEH:9003","zone":"UNKNOWN","readyToServe":true,"metaInfo":{"instanceId":"productservices-9003","appName":"productservices","serverGroup":null,"serviceIdForDiscovery":null},"metadata":{"secure":"false"},"passingChecks":true,"healthService":{"node":{"id":"95a5f00e-4d61-c010-c997-5c30e5cbc400","node":"DESKTOP-VQE7HEH","address":"127.0.0.1","datacenter":"dc1","taggedAddresses":{"lan":"127.0.0.1","lan_ipv4":"127.0.0.1","wan":"127.0.0.1","wan_ipv4":"127.0.0.1"},"meta":{"consul-network-segment":""},"createIndex":10,"modifyIndex":11},"service":{"id":"productservices-9003","service":"productservices","tags":["secure=false"],"address":"DESKTOP-VQE7HEH","meta":{},"port":9003,"enableTagOverride":false,"createIndex":15,"modifyIndex":15},"checks":[{"node":"DESKTOP-VQE7HEH","checkId":"serfHealth","name":"Serf Health Status","status":"PASSING","notes":"","output":"Agent alive and reachable","serviceId":"","serviceName":"","serviceTags":[],"createIndex":10,"modifyIndex":10},{"node":"DESKTOP-VQE7HEH","checkId":"service:productservices-9003","name":"Service 'productservices' check","status":"PASSING","notes":"","output":"HTTP GET http://DESKTOP-VQE7HEH:9003/actuator/health: 200  Output: {\"status\":\"UP\"}","serviceId":"productservices-9003","serviceName":"productservices","serviceTags":["secure=false"],"createIndex":15,"modifyIndex":17}]},"alive":true,"hostPort":"DESKTOP-VQE7HEH:9003"},"secure":false,"metadata":{"secure":"false"},"scheme":null,"host":"DESKTOP-VQE7HEH","port":9003,"instanceId":"DESKTOP-VQE7HEH:9003","uri":"http://DESKTOP-VQE7HEH:9003"}

根据负载均衡策略选取某一个服务

4.基于@loadBalanced形式调用

@Configuration
public class RestTemplateConfig {
    //在工厂中创建一个restTemplate对象
    @Bean
    @LoadBalanced //代表ribbon负载均衡的restTemplate客户端对象
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

 

    @RequestMapping("/user/showProductMsg")
    public String showProductMsg() {
       String msg = restTemplate.getForObject("http://productservices/product/findAll",String.class);
        return msg;
    }

 整合restTemplate + ribbon

5.更改ribbon负载均衡算法

productservices.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule

 

posted @ 2020-08-09 12:14  陶海军  阅读(312)  评论(0编辑  收藏  举报