当前位置 主页 > 服务器问题 > Linux/apache问题 >

    Spring boot2X Consul如何通过RestTemplate实现服务调用(3)

    栏目:Linux/apache问题 时间:2019-12-02 15:33

    服务调用

    package com.xyz.comsumer.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    @RestController
    public class RibbonHelloController {
     @Autowired
     private RestTemplate restTemplate;
     private String serviceName = "service-provider";
    
     @RequestMapping("/ribbon/hello")
     public String hello() {
      String callServiceResult = restTemplate.getForObject("http://"+serviceName+"/hello", String.class);
      return callServiceResult;
     }
    }

    配置添加

    service-provider.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule

    重新启动 comsumer

    测试地址 http://localhost:8015/ribbon/hello

    输出的结果不再是交替出现,改为随机的了

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持IIS7站长之家。