Spring Cloud实战(四)-Spring Cloud Netflix Feign
2016-12-28 18:52
786 查看
概要
什么是Spring Cloud Netflix Feign?怎么用Feign?
什么是Spring Cloud Netflix Feign?
Feign:Declarative REST clients.Feign:是一个声明式的REST客户端.
在之前的例子中我们使用的有DiscoveryClient,LoadBalancerClient,如他们一样FeignClient也是调用Eureka Server中服务用的,不过它提供了声明式的REST风格的调用,使编程更加简单.它是在运行时Runtime实现接口的实现,所以pom可以指定运行时依赖.
怎么用Feign?
百说不如一run,构造一个例子来实现Eureka Server 如之前一样,正常启动即可
Eureka-Client 修改bootstrap.xml,变得简单一点,只有noun服务
spring: application: name: noun server: port: 8030 eureka: client: service-url: defaultZone: http://127.0.0.1:8010/eureka/
Sentence-Client添加NounClient接口
@FeignClient("noun") public interface NounClient { @RequestMapping(value = "/",method = RequestMethod.GET) public String getWord(); }
Sentence-Client添加pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
Sentence-Client在Application添加@EnableFeignClients
Sentence-Client在SentenceController中使用NounClient
@RestController public class SentenceController { @Autowired private NounClient nounClient; @RequestMapping("/sentence") public @ResponseBody String getSentence() { return "<h3>造句:</h3><br/>" + buildSentence() + "<br/><br/>" + buildSentence() + "<br/><br/>" + buildSentence() + "<br/><br/>" + buildSentence() + "<br/><br/>" + buildSentence() + "<br/><br/>" ; } public String buildSentence() { String sentence = "There was a problem assembling the sentence!"; try{ sentence = nounClient.getWord(); } catch ( Exception e ) { System.out.println(e); } return sentence; } }
现在去http://127.0.0.1:8080/sentence检查下是否调用服务成功吧
特别感谢 kennyk65
Spring Cloud 中文用户组 31777218
Spring-Cloud-Config 官方文档-中文译本 (本人有参与,哈哈)
Spring Cloud Netflix 官网文档-中文译本
本文实例github地址 mmb-feign
原文地址:https://segmentfault.com/a/1190000006188802
相关文章推荐
- Spring Cloud实战(五)-Spring Cloud Netflix Hystrix
- (Spring Cloud微服务实战-书中之坑)spring cloud feign同时使用继承特性和断路器
- Spring Cloud实战(七)-Spring Cloud Netflix Zuul
- Spring Cloud实战(六)-Spring Cloud Netflix Bus
- springcloud微服务实战:Eureka+Zuul+Feign/Ribbon+Hystrix Turbine+SpringConfig+sleuth+zipkin
- Spring Cloud(Netflix) Feign: 以Dubbo暴露服务的方式使用Feign
- Spring Cloud Netflix 教程(Feign+Ribbon+Hystrix)
- SpringCloud微服务实战之Feign
- Spring Cloud实战(三)-Spring Cloud Netflix Ribbon
- Spring Cloud实战(五)-Spring Cloud Netflix Hystrix
- SpringCloudNetflix-Feign/Hystrix Setting
- springcloud实战之6 服务消费者(Feign)
- Spring Cloud实战(六)-Spring Cloud Netflix Bus
- Spring Cloud实战(二)-Spring Cloud Eureka
- Spring Cloud实战(一)-Spring Cloud Config Server
- spring-cloud-feign 使用@RequetParam报错QueryMap parameter must be a Map: class java.lang.String
- Spring Cloud Netflix Eureka: 多网卡环境下Eureka服务注册IP选择问题
- Spring Cloud Netflix
- SpringCloud调研系列2.2:服务发现-Feign
- Spring Cloud Netflix中文文档翻译笔记