SpringCloud 中,默认是使用HTTP进行微服务间通信,其中最常用的有两种实现形式。主要是RestTemplate和Feign。在SpringWeb里面,已经原生支持了 RestTemplate,只不过我们一般使用的是把请求URL直接写死,而不是通过服务名的形式来调用,但是在微服务架构中,因为注册中心的存在,我们的负载均衡可以不需要使用第三方软件或者硬件实现了,所以,我们最佳的方式是经过服务名访问,请求到那个实例,由 Ribbon 的负载均衡策略来替我们决定。这时候RestTemplate渐渐不适合这种通过域名访问的形式了,下面介绍下从RestTemplate到Feign的演变过程。便于我们理解为啥现在服务中用的都是Feign,而不是RestTemplate。
@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 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 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();
}
}
RestTemplate配置类,加上 @LoadBalanced,这样在服务调用的时候,就可以在 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;
}
Feign 是 Netflix 开发的声明式、模板化的 HTTP客户端, Feign 可以帮助我们更快捷、优雅地调用 HTTP API
org.springframework.cloud
spring-cloud-starter-openfeign
@SpringBootApplication
@EnableFeignClients
public class SpringCloudRibbonClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudRibbonClientApplication.class, args);
}
}
@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);
}
@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 map = new HashMap<>(16);
map.put("requestMsg", requestMsg);
Map response = ribbonServerClient.PostRibbonServer(map);
log.info("response={}", response);
return response;
}
}
在实际项目中,Feign调用的入参,返回结果可以自己封装成DTO,不需要用Map。可以结合自己项目中的实际使用。
https://www.jianshu.com/p/b64451435126