• 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 为注册的服务名
    
    
  • 相关阅读:
    Debate
    图形算法
    OpenGL Notes
    How to Write an Ethics Paper
    Thesis
    addWindowListener -> WindowAdapter -> windowClosing
    Thesis
    Bootcamp: An error occurred while partitioning the disk
    What Is XML Schema
    What Is XML
  • 原文地址:https://www.cnblogs.com/appleat/p/9989802.html
Copyright © 2020-2023  润新知