Declarative REST Client: Feign

Feign is a declarative web service client. It makes writing web service clients easier.

如上是Spring Cloud文档中对于Feign的定义,结合之前的两篇博文,在这里我们就可以吧Feign简单的理解为用户(前端)可以直接接触到的REST接口提供者。在Feign中,我们可以方便的访问和使用意已经在Erueka服务器中注册过的服务了。

1、建立maven工程,配置pom.xml文件

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.5.2.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  7. <properties>
  8. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  9. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  10. <java.version>1.8</java.version>
  11. </properties>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-starter-feign</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.cloud</groupId>
  19. <artifactId>spring-cloud-starter-eureka</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-test</artifactId>
  24. <scope>test</scope>
  25. </dependency>
  26. </dependencies>
  27. <dependencyManagement>
  28. <dependencies>
  29. <dependency>
  30. <groupId>org.springframework.cloud</groupId>
  31. <artifactId>spring-cloud-dependencies</artifactId>
  32. <version>Camden.SR6</version>
  33. <type>pom</type>
  34. <scope>import</scope>
  35. </dependency>
  36. </dependencies>
  37. </dependencyManagement>
  38. <build>
  39. <plugins>
  40. <plugin>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-maven-plugin</artifactId>
  43. </plugin>
  44. </plugins>
  45. </build>

2、建立包及启动类
  1. @Configuration
  2. @ComponentScan
  3. @EnableAutoConfiguration
  4. @EnableEurekaClient
  5. @EnableFeignClients
  6. @SpringBootApplication
  7. public class FeignApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(FeignApplication.class, args);
  10. }
  11. }

3、建立接口类,用来调用之前文章中CLIENT-SERVICE1服务的方法hello()
  1. @FeignClient("CLIENT-SERVICE1")
  2. public interface IHello {
  3. @RequestMapping(value = "/hello",method = RequestMethod.GET)
  4. String getHello();
  5. }
其中@FeignClient中指定需要调用的微服务的名称,@RequestMapping中指定访问微服务响应接口的路径,如之前微服务的hello方法是通过"/hello"路径访问,那么这里需要配置一致

4、新建Controller类,为前端提供REST接口
  1. @RestController
  2. public class HelloController {
  3. @Autowired
  4. private IHello iHello;
  5. @RequestMapping(value = "gethello",method = RequestMethod.GET)
  6. public String getHello(){
  7. return iHello.getHello();
  8. }
  9. }

5、配置Feign的配置文件,指定Erureka服务器注册地址和访问端口application.yml
  1. server:
  2. port: 8081
  3. eureka:
  4. client:
  5. serviceUrl:
  6. defaultZone: http://localhost:1000/eureka/

 成功!

Spring Cloud探路(三)REST 客户端Feign的更多相关文章

  1. spring cloud 声明式rest客户端feign调用远程http服务

    在Spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.Feign就是Spring Cloud提供的一种声明式R ...

  2. Spring Cloud(三):服务提供与调用 Eureka【Finchley 版】

    Spring Cloud(三):服务提供与调用 Eureka[Finchley 版]  发表于 2018-04-15 |  更新于 2018-05-07 |  上一篇文章我们介绍了 Eureka 服务 ...

  3. Spring Cloud第四篇 | 客户端负载均衡Ribbon

    ​ 本文是Spring Cloud专栏的第四篇文章,了解前三篇文章内容有助于更好的理解本文: ​Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...

  4. Spring Cloud中,如何解决Feign/Ribbon第一次请求失败的问题?

    Spring Cloud中,如何解决Feign/Ribbon第一次请求失败的问题? Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题,要如何解 ...

  5. Spring Cloud(三):Web服务客户端之Feign

    前文介绍了实现客户端负载均衡的Ribbon,但直接使用Ribbon的API来实现服务间的调用相对较为繁琐,服务间的调用能否像本地接口调用一样便捷.透明,更符合编程习惯呢?Feign就是用来干这事的. ...

  6. spring cloud学习(三)使用Ribbon实现客户端负载均衡

    使用Ribbon实现客户端的负载均衡 * 个人博客空间 : https://zggdczfr.cn/ * Ribbon Spring Cloud Netflix Ribbon 是一个客户端负载均衡的组 ...

  7. 一起来学Spring Cloud | 第三章:服务消费者 (负载均衡Ribbon)

    一.负载均衡的简介: 负载均衡是高可用架构的一个关键组件,主要用来提高性能和可用性,通过负载均衡将流量分发到多个服务器,多服务器能够消除单个服务器的故障,减轻单个服务器的访问压力. 1.服务端负载均衡 ...

  8. spring cloud 系列第4篇 —— feign 声明式服务调用 (F版本)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.feign 简介 在上一个用例中,我们使用ribbon+restTem ...

  9. Spring Cloud 声明式服务调用 Feign

    一.简介 在上一篇中,我们介绍注册中心Eureka,但是没有服务注册和服务调用,服务注册和服务调用本来应该在上一章就应该给出例子的,但是我觉得还是和Feign一起讲比较好,因为在实际项目中,都是使用声 ...

随机推荐

  1. 2019-1-17-一段能让-VisualStudio-炸掉的代码

    title author date CreateTime categories 一段能让 VisualStudio 炸掉的代码 lindexi 2019-01-17 09:55:29 +0800 20 ...

  2. SQL SERVER 2008 R2 插入数据非常慢

    表是5字段int类型,第一个字段是主健,自增字段 表结构: id int  Uncheckedbillno bigint  Uncheckedopid int  Checkedbillopid int ...

  3. 【风马一族_php】数组函数

    原文来自:http://www.cnblogs.com/sows/p/6045699.html (博客园的)风马一族 侵犯版本,后果自负  2016-11-09 15:56:26 数组 函数 php- ...

  4. 如何用maven tycho构建自己的Eclipse RCP应用

    在你写了一个Eclipse插件之后,也许你就会想如何把它变成一个P2的项目或者是一个Java App让大家可以安装到自己的Eclipse上,dangdangdang~~ 这是你就可以利用maven-t ...

  5. 大数据ETL详解

    ETL是BI项目最重要的一个环节,通常情况下ETL会花掉整个项目的1/3的时间,ETL设计的好坏直接关接到BI项目的成败.ETL也是一个长期的过程,只有不断的发现问题并解决问题,才能使ETL运行效率更 ...

  6. html Servlet web.xml(转)

    在浏览器输入:http://127.0.0.1:8080/test/test.html点击提交按钮,Tomcat后台输出:control: aaa's value is : bbb页面显示结果:pag ...

  7. blogbeta1

    //html <!DOCTYPE html> blog 身高:170 体重:230 座右铭 再给我吃一口 关于我 微信 微博 标签 SM SP 重金求爹 2019/11/16 本人找爹,带 ...

  8. Directx11 教程(2) 基本的windows应用程序框架(2)

    原文:Directx11 教程(2) 基本的windows应用程序框架(2)      在本教程中,我们把前面一个教程的代码,进行封装.把初始化函数,Run函数,窗口回调函数,ShutdownWind ...

  9. 一条SQL完成跨数据库实例Join查询

    背景 随着业务复杂程度的提高.数据规模的增长,越来越多的公司选择对其在线业务数据库进行垂直或水平拆分,甚至选择不同的数据库类型以满足其业务需求.原本在同一数据库实例里就能实现的SQL查询,现在需要跨多 ...

  10. 国外最受欢迎的十大社交APP网站

    国外最受欢迎的十大社交APP网站 2016-11-01 09:34悠悠国外网     有哪些好的国外社交软件你知道吗,想使用国外流行的社交应用来体验不一样的社交么,想和外国友人交朋友么.本期悠悠国外网 ...