Feign是一個聲明式WebService客戶端,使用Feign可以讓編寫Web Service客戶端變得更簡單,它的使用方法就是定義一個接口,而後在上面添加註解。SpringCloud對Feign進行了封裝,支持SpringMVC註解和HTTPMessageConverters,Feign能夠與Eureka和Ribbon組合使用以支持負載均衡。簡單講,只須要建立一個接口,而後在上面使用註解便可。java
參考項目microservicecloud-consumer-dept-80新建microservicecloud-consumer-dept-feign,拷貝相應的包和配置文件,去掉IRule等信息,修改pom.xml文件,添加對Feign的支持web
<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>
<parent>
<groupId>com.luo.springcloud</groupId>
<artifactId>microservicecloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservicecloud-consumer-dept-feign</artifactId>
<dependencies>
<dependency><!-- 本身定義的api -->
<groupId>com.luo.springcloud</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!-- Ribbon相關 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 修改後當即生效,熱部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
複製代碼
因爲Feign是面向接口編程,爲方便接口的互相調用,將接口和公共的方向在項目microservicecloud-api中,所以修改成:spring
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
複製代碼
@FeignClient(value = "MICROSERVICECLOUD-DEPT")
public interface DeptClientService {
@RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)
public Dept get(@PathVariable("id") long id);
@RequestMapping(value = "/dept/list",method = RequestMethod.GET)
public List<Dept> list();
@RequestMapping(value = "/dept/add", method = RequestMethod.POST)
public boolean add(Dept dept);
}
複製代碼
@RestController
public class DeptController_Consumer {
@Autowired
private DeptClientService service;
@RequestMapping(value = "/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Long id){
return this.service.get(id);
}
@RequestMapping(value = "/consumer/dept/list")
public List<Dept> list(){
return this.service.list();
}
@RequestMapping(value = "/consumer/dept/add")
public Object add(Dept dept){
return this.add(dept);
}
}
複製代碼
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.luo.springcloud"})
@ComponentScan("com.luo.springcloud")
public class DeptConsumer80_Feign_App {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer80_Feign_App.class, args);
}
}
複製代碼
啓動3個Eureka集羣,啓動三個部門微服務提供者,啓動Feign,訪問http://localhost/consumer/dept/list便可apache
Feign集成了Ribbon,利用Ribbon維護了MicroServiceCloud-Dept的服務列表信息,並經過輪詢的方式實現了客戶端的複雜均衡,與Ribbon不一樣的是,經過Feign只須要定義服務綁定接口且以聲明式法人方法,優雅而簡單的實現服務調用。編程