史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)

不念不忘少年蓝@ 2022-06-09 05:13 230阅读 0赞

category_icon.jpg 分类:

springcloud(25) arrow_triangle_20_down.jpg

版权声明:本文为博主原创文章,欢迎转载,转载请注明作者、原文超链接 ,博主地址:http://blog.csdn.net/forezp。

目录(?)[+]

转载请标明出处:
http://blog.csdn.net/forezp/article/details/69808079
本文出自方志朋的博客

上一篇文章,讲述了如何通过RestTemplate+Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务。

一、Feign简介

Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

简而言之:

  • Feign 采用的是基于接口的注解
  • Feign 整合了ribbon

二、准备工作

继续用上一节的工程, 启动eureka-server,端口为8761; 启动service-hi 两次,端口分别为8762 、8773.

三、创建一个feign的服务

新建一个spring-boot工程,取名为serice-feign,在它的pom文件引入Feign的起步依赖spring-cloud-starter-feign、Eureka的起步依赖spring-cloud-starter-eureka、Web的起步依赖spring-boot-starter-web,代码如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.forezp</groupId>
  5. <artifactId>service-feign</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>service-feign</name>
  9. <description>Demo project for Spring Boot</description>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>1.5.2.RELEASE</version>
  14. <relativePath/> <!-- lookup parent from repository -->
  15. </parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-starter-eureka</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.cloud</groupId>
  28. <artifactId>spring-cloud-starter-feign</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-web</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-test</artifactId>
  37. <scope>test</scope>
  38. </dependency>
  39. </dependencies>
  40. <dependencyManagement>
  41. <dependencies>
  42. <dependency>
  43. <groupId>org.springframework.cloud</groupId>
  44. <artifactId>spring-cloud-dependencies</artifactId>
  45. <version>Dalston.RC1</version>
  46. <type>pom</type>
  47. <scope>import</scope>
  48. </dependency>
  49. </dependencies>
  50. </dependencyManagement>
  51. <build>
  52. <plugins>
  53. <plugin>
  54. <groupId>org.springframework.boot</groupId>
  55. <artifactId>spring-boot-maven-plugin</artifactId>
  56. </plugin>
  57. </plugins>
  58. </build>
  59. <repositories>
  60. <repository>
  61. <id>spring-milestones</id>
  62. <name>Spring Milestones</name>
  63. <url>https://repo.spring.io/milestone</url>
  64. <snapshots>
  65. <enabled>false</enabled>
  66. </snapshots>
  67. </repository>
  68. </repositories>
  69. </project>
  70. 1
  71. 2
  72. 3
  73. 4
  74. 5
  75. 6
  76. 7
  77. 8
  78. 9
  79. 10
  80. 11
  81. 12
  82. 13
  83. 14
  84. 15
  85. 16
  86. 17
  87. 18
  88. 19
  89. 20
  90. 21
  91. 22
  92. 23
  93. 24
  94. 25
  95. 26
  96. 27
  97. 28
  98. 29
  99. 30
  100. 31
  101. 32
  102. 33
  103. 34
  104. 35
  105. 36
  106. 37
  107. 38
  108. 39
  109. 40
  110. 41
  111. 42
  112. 43
  113. 44
  114. 45
  115. 46
  116. 47
  117. 48
  118. 49
  119. 50
  120. 51
  121. 52
  122. 53
  123. 54
  124. 55
  125. 56
  126. 57
  127. 58
  128. 59
  129. 60
  130. 61
  131. 62
  132. 63
  133. 64
  134. 65
  135. 66
  136. 67
  137. 68
  138. 69
  139. 70
  140. 71
  141. 72
  142. 73
  143. 74
  144. 75
  145. 76
  146. 77
  147. 78
  148. 79
  149. 80
  150. 81
  151. 82
  152. 1
  153. 2
  154. 3
  155. 4
  156. 5
  157. 6
  158. 7
  159. 8
  160. 9
  161. 10
  162. 11
  163. 12
  164. 13
  165. 14
  166. 15
  167. 16
  168. 17
  169. 18
  170. 19
  171. 20
  172. 21
  173. 22
  174. 23
  175. 24
  176. 25
  177. 26
  178. 27
  179. 28
  180. 29
  181. 30
  182. 31
  183. 32
  184. 33
  185. 34
  186. 35
  187. 36
  188. 37
  189. 38
  190. 39
  191. 40
  192. 41
  193. 42
  194. 43
  195. 44
  196. 45
  197. 46
  198. 47
  199. 48
  200. 49
  201. 50
  202. 51
  203. 52
  204. 53
  205. 54
  206. 55
  207. 56
  208. 57
  209. 58
  210. 59
  211. 60
  212. 61
  213. 62
  214. 63
  215. 64
  216. 65
  217. 66
  218. 67
  219. 68
  220. 69
  221. 70
  222. 71
  223. 72
  224. 73
  225. 74
  226. 75
  227. 76
  228. 77
  229. 78
  230. 79
  231. 80
  232. 81
  233. 82

在工程的配置文件application.yml文件,指定程序名为service-feign,端口号为8765,服务注册地址为http://localhost:8761/eureka/ ,代码如下:

  1. eureka:
  2. client:
  3. serviceUrl:
  4. defaultZone: http://localhost:8761/eureka/
  5. server:
  6. port: 8765
  7. spring:
  8. application:
  9. name: service-feign
  10. 1
  11. 2
  12. 3
  13. 4
  14. 5
  15. 6
  16. 7
  17. 8
  18. 9
  19. 10
  20. 1
  21. 2
  22. 3
  23. 4
  24. 5
  25. 6
  26. 7
  27. 8
  28. 9
  29. 10

在程序的启动类ServiceFeignApplication ,加上@EnableFeignClients注解开启Feign的功能:

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. @EnableFeignClients
  4. public class ServiceFeignApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(ServiceFeignApplication.class, args);
  7. }
  8. }
  9. 1
  10. 2
  11. 3
  12. 4
  13. 5
  14. 6
  15. 7
  16. 8
  17. 9
  18. 10
  19. 11
  20. 1
  21. 2
  22. 3
  23. 4
  24. 5
  25. 6
  26. 7
  27. 8
  28. 9
  29. 10
  30. 11

定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了service-hi服务的“/hi”接口,代码如下:

  1. /** * Created by fangzhipeng on 2017/4/6. */
  2. @FeignClient(value = "service-hi")
  3. public interface SchedualServiceHi {
  4. @RequestMapping(value = "/hi",method = RequestMethod.GET)
  5. String sayHiFromClientOne(@RequestParam(value = "name") String name);
  6. }
  7. 1
  8. 2
  9. 3
  10. 4
  11. 5
  12. 6
  13. 7
  14. 8
  15. 9
  16. 10
  17. 11
  18. 1
  19. 2
  20. 3
  21. 4
  22. 5
  23. 6
  24. 7
  25. 8
  26. 9
  27. 10
  28. 11

在Web层的controller层,对外暴露一个”/hi”的API接口,通过上面定义的Feign客户端SchedualServiceHi 来消费服务。代码如下:

  1. @RestController
  2. public class HiController {
  3. @Autowired
  4. SchedualServiceHi schedualServiceHi;
  5. @RequestMapping(value = "/hi",method = RequestMethod.GET)
  6. public String sayHi(@RequestParam String name){
  7. return schedualServiceHi.sayHiFromClientOne(name);
  8. }
  9. }
  10. 1
  11. 2
  12. 3
  13. 4
  14. 5
  15. 6
  16. 7
  17. 8
  18. 9
  19. 10
  20. 11
  21. 12
  22. 1
  23. 2
  24. 3
  25. 4
  26. 5
  27. 6
  28. 7
  29. 8
  30. 9
  31. 10
  32. 11
  33. 12

启动程序,多次访问http://localhost:8765/hi?name=forezp,浏览器交替显示:

hi forezp,i am from port:8762

hi forezp,i am from port:8763

Feign源码解析:http://blog.csdn.net/forezp/article/details/73480304

本文源码下载:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter3

五、参考资料

spring-cloud-feign

发表评论

表情:
评论列表 (有 0 条评论,230人围观)

还没有评论,来说两句吧...

相关阅读