spring Cloud 之 Eureka、Feign、Hystrix、Zuul、Config、Bus
一、服务发现——Netflix Eureka
Eureka包含两个组件: Eureka Server和Eureka Client:
Eureka 的作用:提供服务注册和发现功能
- Eureka Server 提供服务发现的功能, 各个微服务会将自己的信息注册到Eureka Server。
- Eureka Client 即服务提供者,将其信息注册到Eureka Server上面。
- 微服务会周期性(默认30秒)地向Eureka Server 发送心跳以维持自己的注册状态,如果Eureka Server 在一定时间(默认90秒)没有接受到某个微服务实例的心跳,Eureka Server 将会注销该实例。
- 默认情况下,Eureka Server 同时也是 Client, 多个Eureka Server 实例之间可以通过复制的方法, 来实现服务注册表数据的同步。
- Eureka Client 会缓存服务注册表中的信息,所以 Eureka Client 无须每次调用微服务都要先查询Eureka Server,能有效地缓解Eureka Server的压力,而且即使所有的Eureka Server节点都宕掉,Client 依然可以根据缓存中信息找到服务提供者。
1、创建Eureka Server服务端
(1)、引入依赖 父工程pom.xml定义SpringCloud版本
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐dependencies</artifactId>
<version>Finchley.M9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
(2)、tensquare_eureka模块pom.xml引入eureka-server
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐netflix‐eureka-server</artifactId>
</dependency>
</dependencies>
(3)、添加application.yml
server:
port: 6868
eureka:
client:
register-with-eureka: false #是否将自己注册到Eureka服务中,本身就是服务无需注册
fetch-registry: false #是否从Eureka服务中获取注册信息
service-url:
defaultZone: http://127.0.0.1:${server.port}/eureka/
(4)编写启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication //SpringBoot 开启注解
@EnableEurekaServer //EurekaServer Eureka服务端
public class EurekaServer {
public static void main(String[] args) {
SpringApplication.run(EurekaServer.class);
}
}
(5)、结果:
2、创建Eureka Client 服务端
(1)、将其他微服务模块添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
(2)、修改每个微服务的application.yml,添加注册eureka服务的配置
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:6868/eureka/
(3)、修改每个服务类的启动类,添加注解
@EnableEurekaClient
(4)、结果:将每个微服务启动起来,会发现eureka的注册列表中可以看到这些微服 务了
二、服务调用——Netflix Feign 实现服务间的调用
Feign是简化Java HTTP客户端开发的工具(java-to-httpclient-binder),
它的灵感 来自于Retrofit、JAXRS-2.0和WebSocket。
Feign的初衷是降低统一绑定Denominator到 HTTP API的复杂度,不区分是否为restful。
1、在要 通信的 服务上添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2、启动类上,添加注解
@SpringBootApplication
@EnableEurekaClient //客户端标识
@EnableDiscoveryClient //发现服务
@EnableFeignClients //用Feign的方式发现服务
3、创建 com.tensquare.qa.client包,包下创建接口
@FeignClient(value = "tensquare-base")
public interface BaseClient { @GetMapping(value = "/label/{labelId}")
public Result findById(@PathVariable("labelId") String labelId);
}
@FeignClient注解用于指定从哪个服务中调用功能 ,注意 里面的名称与被调用的服务 名保持一致,并且不能包含下划线。
@RequestMapping注解用于对被调用的微服务进行地址映射。注意 @PathVariable注 解一定要指定参数名称,否则出错
4、修改tensquare_qa模块的 ProblemController
@Autowired
private BaseClient baseClient; @GetMapping(value = "/label/{labelId}")
public Result findByLabelId(@PathVariable String labelId){
Result result= baseClient.findById(labelId);
return result;
}
5、结果 http://localhost:9003/problem/label/1144250491664994304 获取数据
/label/{labelId} 是 teasquare_base 服务中的方法
原本可由 :http://localhost:9001/label/1144250491664994304 查询到
三、熔断器——Netflix Hystrix
在微服务架构中通常会有多个服务层调用,基础服务的故障可能会导致级联故障,
进而造成整个系统不可用的情况,这种现象被称为服务雪崩效应。
服务雪崩效应是一种 因“服务提供者”的不可用导致“服务消费者”的不可用,并将不可用逐渐放大的过程。
如何避免产生这种雪崩效应呢?我们可以使用Hystrix来实现熔断器。
Hystrix 能使你的系统在出现依赖服务失效的时候,通过隔离系统所依赖的服务,
防 止服务级联失败,同时提供失败回退机制,
更优雅地应对失效,并使你的系统能更快地 从异常中恢复。
Feign 本身支持Hystrix,不需要额外引入依赖。
1、修改tensquare_qa模块的application.yml ,开启hystrix
feign:
hystrix:
enabled: true
2、在com.tensquare.qa.client包下创建impl包,包下创建熔断实现类,实现自接口 LabelClient
@Component
public class BaseClientImpl implements BaseClient {
@Override
public Result findById(String labelId) {
return new Result(false, StatusCode.ERROR,"触发熔断器");
}
}
3、修改LabelClient的注解
@FeignClient(value = "tensquare-base",fallback = BaseClientImpl.class)
4、测试运行
重新启动问答微服务,测试看熔断器是否运行
四、服务网关——Netflix Zuul
Zuul作用:过滤器,动态路由;
- 验证与安全保障: 识别面向各类资源的验证要求并拒绝那些与要求不符的请求。
- 审查与监控: 在边缘位置追踪有意义数据及统计结果,从而为我们带来准确的生产状态结论。
- 动态路由: 以动态方式根据需要将请求路由至不同后端集群处。
- 压力测试: 逐渐增加指向集群的负载流量,从而计算性能水平。
- 负载分配: 为每一种负载类型分配对应容量,并弃用超出限定值的请求。
- 静态响应处理: 在边缘位置直接建立部分响应,从而避免其流入内部集群。
- 多区域弹性: 跨越AWS区域进行请求路由,旨在实现ELB使用多样化并保证边缘位置与使用者尽可能接近。
1、创建子模块tensquare_web,pom.xml引入eureka-client 和zuul的依赖
<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-netflix-zuul</artifactId>
</dependency>
2、创建application.yml
server:
port: 9012
spring:
application:
name: tensquare-web
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:6868/eureka/
instance:
prefer-ip-address: true
zuul:
routes:
tensquare-base:
path: /base/**
serviceId: tensquare-base
tensquare-user:
path: /user/**
serviceId: tensquare-user
tensquare-qa:
path: /qa/**
serviceId: tensquare-qa
3、编写启动类
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class);
}
}
4、Zuul过滤器
@Component
public class WebFilter extends ZuulFilter {
@Override
public String filterType() {
return "pre"; // 前置过滤器
} @Override
public int filterOrder() {
return 0; // 优先级为0,数字越大,优先级越低
} @Override
public boolean shouldFilter() {
return true; // 是否执行该过滤,此处为true,说明需要过滤
} @Override
public Object run() throws ZuulException {
System.out.println("zuul过滤器..."); return null;
}
}
5、结果
工作台输出:zuul过滤器...
原本路径 :http://localhost:9001/label/1144250491664994304 查询到
原本路径 改成这个 9012/base/**
访问路径:http://localhost:9012/base/label/1144250491664994304
五、分布式配置——Spring Cloud Config
在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),
也支持放在远程Git仓库 中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。
Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个 环境下的配置,
默认使用Git存储配置文件内容,也可以使用SVN存储,或者是本地文件 存储。
Config Client是Config Server的客户端,用于操作存储在Config Server中的配置内容。
微服务在启动时会请求Config Server获取配置文件的内容,请求到后再启动容器。
1、配置中心微服务
(1)、创建工程模块 配置中心微服务 tensquare_config,pom.xml引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
(2)、创建启动类ConfigServerApplication
@SpringBootApplication
@EnableConfigServer //开启配置服务
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class);
}
}
(3)、编写配置文件application.yml
server:
port: 12000
spring:
application:
name: tensquare_config
cloud:
config:
server:
git:
uri: https://github.com/MengWangwang/tensquare_dev.git
(4)、测试结果 将 tensquare_base 中的application.yml内容上传到github
(5)、测试结果 开启 tensquare_config,http://localhost:12000/base-dev.yml测试
2、配置客户端
(1)、在tensquare_base工程添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
(2)、添加bootstrap.yml ,删除application.yml
spring:
cloud:
config:
name: base
profile: dev
label: master
uri: http://127.0.0.1:12000
(3)测试: 启动工程tensquare_eureka tensquare_config tensquare_base tensquare_common,看是 否可以正常运行
测试路径:http://localhost:9001/label
六、消息总线 —— Spring Cloud Bus
在不重启微服务的情况 下更新配置如何来实现呢? 我们使用SpringCloudBus来实现配置的自动更新
1、配置服务端
(1)、修改tensquare_config工程的pom.xml,引用依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
(2)、修改application.yml ,添加配置
server:
port: 12000
spring:
application:
name: tensquare_config
cloud:
config:
server:
git:
uri: https://github.com/MengWangwang/tensquare_dev.git
rabbitmq:
host: 127.0.0.1
management: #暴露触发消息总线的地址
endpoints:
web:
exposure:
include: bus-refresh
2、配置客户端
(1)、修改tensquare_base工程 ,引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
(2)、修改 base-dev.yml 将 ip:127.0.0.1111111111 修改为 ip:127.0.0.222222222
(3)、点击测试 http://127.0.0.1:12000/actuator/bus-refresh,就会刷新配置。
ip:127.0.0.11111111111111
++++++++++++++:Bearer eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxMTQ4NTcwMjc0OTA4NzM3NTM2Iiwic3ViIjoiYWRtaW4iLCJpYXQiOjE1NjMwNzU3ODQsInJvbGVzIjoiYWRtaW4iLCJleHAiOjE1NjMwNzkzODR9.LFKjNF8F6PdbrL_EYgRtOWuAtgYW8Qw2DlWEjdXZGfk
(4)、点击测试 http://localhost:9001/label,查询。
ip:127.0.0.222222222
++++++++++++++:Bearer eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxMTQ4NTcwMjc0OTA4NzM3NTM2Iiwic3ViIjoiYWRtaW4iLCJpYXQiOjE1NjMwNzU3ODQsInJvbGVzIjoiYWRtaW4iLCJleHAiOjE1NjMwNzkzODR9.LFKjNF8F6PdbrL_EYgRtOWuAtgYW8Qw2DlWEjdXZGfk
七、github源码:https://github.com/MengWangwang/tensquare
Spring Cloud Config的GitHub:https://github.com/MengWangwang/tensquare_dev
注:本工程还用到了rabbitMq和redis,以及mongodb,所以启动相关模块时,记得开启工具。
spring Cloud 之 Eureka、Feign、Hystrix、Zuul、Config、Bus的更多相关文章
- SpringCloud微服务(基于Eureka+Feign+Hystrix+Zuul)
一.搭建注册中心 1.1.创建一个cloud-service项目 1.2:POM文件依赖 1 <?xml version="1.0" encoding="UTF-8 ...
- Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul)
Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul) 1.Eureka Eureka是Netflix的一个子模块,也是核心模块之一.Eureka是 ...
- Spring Boot,Spring Cloud,Eureka,Actuator,Spring Boot Admin,Stream,Hystrix
Spring Boot,Spring Cloud,Eureka,Actuator,Spring Boot Admin,Stream,Hystrix 一.Spring Cloud 之 Eureka. 1 ...
- spring cloud 2.x版本 Hystrix Dashboard断路器教程
前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...
- Spring Cloud(Dalston.SR5)--Feign 声明式REST客户端
Spring Cloud 对 Feign 进行了封装,集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,Spring Cloud 实现的 Feign 客户端类名为 LoadBala ...
- Spring Cloud第八篇 | Hystrix集群监控Turbine
本文是Spring Cloud专栏的第八篇文章,了解前七篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...
- 【Spring Cloud笔记】 断路器-hystrix
在微服务架构中,一个微服务的超时失败可能导致瀑布式连锁反映,Spring Cloud Netflix 的断路器Hystrix通过自主反馈,防止了这种情况发生.下面介绍简单的断路器使用方法. [step ...
- Spring Cloud 入门 之 Feign 篇(三)
原文地址:Spring Cloud 入门 之 Feign 篇(三) 博客地址:http://www.extlight.com 一.前言 在上一篇文章<Spring Cloud 入门 之 Ribb ...
- Spring Cloud Security&Eureka安全认证(Greenwich版本)
Spring Cloud Security&Eureka安全认证(Greenwich版本) 一·安全 Spring Cloud支持多种安全认证方式,比如OAuth等.而默认是可以直接添加spr ...
- spring cloud之eureka简介
最近线上的接口出了一些问题,有一些可能不是代码的问题,但是由于是测试和其他方面的同事爆出来的,所以感觉对接口的监控应该提上日程. 经过搜索发现,spring cloud的eureka就是专门做这方面工 ...
随机推荐
- Jenkins用户授予root权限
Jenkins用户授予root权限 由于需要在jenkins中执行shell脚本,但是有些目录下没有权限,下面的操作为jenkins用户授予root权限. jenkins用户加入到root组 ...
- 算法学习之二分查找算法的python实现
——参考自<算法图解> 我们假设需要查找的数组是有序的(从大到小或者从小到大),如果无序,可以在第四行后插入一句 my_list.sort() 完整代码如下 def binary_sear ...
- opencv python 图像二值化/简单阈值化/大津阈值法
pip install matplotlib 1简单的阈值化 cv2.threshold第一个参数是源图像,它应该是灰度图像. 第二个参数是用于对像素值进行分类的阈值, 第三个参数是maxVal,它表 ...
- python--线程锁,队列
#线程数据安全处理--同步锁 import time def sub(): global num print("ok") lock.acquire()#获取这把锁--->只有 ...
- win10文件夹共享
1.开启server服务就可以使用net share 命令 2.查看目前已共享的文件夹 3.关闭默认共享 只有用administrator(且有密码)才能连上win10上的默认共享了,只有admini ...
- 过采样算法之SMOTE
SMOTE(Synthetic Minority Oversampling Technique),合成少数类过采样技术.它是基于随机过采样算法的一种改进方案,由于随机过采样采取简单复制样本的策略来增加 ...
- SpringCloud学习系列-构建部门微服务提供者Module
1.新建microservicecloud-provider-dept-8001 2.POM <project xmlns="http://maven.apache.org/POM/4 ...
- Ldap 从入门到放弃(二)
OpenLDAP 服务器安装与配置 本文内容是自己通过官网文档.网络和相关书籍学习和理解并整理成文档,其中有错误或者疑问请在文章下方留言. 一.概述 本文以Centos 6.8(64bit)为例介绍 ...
- Spring Cloud(2)主要组件应用实例
SpringCloud SpringCloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.负载均衡.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行 ...
- springboot自定义异常数据
一.源码分析 自定义异常数据之前我们先看看一下源码 上述代码意思是如果你没有提供就使用springboot提供的类 这是springboot提供的异常属性类,我们想要自 ...