SpringCloud之Feign的简介及基础使用

一、Feign简介

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的使用更加便捷。

二、Feign入门使用----为服务消费者整合Feign

1、创建Eureka注册服务项目

具体项目结构和资源请参照我的这篇博客:Spring Cloude 之Ribbon的基本应用 中的pringcloud-deepeureka项目

2、创建User数据提供项目

具体项目结构和资源请参照我的这篇博客:https://blog.csdn.net/qq_34195507/article/details/100601290 中的springcloud-user 项目

3、新创建一个 Feign项目 springcloud-feign

3.1、pom.xml依赖

  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
            
        
    


3.2、创建一个实体User类
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;
  }

}
3.3、新创建一个接口 UserFeignClient :并添加@FeignClient注解。
@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);
}

解释:@FeignClient注解中的springcloud-user是一个任意客户端名称,参照创建Ribbon负载均衡器,由于使用了Eureka,所以Ribbon会把springcloud-user解析成Eureka Server服务注册表内的服务。当然,如果不想使用Eureka,可使用service.ribbon.listOfServers属性配置服务器列表。
3.4、创建Controller 访问类 UserFeignController:让其调用Feign接口
@RestController
public class UserFeignController {

	@Autowired
	private UserFeignClient userFeignClient;

	@GetMapping("/user/{id}")
	public User findById(@PathVariable Long id) {
		return this.userFeignClient.findById(id);
	}
}
3.5、启动类 :添加@EnableFeignClients注解
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients //添加注解
public class FeignApplication {

	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}

	public static void main(String[] args) {
		SpringApplication.run(FeignApplication.class, args);
	}
}
3.6、application.yml配置文件
spring:
  application:
    name: springcloud-feign
server:
  port: 7901
eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://localhost:8081/eureka/
  instance:
    prefer-ip-address: true

整个项目结构

SpringCloud之Feign的简介及基础使用_第1张图片

三、依次启动项目测试

启动 springcloud-deepeureka

启动 springcloud-feign

启动 springcloud-user (启动多个测试)

1、访问 http://localhost:7901/user/1 测试

SpringCloud之Feign的简介及基础使用_第2张图片

2、查看后台打印结果
SpringCloudUserApplication 7900 打印结果:
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]

SpringCloudUserApplication 8900 打印结果:
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]

以上结果说明 Feign 一样实现了客户端的负载均衡

你可能感兴趣的:(Spring,Cloud)