前面我们了解了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即可:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<scope>test</scope>
</dependency> </dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

  2、application:

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

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

package hello;

import hello.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.*; import java.util.List; @EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignApplication { public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
} @RestController
class ServiceInstanceRestController { @Autowired
private ConsumerService consumerService; @Autowired
private DiscoveryClient discoveryClient; @RequestMapping("/consumer/sayHi")
public String sayHi(@RequestParam(value = "name") String name, @RequestParam(value = "accessToken", required = false) String accessToken) {
return name + " say hi: " + consumerService.hello(name);
} @RequestMapping("/service-instances/{applicationName}")
public List<ServiceInstance> serviceInstancesByApplicationName(
@PathVariable String applicationName) {
return this.discoveryClient.getInstances(applicationName);
}
}

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

package hello.service;

import hello.service.impl.BackUPCallHi;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; @Component
@FeignClient(name = "a-bootiful-client", fallback = BackUPCallHi.class)
public interface ConsumerService {
@RequestMapping("/hello")
String hello(@RequestParam(value = "name") String name);
}
package hello.service.impl;

import hello.service.ConsumerService;
import org.springframework.stereotype.Component; @Component
public class BackUPCallHi implements ConsumerService { @Override
public String hello(String name) {
return "I'm feign hystrix.";
}
}

  搞定,可以看到原来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. Springboot分别使用乐观锁和分布式锁(基于redisson)完成高并发防超卖

    原文 :https://blog.csdn.net/tianyaleixiaowu/article/details/90036180 乐观锁 乐观锁就是在修改时,带上version版本号.这样如果试图 ...

  2. CMake---基础练习1

    因为卡在一个问题上,几经排除应该可能是CMakeLists.txt写的不正确,但是又生成了可执行文件,运行可执行文件报错.多方排除,应该是CMakeLists.txt加载动态库的时候,函数加载的不全. ...

  3. 【二叉搜索树】PAT-天梯赛- L2-004. 这是二叉搜索树吗?

    大致题意: 一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点,    其左子树中所有结点的键值小于该结点的键值:    其右子树中所有结点的键值大于等于该结点的键值:    其左右子树 ...

  4. 关于部署 Kafka 的一些所得

    前记 最近在做日志模块. 其中用到的日志信息传输的中间工具用的是的 Kafka,在自己的摸索中总是有一些问题的,在这里记录下来.  Kafka 环境搭建 首先是下载需要的安装软件. JDK.Zooke ...

  5. erase & remove_if 合用

    words_.erase( remove_if( words_.begin(), words_.end(), [&](const entry& e) { return (e.type ...

  6. Git----拉取远程分支,git pull,git rebase,git pull --rebase的区别

    git pull 相当于自动的 fetch 和 merge 操作,会试图自动将远程库合并入本地库,在有冲突时再要求手动合并. git rebase 可以确保生产分支commit是一个线性结构,方便ro ...

  7. java中的AIO

    AIO(异步非阻塞)AIO采用了Proactor模式,AIO与NIO的不同之处在于当AIO在进行读写操作时,不用先等通知,可直接调用相应的read/write方法,这两种方法均为异步的,对于读操作而言 ...

  8. flask 框架 转载:https://cloud.tencent.com/developer/article/1465968

    特点总结: 类名称---->数据库表名 类属性---->数据库字段 类的对象----->数据库表中的一行一行数据 3.ORM操作注意(理解) 1/因为SQLALChemy去app身上 ...

  9. 2015浙工大校赛-Problem C: 三角—— 费马大定理+勾股数

    题目 有一个直角三角形三边为 A,B,C 三个整数.已知 C 为最长边长,求一组B,C,使得B和C最接近. (题目链接) 分析 打表找规律. 或者直接一点的枚举 $C-B$ 的值.(既然枚举 B 不现 ...

  10. Helm:kubernetes应用包管理工具

    概要 Helm:kubernetes应用包管理工具 K8s部署应用的时候,应用会通过yaml描述信息调用K8s-api:Helm即是管理这些Yaml的应用包管理工具 组成 Helm包含5个部分 Hel ...