前面我们了解了Spring Cloud Ribbon和Hystrix,在使用上它们基本上会成队出现,那么是不是可以把它们组合起来使用?而且我们发现,在服务消费方a-beautiful-client里通过REST调用服务提供方时,会有很多RestTemplate的代码,这些重复代码能否简化掉呢?答案是肯定的,Spring Cloud为我们提供了Feign,就是整合了Ribbon和Hystrix,并且提供了一种声明式的Web服务客户端定义方式,让我们只需要定义好REST接口即可,服务消费方无需再写实现了。

  我们这次新增一个a-feign-client吧,把它跟a-beautiful-client(参见Greenwich.SR2版本的Spring Cloud Eureka实例)来做一番比较。三板斧祭出:

  1、pom里我们去掉了ribbon和hystrix,只需引入openfeign即可:

  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"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5.  
  6. <groupId>com.example</groupId>
  7. <artifactId>feign</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10.  
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.1.6.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17.  
  18. <properties>
  19. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  20. <java.version>1.8</java.version>
  21. </properties>
  22.  
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-starter-openfeign</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-web</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-test</artifactId>
  39. <scope>test</scope>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.cloud</groupId>
  43. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  44. <scope>test</scope>
  45. </dependency>
  46.  
  47. </dependencies>
  48.  
  49. <dependencyManagement>
  50. <dependencies>
  51. <dependency>
  52. <groupId>org.springframework.cloud</groupId>
  53. <artifactId>spring-cloud-dependencies</artifactId>
  54. <version>Greenwich.SR2</version>
  55. <type>pom</type>
  56. <scope>import</scope>
  57. </dependency>
  58. </dependencies>
  59. </dependencyManagement>
  60.  
  61. <build>
  62. <plugins>
  63. <plugin>
  64. <groupId>org.springframework.boot</groupId>
  65. <artifactId>spring-boot-maven-plugin</artifactId>
  66. </plugin>
  67. </plugins>
  68. </build>
  69.  
  70. </project>

  2、application:

  1. #本机端口
  2. server.port=8764
  3.  
  4. #本机服务名
    spring.application.name=a-feign-client
  1. #服务提供方实例地址
  2. app.service.url=http://A-BOOTIFUL-CLIENT/
  3.  
  4. #注册中心地址
  5. eureka.client.service-url.defaultZone=http://localhost:8888/eureka/
  6.  
  7. #开启熔断
  8. feign.hystrix.enabled=true
  9.  
  10. #负载均衡配置
  11. a-bootiful-client.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule

  主类我们用@EnableFeignClients代替了@EnableCircuitBreaker和@LoadBalance:

  1. package hello;
  2.  
  3. import hello.service.ConsumerService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.SpringApplication;
  6. import org.springframework.boot.autoconfigure.SpringBootApplication;
  7. import org.springframework.cloud.client.ServiceInstance;
  8. import org.springframework.cloud.client.discovery.DiscoveryClient;
  9. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  10. import org.springframework.cloud.openfeign.EnableFeignClients;
  11. import org.springframework.web.bind.annotation.*;
  12.  
  13. import java.util.List;
  14.  
  15. @EnableFeignClients
  16. @EnableDiscoveryClient
  17. @SpringBootApplication
  18. public class FeignApplication {
  19.  
  20. public static void main(String[] args) {
  21. SpringApplication.run(FeignApplication.class, args);
  22. }
  23. }
  24.  
  25. @RestController
  26. class ServiceInstanceRestController {
  27.  
  28. @Autowired
  29. private ConsumerService consumerService;
  30.  
  31. @Autowired
  32. private DiscoveryClient discoveryClient;
  33.  
  34. @RequestMapping("/consumer/sayHi")
  35. public String sayHi(@RequestParam(value = "name") String name, @RequestParam(value = "accessToken", required = false) String accessToken) {
  36. return name + " say hi: " + consumerService.hello(name);
  37. }
  38.  
  39. @RequestMapping("/service-instances/{applicationName}")
  40. public List<ServiceInstance> serviceInstancesByApplicationName(
  41. @PathVariable String applicationName) {
  42. return this.discoveryClient.getInstances(applicationName);
  43. }
  44. }

  3、我们改写一下接口,再新增一个熔断的服务降级类:

  1. package hello.service;
  2.  
  3. import hello.service.impl.BackUPCallHi;
  4. import org.springframework.cloud.openfeign.FeignClient;
  5. import org.springframework.stereotype.Component;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestParam;
  8.  
  9. @Component
  10. @FeignClient(name = "a-bootiful-client", fallback = BackUPCallHi.class)
  11. public interface ConsumerService {
  12. @RequestMapping("/hello")
  13. String hello(@RequestParam(value = "name") String name);
  14. }
  1. package hello.service.impl;
  2.  
  3. import hello.service.ConsumerService;
  4. import org.springframework.stereotype.Component;
  5.  
  6. @Component
  7. public class BackUPCallHi implements ConsumerService {
  8.  
  9. @Override
  10. public String hello(String name) {
  11. return "I'm feign hystrix.";
  12. }
  13. }

  搞定,可以看到原来a-beautiful-client能干的事情,a-feign-client也做到了:

  负载均衡:

  熔断:

  上面我们看到url后面多了一个accessToken的参数,这个其实是给网关鉴权用的,详见Greenwich.SR2版本的Spring Cloud Zuul实例

Greenwich.SR2版本的Spring Cloud Feign实例的更多相关文章

  1. Greenwich.SR2版本的Spring Cloud Zuul实例

    网关作为对外服务,在微服务架构中是一个很重要的组件,主要体现在动态路由和接入鉴权这两个功能上.现在我们通过Spring Cloud Zuul来实现对之前a-feign-client(参见Greenwi ...

  2. Greenwich.SR2版本的Spring Cloud Hystrix实例

    之前我们在eureka(参见Greenwich.SR2版本的Spring Cloud Eureka实例)中,服务消费方a-beautiful-client调用服务提供方a-bootiful-clien ...

  3. Greenwich.SR2版本的Spring Cloud Ribbon实例

    上次我们了解了eureka(参见Greenwich.SR2版本的Spring Cloud Eureka实例),里面的服务消费方(服务实例a-beautiful-client)我们其实已经用到了ribb ...

  4. Greenwich.SR2版本的Spring Cloud Eureka实例

    作为微服务架构中最为核心和基础的服务治理,注册中心提供了微服务实例的自动化注册与发现.而作为一个服务注册中心,eureka的作用与传统的zk.etcd的作用是一样的,同样也支持高可用(集群).不同之处 ...

  5. Greenwich.SR2版本的Spring Cloud Zipkin实例

    调用链跟踪是微服务架构中的基础能力,Spring Cloud Zipkin+Sleuth为我们提供了该能力.首先我们先建立Zipkin服务端,它需要集成Eureka,用于发现服务提供方和消费方,进行数 ...

  6. Greenwich.SR2版本的Spring Cloud Config+BUS实例

    Spring Cloud Config统一的配置中心同注册中心Eureka一样,也分服务端和客户端.服务端用来保存配置信息,客户端用来读取.它的优势是基于Git仓库,支持多环境.多分支配置.动态刷新. ...

  7. spring cloud feign 坑

    feign是啥? 很多人可能对于feign 不是很熟悉,可以看一下其他网友的实例分享:spring cloud feign简介 如果觉得上面这个比较难的话,还有一个简单入门的:spring cplou ...

  8. 0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例

    这里的feign依然是原来的feign,只不过将注册中心由eureka换成了nacos.服务提供方参见0.9.0.RELEASE版本的spring cloud alibaba nacos实例,消费方跟 ...

  9. 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例

    既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...

随机推荐

  1. Castle.Windsor依赖注入的高级应用与生存周期

    1. 使用代码方式进行组件注册[依赖服务类] using System; using System.Collections.Generic; using System.Linq; using Syst ...

  2. 【loj-1055-Going Together-三个棋子推箱子走到目的地--讲预判的bfs】

    light oj 1055-Going Together 题目大致意思: 简单的三个棋子,每次可以下达一个命令,robots全部按照指令进行前进:若下一步不为空地则停留在原地. 特殊考虑: 1.例如A ...

  3. python_面向对象——多继承

    1.多继承 class Shenxian: def fly(self): print('神仙会飞~') class Monkey: def eat_peach(self): print('猴子喜欢吃桃 ...

  4. 如何在vscode中用standard style 风格去验证 vue文件

    1 JavaScript Standard Style简介 本工具通过以下三种方式为你(及你的团队)节省大量时间: 无须配置. 史上最便捷的统一代码风格的方式,轻松拥有. 自动代码格式化. 只需运行 ...

  5. python 操作 MySQL 即相关问题

    导入pymysql import pymysql # 创建connect()对象 conn = pymysql.connect( host = '127.0.0.1', port = 3306, us ...

  6. Java中CAS-ABA的问题解决方案

    忻州SEO摘要 CAS即对比交换,它在保证数据原子性的前提下尽可能的减少了锁的使用,很多编程语言或者系统实现上都大量的使用了CAS.   了解CAS(Compare-And-Swap) CAS即对比交 ...

  7. 2019HDU多校第7场——构造

    题意 假设现在你在准备考试,明天的考试有 $n$ 道题目,对于分值为 $i$ 的题目至少复习 $i+1$ 小时才能做对,已知总分为$m$,求确保完成 $k$ 道题的最少时间. 分析 手动尝试一下,发现 ...

  8. Luogu P2148 [SDOI2009]E&D (sg函数 博弈)

    题目 洛谷传送门 题解 打表找sgsgsg规律. 严谨证明见:纳尔的博客 CODE #include <bits/stdc++.h> using namespace std; int sg ...

  9. 学到了林海峰,武沛齐讲的Day21-完 模块和包

    调用包,会执行包的__init__.py "IF__name__=='__main__':执行当前文件会执行" time random 开始玩高级的了.. 爽

  10. 二十七. Keepalived热备 Keepalived+LVS 、 HAProxy服务器

    1.Keepalived高可用服务器 proxy:192.168.4.5(客户端主机) web1:192.168.4.100(Web服务器,部署Keepalived高可用软件) web2:192.16 ...