1. 程式人生 > >SpringCloud框架服務消費者-Feign

SpringCloud框架服務消費者-Feign

1.Feign介紹

Feign是一個宣告式的web service客戶端,它使得編寫web service客戶端更為容
易。建立介面,為介面添加註解,即可使用Feign。Feign可以使用Feign註解或者
JAX-RS註解,還支援熱插拔的編碼器和解碼器。Spring Cloud為Feign添加了
Spring MVC的註解支援,並整合了Ribbon和Eureka來為使用Feign時提供負載均衡。

譯自:http://projects.spring.io/spring-cloud/docs/1.0.3/springcloud.
html#spring-cloud-feign

2.示例

建立一個Maven專案,並在pom.xml新增如下內容:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht
tp://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://m
aven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</
modelVersion
>
<artifactId>microservice-consumer-movie-feign</artifactId> <packaging>jar</packaging> <parent> <groupId>com.itmuch.cloud</groupId> <artifactId>spring-cloud-microservice-study</artifactId> <version>0.0.1-SNAPSHOT</version>
</parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>
  • 啟動類:MovieFeignApplication.java
/**
* 使用@EnableFeignClients開啟Feign
* @author eacdy
*/
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class MovieFeignApplication {
	public static void main(String[] args) {
		SpringApplication.run(MovieFeignApplication.class, args);
	}
}
  • 實體類:User.java
public class User {
	private Long id;
	private String username;
	private Integer age;
	...
	// getters and setters
}
  • Feign測試類:UserFeignClient.java
/**
* 使用@FeignClient("microservice-provider-user")註解繫結microserv
ice-provider-user服務,還可以使用url引數指定一個URL。
* @author eacdy
*/
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
	@RequestMapping("/{id}")
	public User findByIdFeign(@RequestParam("id") Long id);
	}
}
  • Feign的測試類:FeignController.java
@RestController
public class FeignController {
	@Autowired
	private UserFeignClient userFeignClient;
	@GetMapping("feign/{id}")
		public User findByIdFeign(@PathVariable Long id) {
		User user = this.userFeignClient.findByIdFeign(id);
		return user;
	}
}
  • application.yml
server:
	port: 8020
spring:
	application:
		name: microservice-consumer-movie-feign
eureka:
	client:
		serviceUrl:
			defaultZone: http://discovery:8761/eureka/
instance:
	preferIpAddress: true
ribbon:
	eureka:
		enabled: true 
# 預設為true。如果設定為false,Ribbon將不會從Eureka中獲得服務列表,
#而是使用靜態配置的服務列表。
#靜態服務列表可使用:<client>.ribbon.listOfServers來指定。
#參考:http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html#spring-cloud-ribbonwithout-eureka
### 參考:https://spring.io/guides/gs/client-side-load-balancing/

Feign程式碼地址:
http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microserviceconsumer-
movie-feign https://github.com/eacdy/spring-cloudstudy/
tree/master/microservice-consumer-movie-feign