springCloud 使用feign服务。
- springboot启动带上@EnableFeignCleints (系统自动扫描@FeignCleint的类并注册为bean)
- 为请求服务的接口带上@FeignCleint(当请求到方法时,被ReflectiveFeign拦截,通过jdk代理之后返回代理对象)
- 进行RequestTemplate请求外部服务使用它的client对象,默认请求对象是HttpURLConnection方式,支持httpclient和OKhttpclient 需要对pom.xml进行处理(这里的默认请求居然是post的有点坑,加上@RequestParam参数可以自动识别为get请求)
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-httpclient</artifactId>
<version>RELEASE</version>
</dependency>
或者
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-okhttp</artifactId>
<version>RELEASE</version>
</dependency>
4.feign使用负载均衡策略(默认使用LoadBalancerFeignClient实现),当请求封装完毕之后,通过executeWithLoadBalancer方法进行负载均衡。