1. 程式人生 > 實用技巧 >springcloud-基於RestTemplate+Ribbon的服務呼叫

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