Feign是Netflix开发的声明式、模板化的HTTP客户端,其灵感来自Retrofit、JAXRS-2.0以及WebSocket。Feign可帮助我们更加便捷、优雅地调用HTTPAPI。
在Spring Cloud中,使用Feign非常简单—创建一个接口,并在接口上添加一些注解,代码就完成了。Feign支持多种注解,例如Feign自带的注解或者JAX-RS注解等。
Spring Cloud对Feign进行了增强,使Feign支持了Spring MVC注解,并整合了Ribbon和Eureka,从而让Feign的使用更加方便。
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
已经过期,推荐使用
package com.cc.cloud.member.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@FeignClient("cloud-service-order")
public interface OrderFeign {
@RequestMapping("/order/orders")
List<String> getAllOrderList();
}
修改Controller代码,让其调用Feign接口。
package com.cc.cloud.member.controller;
import com.cc.cloud.member.feign.OrderFeign;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/member")
public class MemberController {
private OrderFeign orderFeign;
@Autowired
public void setOrderFeign(OrderFeign orderFeign) {
this.orderFeign = orderFeign;
}
@RequestMapping("/orders")
@ResponseStatus(HttpStatus.OK)
public List<String> getOrderList() {
return orderFeign.getAllOrderList();
}
@RequestMapping("/members")
@ResponseStatus(HttpStatus.OK)
public List<String> getMemberList() {
List<String> memberList = Lists.newArrayList();
memberList.add("member 1");
memberList.add("member 2");
memberList.add("member 3");
return memberList;
}
}
package com.cc.cloud.order.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@FeignClient("cloud-service-member")
public interface MemberFeign {
@RequestMapping("/member/members")
List<String> getAllMemberList();
}
修改Controller代码,让其调用Feign接口。
package com.cc.cloud.order.controller;
import com.cc.cloud.order.feign.MemberFeign;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/order")
public class OrderController {
private MemberFeign memberFeign;
@Autowired
public void setMemberFeign(MemberFeign memberFeign) {
this.memberFeign = memberFeign;
}
@RequestMapping("/members")
@ResponseStatus(HttpStatus.OK)
public List<String> getMemberList() {
return memberFeign.getAllMemberList();
}
@GetMapping("/orders")
@ResponseStatus(HttpStatus.OK)
public List<String> getOrders() {
List<String> orders = Lists.newArrayList();
orders.add("order 1");
orders.add("order 2");
return orders;
}
}
@FeignClient 注解中的cloud-service-member是一个任意的客户端名称,用于创建Ribbon负载均衡器。在本例中,由于使用了Eureka,所以Ribbon会把cloud-service-member解析成Eureka Server服务注册表中的服务。当然,如果不想使用Eureka,可使用service.ribbon.listofServers属性配置服务器列表.
还可使用url属性指定请求的URL(URL可以是完整的URL或者主机名),例如
@FeignClient(name =“cloud-service-member”,url=“http://localhost:8762”)。
package com.cc.cloud.member;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class MemberApp {
public static void main(String[] args) {
SpringApplication.run(MemberApp.class, args);
}
}
package com.cc.cloud.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class OrderApp {
public static void main(String[] args) {
SpringApplication.run(OrderApp.class, args);
}
}
分别访问: http://localhost:8762/member/orders 和 http://localhost:8765/order/members
SpringCloud与Docker微服务架构实战-完整版.pdf
【SpringCloud Greenwich版本】第三章:服务消费者(Feign)
https://gitee.com/cckevincyh/spring-cloud-demo/tree/fegin