zoukankan      html  css  js  c++  java
  • SpringCloud

    1. 直接使用

    RestTemplate restTemplate = new RestTemplate();
    String result = restTemplate.getForObject("http://localhost:8761/order", String.class);

    2.使用 LoadBalancerClient 获取服务地址

        @Autowired
        LoadBalancerClient loadBalancerClient;
    
        @GetMapping
        public String getOrder()
        {
            RestTemplate template = new RestTemplate();
    
            ServiceInstance instance = loadBalancerClient.choose("ORDER");
            String url = instance.getHost();
            int port = instance.getPort();
    
            String result = template.getForObject(url + ":" + port + "/order", String.class);
            return result;
        }

    3. 使用 @LoadBalanced 注解

        1.RestTemplate 注解为bean, 并加上 @LoadBalanced
        @Bean
        @LoadBalanced
        RestTemplate restTemplate()
        {
            return new RestTemplate();
        }
    
    2. 注入 @Autowired RestTemplate template;
    3. 使用 @GetMapping
    public String getOrder() { String result = template.getForObject("http://ORDER/order", String.class); return result; }

       ORDER 为注册的服务名
    
    
  • 相关阅读:
    Lightoj 1023
    Tju 4119. HDFS
    Lightoj 1020
    Lightoj 1019
    小奇挖矿 2(4和7)
    [AHOI2012]树屋阶梯
    漂亮字串
    Prison 监狱
    2-XOR-SAT
    牛宫
  • 原文地址:https://www.cnblogs.com/appleat/p/9989802.html
Copyright © 2011-2022 走看看