转载请标明出处:
http://blog.csdn.net/forezp/a...
本文出自方志朋的博客html
上一篇文章,讲述了经过restTemplate+ribbon去消费服务,这篇文章主要讲述经过feign去消费服务。java
Feign是一个声明式的web服务客户端,它使得写web服务变得更简单。使用Feign,只须要建立一个接口并注解。它具备可插拔的注解特性,包括Feign 注解和JAX-RS注解。Feign同时支持可插拔的编码器和解码器。Spring cloud对Spring mvc添加了支持,同时在spring web中次用相同的HttpMessageConverter。当咱们使用feign的时候,spring cloud 整和了Ribbon和eureka去提供负载均衡。git
简而言之:github
feign采用的是接口加注解web
feign 整合了ribbonspring
继续用上一节的工程: 启动eureka-server,端口为8761; 启动service-hi 两次,端口分别为8762 、8773.apache
建立一个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-ribbon既是服务提供者,也是服务消费者,配置文件application.ymlmvc
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8765 spring: application: name: service-feign
在程序的入口类,须要经过注解@EnableFeignClients来开启feign:app
@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 { @Autowired SchedualServiceHi schedualServiceHi; @RequestMapping(value = "/hi",method = RequestMethod.GET) public String sayHi(@RequestParam String name){ return schedualServiceHi.sayHiFromClientOne(name); } }
访问http://localhost:8765/hi?name...浏览器交替显示:
hi forezp,i am from port:8762
hi forezp,i am from port:8763
在声明feignclient的时候,不只要指定服务名,同时须要制定服务配置类:
@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"); } }
本文源码下载:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter3