javascript
SpringCloud教程 | 第三篇: 服务消费者(Feign)
上一篇文章,講述了通過restTemplate+ribbon去消費服務,這篇文章主要講述通過feign去消費服務。
一、Feign簡介
Feign是一個聲明式的web服務客戶端,它使得寫web服務變得更簡單。使用Feign,只需要創建一個接口并注解。它具有可插拔的注解特性,包括Feign 注解和JAX-RS注解。Feign同時支持可插拔的編碼器和解碼器。spring cloud對Spring mvc添加了支持,同時在spring web中次用相同的HttpMessageConverter。當我們使用feign的時候,spring cloud 整和了Ribbon和eureka去提供負載均衡。
簡而言之:
- feign采用的是接口加注解
- feign 整合了ribbon
二、準備工作
繼續用上一節的工程: 啟動eureka-server,端口為8761; 啟動service-hi 兩次,端口分別為8762 、8773.
三、創建一個feign的服務
創建一個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>向服務注冊中心注冊它自己,這時service-feign既是服務提供者,也是服務消費者,配置文件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(“服務名”),來指定調用哪個服務:
/*** Created by fangzhipeng on 2017/4/6.*/ @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 {@AutowiredSchedualServiceHi 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的時候,不僅要指定服務名,同時需要制定服務配置類:
@FeignClient(name = "stores", configuration = FooConfiguration.class) public interface StoreClient {//.. }重寫配置,需要加@Configuration注解,并重寫下面的兩個bean,栗子:
@Configuration public class FooConfiguration {@Beanpublic Contract feignContractg() {return new feign.Contract.Default();}@Beanpublic BasicAuthRequestInterceptor basicAuthRequestInterceptor() {return new BasicAuthRequestInterceptor("user", "password");} }總結
以上是生活随笔為你收集整理的SpringCloud教程 | 第三篇: 服务消费者(Feign)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringCloud教程 | 第二篇:
- 下一篇: Docker三个基本概念镜像(Image