zoukankan      html  css  js  c++  java
  • Spring RestTemplate具备负载均衡功能

       创建RestTemplate的Bean时使用@LoadBalanced注解, 就可以自动配置为使用ribbon。如下面的示例所示:

    @Configuration
    public class MyConfiguration {
    
        @LoadBalanced
        @Bean
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }
    
    public class MyClass {
        @Autowired
        private RestTemplate restTemplate;
    
        public String doOtherStuff() {        //注意:代码中的url要使用服务名,而不是主机名
            String results = restTemplate.getForObject("http://stores/stores", String.class);
            return results;
        }
    }
    

      多个RestTemplate对象 

    @Configuration
    public class MyConfiguration {
    
        @LoadBalanced
        @Bean
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
    
        @Primary
        @Bean
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }
    
    public class MyClass {
        @Autowired
        private RestTemplate restTemplate;
        
        @LoadBalanced
        @Autowired
        private RestTemplate restTemplateLoadBalanced;
        
    
        public String doOtherStuff() {        //注意:代码中的url要使用服务名,而不是主机名
            String results = restTemplate.getForObject("http://stores/stores", String.class);
            return results;
        }
    }
  • 相关阅读:
    HDU_1242_Rescue
    HDU_1175_连连看
    HDU_1072_Nightmare
    HDU_2544_最短路
    POJ_2195_Going Home
    POJ_3565_Ants
    KM算法(Kuhn-Munkres)
    POJ_2536_Gopher II
    ODATA 云驱动 http://www.cdata.com/cloud/
    Wijmo 5 与Breeze 的组合,及与METRONIC 的集成
  • 原文地址:https://www.cnblogs.com/yuarvin/p/14109825.html
Copyright © 2011-2022 走看看