Feign是一个很好用的REST通讯包,十分强大。
1、MyFeignClient
package com.neohope.springcloud.test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import feign.hystrix.FallbackFactory; import org.springframework.stereotype.Component; @SpringBootApplication @EnableEurekaClient @EnableFeignClients(clients = {MyFeignClient.IServiceClient.class}) @RestController public class MyFeignClient { @Value("${spring.application.name}") private String appName; @RequestMapping("/appName") public String appName() { return appName; } public static void main(String[] args) { new SpringApplicationBuilder(MyFeignClient.class).web(true).run(args); } @RequestMapping("/") public String hello() { return "It works"; } @Autowired private IServiceClient aClient; @RequestMapping("/invokeC") public String invokeC() { return aClient.invokeA(); } @RequestMapping("/invokeC/{clientName}") String invokeC(@PathVariable("clientName") String clientName) { if(aClient==null) { return "aClient is null"; } else { //return aClient.invokeA(clientName); return aClient.invokeA(); } } //primary = false @FeignClient(name="netflix-feign-server", configuration = MyFeignClientConfiguration.class, fallbackFactory=HystrixClientFallbackFactory.class) public interface IServiceClient { @RequestMapping(method = RequestMethod.GET, value = "/invokeA") String invokeA(); //@RequestMapping(method = RequestMethod.GET, value = "/invokeA/{clientName}", consumes = "application/json") //String invokeA(@PathVariable("clientName") String clientName); } @Component public static class HystrixClientFallbackFactory implements FallbackFactory<IServiceClient>{ @Override public IServiceClient create(final Throwable cause) { return new IServiceClient() { @Override public String invokeA() { return "invokeA failed: "+cause.getMessage(); } //@Override //public String invokeA(String clientName) //{ // return "invokeA with clientName "+clientName+" failed: "+cause.getMessage(); //} }; } } }
2、MyFeignClientConfiguration
package com.neohope.springcloud.test; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import feign.Logger; import feign.auth.BasicAuthRequestInterceptor; @Configuration public class MyFeignClientConfiguration { @Bean public BasicAuthRequestInterceptor basicAuthRequestInterceptor() { return new BasicAuthRequestInterceptor("user", "hystrix"); } /* @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } */ }
3、pom.xml
<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>org.springframework.cloud.stream.app</groupId> <artifactId>app-starters-build</artifactId> <version>1.1.3.M1</version> </parent> <groupId>com.neohope.springcloud.test</groupId> <artifactId>netflix-feign-client</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>netflix-feign-client</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.3.0.M1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.1.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
4、application.properties
spring.application.name=feign-client server.port=7002 security.user.name=user security.user.password=hystrix eureka.client.serviceUrl.defaultZone=http://user:hystrix@localhost:7000/eureka/ eureka.client.preferSameZoneEureka=true eureka.client.healthcheck.enabled=true eureka.instance.metadataMap.zone = zone1 eureka.instance.statusPageUrlPath=${management.context-path}/info eureka.instance.healthCheckUrlPath=${management.context-path}/health eureka.instance.lease-renewal-interval-in-seconds=5 eureka.instance.lease-expiration-duration-in-seconds=10 feign.hystrix.enabled=true #feign.compression.request.enabled=true #feign.compression.response.enabled=true #feign.compression.request.enabled=true #feign.compression.request.mime-types=text/xml,application/xml,application/json #feign.compression.request.min-request-size=2048 #NONE,BASIC,HEADERS,FULL logging.level.com.neohope.springcloud.test.MyFeignClient.IServiceClient=BASIC