Feign is a Java to HTTP client binder inspired by Retrofit, JAXRS-2.0, and WebSocket. Feign's first goal was reducing the complexity of binding Denominator uniformly to HTTP APIs regardless of ReSTfulness.
interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
@RequestLine("POST /repos/{owner}/{repo}/issues")
void createIssue(Issue issue, @Param("owner") String owner, @Param("repo") String repo);
}
一、注册中心使用
(1)依赖引用
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
(2)启动类增加注解@EnableFeignClients
@SpringBootApplication
@EnableFeignClients
public class UserApplicaiton
(3)编写openfeign接口
@FeignClient(name="order")
public interface OrderFeignInterface {
@RequestMapping("/order/query")
String query();
}
(4)controller使用
@Resource
private OrderFeignInterface orderFeignInterface;
@RequestMapping("/feignquery")
public String feginQuery(){
return this.orderFeignInterface.query();
}
二、使用
(1)编写openfeign接口
@FeignClient(name = "jsk",url = "http://127.0.0.1:8081")
public interface SkFeignClient {
@RequestMapping("/order/query")
String query();
}
模板如下
@FeignClient(name = "${feign.name}", url = "${feign.url}")
public interface StoreClient {
//..
}
(2)controller使用
@Resource
private SkFeignClient skFeignClient;
@RequestMapping("/openfeign-url")
public String openfeignUrl(){
return this.skFeignClient.query();
}
三、OpenFeign默认值修改:Spring Cloud OpenFeign
(1)、日志级别修改
其他方式:配置
logging:
level:
com.sk.cloud.user.feign.OrderFeignInterface: DEBUG
feign:
client:
config:
order:#定义服务配置,全局服务配置改成default
loggerLevel: full
(2)、http请求方式修改:httpurlconnection --> httpclient
2.1、依赖引入
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
2.2、增加配置
feign:
httpclient:
enabled: true
max-connections: 200
max-connections-per-route: 50
四、OpenFeign架构