Feign是Netflix开发的声明式、模块化的HTTP客户端,其灵感来自Retrofit、JAXRS-2.0以及WebSocket。Feign可帮助我们更加便捷、优雅的调用HTTP API。
在Spring Cloud中,使用Feign非常简单——创建一个接口,并在接口上添加一些注解,代码就完成了。Feign支持多种注解,例如Feign自带的注解或者JAX-RS注解等。
Spring Cloud对feign进行了增强,使Feign支持了Spring MVC注解,并整合了Ribbon和Eureka,从而让Feign的使用更加便捷。
4.0.0
cn.com.qihang.cloud
springcloud-feign
0.0.1-SNAPSHOT
jar
springcloud-feign
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Edgware.RELEASE
pom
import
org.springframework.boot
spring-boot-maven-plugin
public class User {
private Long id;
private String username;
private String name;
private Short age;
private BigDecimal balance;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return this.age;
}
public void setAge(Short age) {
this.age = age;
}
public BigDecimal getBalance() {
return this.balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
@FeignClient(name = "springcloud-user")
public interface UserFeignClient {
/**
* 使用 Feign方式的两个坑
*
* 1、不支持注解 @GetMapping("/simple/{id}")
*
* 2、@PathVariable()必须设置value @PathVariable("id")
*
* 以上两点可以对照Ribbon
*
*/
@RequestMapping(value = "/simple/{id}",method = RequestMethod.GET)
public User findById(@PathVariable("id") Long id);
}
@RestController
public class UserFeignController {
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("/user/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
}
}
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients //添加注解
public class FeignApplication {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
spring:
application:
name: springcloud-feign
server:
port: 7901
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://localhost:8081/eureka/
instance:
prefer-ip-address: true
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?
2019-09-09 16:28:22.333 TRACE 13316 --- [io-7900-exec-10] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [1]
2019-09-09 16:28:22.334 TRACE 13316 --- [io-7900-exec-10] o.h.type.descriptor.sql.BasicExtractor : extracted value ([age2_0_0_] : [SMALLINT]) - [20]
2019-09-09 16:28:22.335 TRACE 13316 --- [io-7900-exec-10] o.h.type.descriptor.sql.BasicExtractor : extracted value ([balance3_0_0_] : [NUMERIC]) - [100.00]
2019-09-09 16:28:22.335 TRACE 13316 --- [io-7900-exec-10] o.h.type.descriptor.sql.BasicExtractor : extracted value ([name4_0_0_] : [VARCHAR]) - [张三]
2019-09-09 16:28:22.335 TRACE 13316 --- [io-7900-exec-10] o.h.type.descriptor.sql.BasicExtractor : extracted value ([username5_0_0_] : [VARCHAR]) - [user1]
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?
2019-09-09 16:28:31.437 TRACE 13316 --- [nio-7900-exec-9] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [2]
2019-09-09 16:28:31.437 TRACE 13316 --- [nio-7900-exec-9] o.h.type.descriptor.sql.BasicExtractor : extracted value ([age2_0_0_] : [SMALLINT]) - [20]
2019-09-09 16:28:31.437 TRACE 13316 --- [nio-7900-exec-9] o.h.type.descriptor.sql.BasicExtractor : extracted value ([balance3_0_0_] : [NUMERIC]) - [100.00]
2019-09-09 16:28:31.437 TRACE 13316 --- [nio-7900-exec-9] o.h.type.descriptor.sql.BasicExtractor : extracted value ([name4_0_0_] : [VARCHAR]) - [李四]
2019-09-09 16:28:31.437 TRACE 13316 --- [nio-7900-exec-9] o.h.type.descriptor.sql.BasicExtractor : extracted value ([username5_0_0_] : [VARCHAR]) - [user2]
2019-09-09 16:35:38.549 INFO 10420 --- [nio-8900-exec-3] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2019-09-09 16:35:38.550 INFO 10420 --- [nio-8900-exec-3] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2019-09-09 16:35:38.584 INFO 10420 --- [nio-8900-exec-3] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 34 ms
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?
2019-09-09 16:35:38.649 TRACE 10420 --- [nio-8900-exec-3] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [1]
2019-09-09 16:35:38.658 TRACE 10420 --- [nio-8900-exec-3] o.h.type.descriptor.sql.BasicExtractor : extracted value ([age2_0_0_] : [SMALLINT]) - [20]
2019-09-09 16:35:38.659 TRACE 10420 --- [nio-8900-exec-3] o.h.type.descriptor.sql.BasicExtractor : extracted value ([balance3_0_0_] : [NUMERIC]) - [100.00]
2019-09-09 16:35:38.659 TRACE 10420 --- [nio-8900-exec-3] o.h.type.descriptor.sql.BasicExtractor : extracted value ([name4_0_0_] : [VARCHAR]) - [张三]
2019-09-09 16:35:38.660 TRACE 10420 --- [nio-8900-exec-3] o.h.type.descriptor.sql.BasicExtractor : extracted value ([username5_0_0_] : [VARCHAR]) - [user1]
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?
2019-09-09 16:35:42.423 TRACE 10420 --- [nio-8900-exec-7] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [1]
2019-09-09 16:35:42.423 TRACE 10420 --- [nio-8900-exec-7] o.h.type.descriptor.sql.BasicExtractor : extracted value ([age2_0_0_] : [SMALLINT]) - [20]
2019-09-09 16:35:42.423 TRACE 10420 --- [nio-8900-exec-7] o.h.type.descriptor.sql.BasicExtractor : extracted value ([balance3_0_0_] : [NUMERIC]) - [100.00]
2019-09-09 16:35:42.424 TRACE 10420 --- [nio-8900-exec-7] o.h.type.descriptor.sql.BasicExtractor : extracted value ([name4_0_0_] : [VARCHAR]) - [张三]
2019-09-09 16:35:42.424 TRACE 10420 --- [nio-8900-exec-7] o.h.type.descriptor.sql.BasicExtractor : extracted value ([username5_0_0_] : [VARCHAR]) - [user1]
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?
2019-09-09 16:35:46.849 TRACE 10420 --- [nio-8900-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [1]
2019-09-09 16:35:46.850 TRACE 10420 --- [nio-8900-exec-2] o.h.type.descriptor.sql.BasicExtractor : extracted value ([age2_0_0_] : [SMALLINT]) - [20]
2019-09-09 16:35:46.850 TRACE 10420 --- [nio-8900-exec-2] o.h.type.descriptor.sql.BasicExtractor : extracted value ([balance3_0_0_] : [NUMERIC]) - [100.00]
2019-09-09 16:35:46.851 TRACE 10420 --- [nio-8900-exec-2] o.h.type.descriptor.sql.BasicExtractor : extracted value ([name4_0_0_] : [VARCHAR]) - [张三]
2019-09-09 16:35:46.851 TRACE 10420 --- [nio-8900-exec-2] o.h.type.descriptor.sql.BasicExtractor : extracted value ([username5_0_0_] : [VARCHAR]) - [user1]