springcloud项目例子:链接:https://pan.baidu.com/s/1O1PKrdvrq5c8sQUb7dQ5Pg 密码:ynir
作用:
基于Netflix Feign整合了Ribbon和Hystrix,Spring Cloud Feign对RestTemplate进行了进一步的封装,简化了我们的封装操作
一:创建Feign应用
1.pom:依赖主要是spring-cloud-starter-eureka和spring-cloud-starter-feign
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <spring-cloud.version>Dalston.SR3</spring-cloud.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> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2.入口函数:@EnableFeignClients注解表示开启Spring Cloud Feign的支持功能
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class FeignConsumerApplication { public static void main(String[] args) { SpringApplication.run(FeignConsumerApplication.class, args); } }
3.声明 服务
1)创建service层,使用注解@FeignClient,之后Controller直接调用他就能访问生产者提供的服务
@FeignClient("hello-service") public interface HelloService { @RequestMapping("/hello") String hello(); }
//生产者提供的服务代码
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
return "hello";
}
2)Controller层调用服务
@RestController public class FeignConsumerController { @Autowired HelloService helloService; @RequestMapping("/hello") public String hello() { return helloService.hello(); } }
4.属性配置
spring.application.name=feign-consumer server.port=2005 eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
5.测试
运行服务,生产者,Feign消费者调用消费者接口,发现生产者的服务能给调用成功,Feign服务配置成功!
二:Feign的配置
spring.application.name=feign-consumer server.port=2005 eureka.client.service-url.defaultZone=http://localhost:1111/eureka/ ###ribbon测试####### # 设置连接超时时间 ribbon.ConnectTimeout=600 # 设置读取超时时间 ribbon.ReadTimeout=6000 # 对所有操作请求都进行重试 ribbon.OkToRetryOnAllOperations=true # 切换实例的重试次数 ribbon.MaxAutoRetriesNextServer=2 # 对当前实例的重试次数 ribbon.MaxAutoRetries=1 # 设置针对hello-service服务的连接超时时间 hello-service.ribbon.ConnectTimeout=600 # 设置针对hello-service服务的读取超时时间 hello-service.ribbon.ReadTimeout=6000 # 设置针对hello-service服务所有操作请求都进行重试 hello-service.ribbon.OkToRetryOnAllOperations=true # 设置针对hello-service服务切换实例的重试次数 hello-service.ribbon.MaxAutoRetriesNextServer=2 # 设置针对hello-service服务的当前实例的重试次数 hello-service.ribbon.MaxAutoRetries=1 ####Hystrix配置####### # 设置熔断超时时间 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000 # 关闭Hystrix功能(不要和上面的配置一起使用) feign.hystrix.enabled=false # 关闭熔断功能 hystrix.command.default.execution.timeout.enabled=false # 设置熔断超时时间 hystrix.command.hello.execution.isolation.thread.timeoutInMilliseconds=10000 # 关闭熔断功能 hystrix.command.hello.execution.timeout.enabled=false ####Feign其他配置####### ####Spring Cloud Feign支持对请求和响应进行GZIP压缩,以提高通信效率 # 配置请求GZIP压缩 feign.compression.request.enabled=true # 配置响应GZIP压缩 feign.compression.response.enabled=true # 配置压缩支持的MIME TYPE feign.compression.request.mime-types=text/xml,application/xml,application/json # 配置压缩数据大小的下限 feign.compression.request.min-request-size=2048 # 开启日志 格式为logging.level.+Feign客户端路径,Feign为每一个FeignClient都提供了一个feign.Logger实例 logging.level.org.sang.HelloService=debug