轉(zhuǎn)載自
http://blog.csdn.net/forezp/article/details/69808079
上一篇文章,講述了通過restTemplate+ribbon去消費服務(wù),這篇文章主要講述通過feign去消費服務(wù)。
一、Feign簡介
Feign是一個聲明式的web服務(wù)客戶端,它使得寫web服務(wù)變得更簡單。使用Feign,只需要創(chuàng)建一個接口并注解。它具有可插拔的注解特性,包括Feign 注解和JAX-RS注解。Feign同時支持可插拔的編碼器和解碼器。spring cloud對Spring mvc添加了支持,同時在spring web中次用相同的HttpMessageConverter。當(dāng)我們使用feign的時候,spring cloud 整和了Ribbon和eureka去提供負(fù)載均衡。
簡而言之:
- feign采用的是接口加注解
- feign 整合了ribbon
二、準(zhǔn)備工作
繼續(xù)用上一節(jié)的工程: 啟動eureka-server,端口為8761; 啟動service-hi 兩次,端口分別為8762 、8773.
三、創(chuàng)建一個feign的服務(wù)
創(chuàng)建一個spring-boot工程,取名為:serice-feign,它的pom文件為:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.forezp</groupId>
<artifactId>service-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-feign</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
向服務(wù)注冊中心注冊它自己,這時service-feign既是服務(wù)提供者,也是服務(wù)消費者,配置文件application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8765
spring:
application:
name: service-feign
在程序的入口類,需要通過注解@EnableFeignClients來開啟feign:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceFeignApplication.class, args);
}
}
定義一個feign接口類,通過@ FeignClient(“服務(wù)名”),來指定調(diào)用哪個服務(wù):
@FeignClient(value = "service-hi")
public interface SchedualServiceHi {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}
在web層的controllrt:
@RestController
public class HiController {
@Autowired
SchedualServiceHi schedualServiceHi;
@RequestMapping(value = "/hi",method = RequestMethod.GET)
public String sayHi(@RequestParam String name){
return schedualServiceHi.sayHiFromClientOne(name);
}
}
訪問http://localhost:8765/hi?name=forezp,瀏覽器交替顯示:
hi forezp,i am from port:8762
hi forezp,i am from port:8763
四、更改feign的配置
在聲明feignclient的時候,不僅要指定服務(wù)名,同時需要制定服務(wù)配置類:
@FeignClient(name = "stores", configuration = FooConfiguration.class)
public interface StoreClient {
//..
}
重寫配置,需要加@Configuration注解,并重寫下面的兩個bean,栗子:
@Configuration
public class FooConfiguration {
@Bean
public Contract feignContractg() {
return new feign.Contract.Default();
}
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("user", "password");
}
}