Greenwich.SR2版本的Spring Cloud Feign实例
前面我们了解了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实例的更多相关文章
- Greenwich.SR2版本的Spring Cloud Zuul实例
网关作为对外服务,在微服务架构中是一个很重要的组件,主要体现在动态路由和接入鉴权这两个功能上.现在我们通过Spring Cloud Zuul来实现对之前a-feign-client(参见Greenwi ...
- Greenwich.SR2版本的Spring Cloud Hystrix实例
之前我们在eureka(参见Greenwich.SR2版本的Spring Cloud Eureka实例)中,服务消费方a-beautiful-client调用服务提供方a-bootiful-clien ...
- Greenwich.SR2版本的Spring Cloud Ribbon实例
上次我们了解了eureka(参见Greenwich.SR2版本的Spring Cloud Eureka实例),里面的服务消费方(服务实例a-beautiful-client)我们其实已经用到了ribb ...
- Greenwich.SR2版本的Spring Cloud Eureka实例
作为微服务架构中最为核心和基础的服务治理,注册中心提供了微服务实例的自动化注册与发现.而作为一个服务注册中心,eureka的作用与传统的zk.etcd的作用是一样的,同样也支持高可用(集群).不同之处 ...
- Greenwich.SR2版本的Spring Cloud Zipkin实例
调用链跟踪是微服务架构中的基础能力,Spring Cloud Zipkin+Sleuth为我们提供了该能力.首先我们先建立Zipkin服务端,它需要集成Eureka,用于发现服务提供方和消费方,进行数 ...
- Greenwich.SR2版本的Spring Cloud Config+BUS实例
Spring Cloud Config统一的配置中心同注册中心Eureka一样,也分服务端和客户端.服务端用来保存配置信息,客户端用来读取.它的优势是基于Git仓库,支持多环境.多分支配置.动态刷新. ...
- spring cloud feign 坑
feign是啥? 很多人可能对于feign 不是很熟悉,可以看一下其他网友的实例分享:spring cloud feign简介 如果觉得上面这个比较难的话,还有一个简单入门的:spring cplou ...
- 0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例
这里的feign依然是原来的feign,只不过将注册中心由eureka换成了nacos.服务提供方参见0.9.0.RELEASE版本的spring cloud alibaba nacos实例,消费方跟 ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例
既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...
随机推荐
- 如何在CentOS上搭建gitlab服务器
步骤 1. 打开HTTP和SSH访问 1.1 安装 sudo yum install -y curl policycoreutils-python openssh-server 1.2 开启SSH 这 ...
- 基于GitLab CI搭建Golang自动构建环境
基于GitLab CI搭建Golang自动构建环境 Golang发布遇到的问题 对于golang的发布,之前一直没有一套规范的发布流程,来看看之前发布流程: 方案一 开发者本地环境需要将环境变量文件改 ...
- java容器一:Collection概述
Collection概览 java容器有两类,第一类是Collection,存储的是对象的集合:第二类是Map,存储的是键值对(两个对象以及它们之间的对应关系)的集合 Collection接口下面有三 ...
- MySQL远程连接错误解决
远程连接服务器的MySQL数据库,错误代码是1130,是由于无法给远程连接的用户权限的问题 解决方法: 本机登陆mysql后,将mysql数据库中的user表中的host项,从localhost改为% ...
- UML之九种图
UML说是九种图吧!其实是众说纷纭,不管有几种图,我们只要能够很好的运用这几张图就好,主要有用例图.类图.对象图.状态图.活动图.序列图.协作图.构件图和部署图,至于包图是否属于这九种图,我也理不清楚 ...
- Android 测试-Robolectric,mockito,esspresso
代码参考:https://github.com/googlesamples/android-testing 解释参考: https://www.jianshu.com/p/5732b4afd12f 官 ...
- matlab(5) : 求得θ值后用模型来预测 / 计算模型的精度
求得θ值后用模型来预测 / 计算模型的精度 ex2.m部分程序 %% ============== Part 4: Predict and Accuracies ==============% Af ...
- C#格式化信息,格式化数字、格式化日期
一.格式化方法: 1.ToString()实例方法 使用当前文化: varname.ToString("C4"); 使用特定文化: varname.ToString("C ...
- cvte2018春招前端开发实习面试分享
编程题问题描述: 返回整数数组中出现次数第n多的数字(返回值可能有多个) 最近在找实习,面试二面最后出了一道这样的编程题,当时有思路但语法有错误,而且很紧张,最后没有运行出来,导致凉凉,回来重新思考了 ...
- C语言Ⅰ博客作业06
这个作业属于哪个课程 C语言程序设计Ⅰ 这个作业要求在哪里 熟练掌握多分支结构,字符型数据类型和逻辑运算符 我在这个课程的目标是 https://www.cnblogs.com/tongyingjun ...