Feign是什么?
Feign是一个声明式WebService客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
如何使用Feign?
首先在生产者微服务和消费者微服务中添加Feign的相关依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-feignartifactId>
<dependency>
然后在生产者微服务中新建接口,接口中添加@FeignClient注解
@FeignClient(name = "${proposal.service.name}", path = "/personInsured", configuration = FeignClientsConfiguration.class)
public interface
AHPersonInsuredRestfulService {
/**
* 团单 页面手动添加被保险人(最新)
* @param policy
* @return
*/
@RequestMapping(value ="/v1/savePersonInsured", method = RequestMethod.POST)
Map<String, Object> savePersonInsured(@RequestBody Policy policy);
最后在主启动类上添加@EnableFeignClients注解
@EnableFeignClients({"com.ccic.ap26.ah.proposal.restful"})
消费者微服务直接调用AHPersonInsuredRestfulService 中的方法
通过Feign直接找到服务接口,由于在进行服务调用的时候融合了Ribbon技术,所以也支持负载均衡作用。