在使用 RestTemplate 实现 Rest API 调用的时候,是经过拼接字符串的方式构造 URL,向具体服务实例发起 Http 请求。在定义 RestTemplate 的时候,还能够增长 @LoadBalanced
注解,在调用服务接口的时候,原来 host 部分是经过手动拼接ip和端口的,如今直接用 服务名 来写请求路径。在真正调用的时候,Spring Cloud 会将请求拦截下来,而后经过负载均衡器(如 Ribbon)选出节点,并替换服务名部分为具体的ip和端口,从而实现基于服务名的负载均衡调用,相关见 Spring Cloud 服务消费(Ribbon)java
以上的方式能够实现负载均衡调用,可是因为每一个调用都要拼接 URL,传递的参数也须要放在 URL 中,这样的开发体验是很很差的,比较低效且难以维护。可使用 Feign 实现声明式 REST 调用git
Feign 是 Netflix 的一套的声明式服务调用客户端。它使得编写Web服务客户端变得更加简单。咱们只须要经过建立接口并用注解来配置它便可。它具有可插拔的注解支持,包括Feign注解和JAX-RS注解。支持可插拔编码器和解码器。Spring Cloud 对 Feign 进行了加强,扩展了对Spring MVC 注解的支持,并整合了 Ribbon 和 Eureka 来提供均衡负载的 HTTP 客户端实现。github
咱们利用以前的例子(见 Spring Cloud 服务消费(Ribbon)),eureka-server
做为服务注册中心、product-service
做为服务提供者,order-service-ribbon
是服务消费者。复制 order-service-ribbon
为 order-service-feign
,在 order-service-feign
上去作一些更改spring
添加 Feign 的依赖app
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
修改启动类,添加 @EnableFeignClients
注解负载均衡
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class OrderServiceFeignApplication { public static void main(String[] args) { SpringApplication.run(OrderServiceFeignApplication.class, args); } }
建立 Feign 接口,添加 @FeignClient
注解编码
@FeignClient("product-service") public interface ProductFeignClient { @GetMapping("/product/{id}") Product getProduct(@PathVariable Long id); }
调用 Feign 接口code
@RestController @Log4j2 public class ProductController { @Autowired private ProductFeignClient productFeignClient; @GetMapping("/product/{id}") public Product getProduct(@PathVariable Long id) { return productFeignClient.getProduct(id); } }
启动 eureka-server、product-service:807一、product-service:807二、order-service-feign,访问 http://localhost:8081/product/1 能够查看结果server
示例代码:demoxml