@GetMapping("/RibbonServer/1") public Map getRibbonServer1() { // 新建对象 RestTemplate restTemplate = new RestTemplate(); //请求目标地址 String requestMsg = "方式一 GET 请求 RibbonServer"; Map response = restTemplate.getForObject("http://localhost:9999/RibbonServer/RibbonTest?requestMsg=" + requestMsg, Map.class); log.info("response={}", response); return response; } @PostMapping("/RibbonServer/1") public Map postRibbonServer1() { // 新建对象 RestTemplate restTemplate = new RestTemplate(); //请求目标地址 String requestMsg = "方式一 POST 请求 RibbonServer"; Map<String, Object> requestParam = new HashMap<>(16); requestParam.put("requestMsg", requestMsg); Map response = restTemplate.postForObject("http://localhost:9999/RibbonServer/RibbonTest", requestParam, Map.class); log.info("response={}", response); return response; } 复制代码
直接使用 RestTemplate , Url写死
@Autowired private LoadBalancerClient loadBalancerClient; @GetMapping("/RibbonServer/2") public Map getRibbonServer2() { // 获取IP地址 ServiceInstance choose = loadBalancerClient.choose("RIBBON-SERVER"); String requestMsg = "方式二 GET 请求 RibbonServer"; String url = String.format("http://%s:%s", choose.getHost(), choose.getPort() + "/RibbonServer/RibbonTest?requestMsg=" + requestMsg); RestTemplate restTemplate = new RestTemplate(); Map response = restTemplate.getForObject(url, Map.class); log.info("response={}", response); return response; } @PostMapping("/RibbonServer/2") public Map postRibbonServer2() { // 获取IP地址 ServiceInstance choose = loadBalancerClient.choose("RIBBON-SERVER"); // 组装URL String url = String.format("http://%s:%s", choose.getHost(), choose.getPort() + "/RibbonServer/RibbonTest"); RestTemplate restTemplate = new RestTemplate(); String requestMsg = "方式二 POST 请求 RibbonServer"; Map<String, Object> requestParam = new HashMap<>(16); requestParam.put("requestMsg", requestMsg); Map response = restTemplate.postForObject(url, requestParam, Map.class); log.info("response={}", response); return response; } 复制代码
利用 LoadBalancerClient 通过应用名获取 url,然后再使用 RestTemplate 请求
import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; @Component public class RestTemplateConfig { @Bean @LoadBalanced //添加该注解,可以直接通过服务名找到对应的IP地址 public RestTemplate restTemplate() { return new RestTemplate(); } } 复制代码
@Autowired private RestTemplate restTemplate; @GetMapping("/RibbonServer/3") public Map getRibbonServer3() { String requestMsg = "方式三 GET 请求 RibbonServer"; Map response = restTemplate.getForObject("http://RIBBON-SERVER/RibbonServer/RibbonTest?requestMsg=" + requestMsg, Map.class); log.info("response={}", response); return response; } @PostMapping("/RibbonServer/3") public Map postRibbonServer3() { String requestMsg = "方式三 POST 请求 RibbonServer"; Map response = restTemplate.postForObject("http://RIBBON-SERVER/RibbonServer/RibbonTest", getRequestParam(requestMsg), Map.class); log.info("response={}", response); return response; } 复制代码
利用 @LoadBalanced,可在 RestTemplate 里面直接使用应用名字
1.导入 OpenFeign 的包(SpringCloud2以后,Feign改名为OpenFeign)
<!--可以不导入ribbon,因为feign里面依赖了ribbon--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> 复制代码
2.启动类添加 @EnableFeignClients
@SpringBootApplication @EnableFeignClients public class SpringCloudRibbonClientApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudRibbonClientApplication.class, args); } } 复制代码
3.写一个接口,用来调用指定的微服务(可以由接口提供方写好SDK,我们直接调用)
@FeignClient(name = "ribbon-server") //调用的服务名 public interface RibbonServerClient { /** * 测试请求 RibbonServer * @Method: GET * @author show * @date 16:26 2019/6/9 * @param requestMsg 接口请求参数 * @return java.util.Map 接口返回值,建议用对象接收 */ @GetMapping("/RibbonServer/RibbonTest") Map getRibbonServer(@RequestParam("requestMsg") String requestMsg); /** * 测试请求 RibbonServer * @Method: POST * @author show * @date 16:26 2019/6/9 * @param requestMsg 接口请求参数 * @return java.util.Map 接口返回值,建议用对象接收 */ @PostMapping("/RibbonServer/RibbonTest") Map PostRibbonServer(@RequestBody Map requestMsg); } 复制代码
4.利用Spring的依赖注入,直接调用该接口方法,即可调用到 ribbon-server 服务的接口
@RestController @RequestMapping("/Feign") @Slf4j public class FeignController { @Autowired private RibbonServerClient ribbonServerClient; /** * Feign 访问微服务测试 * @author show * @date 16:42 2019/6/9 * @return java.util.Map */ @GetMapping("/RibbonServer") public Map getRibbonServer() { String requestMsg = "Feign GET 请求 RibbonServer"; Map response = ribbonServerClient.getRibbonServer(requestMsg); log.info("response={}", response); return response; } /** * Feign 访问微服务测试 * @author show * @date 16:42 2019/6/9 * @return java.util.Map */ @PostMapping("/RibbonServer") public Map postRibbonServer1() { String requestMsg = "Feign Post 请求 RibbonServer"; Map<String, Object> map = new HashMap<>(16); map.put("requestMsg", requestMsg); Map response = ribbonServerClient.PostRibbonServer(map); log.info("response={}", response); return response; } } 复制代码