仅使用,无多少技术含量,权记于此以备忘。

微服务架构下的主要组件

服务注册组件:Consul、Etcd等

网关:Zuul、Spring Cloud Gateway等

容错框架:Hystrix

负载均衡器:Ribbon

Web服务调用客户端:Feign

以上组件在Spring Cloud中均有集成,很容易上手使用。虽然组件层出不穷,有的过时、又有新的类似的产生,但万变不离其宗,掌握一个后,同类通过类比可以迅速掌握。

Zuul

what

微服务的网关,相当于一个反向代理服务器(此时与Nginx、Spring Cloud Gateway的功能类似),Netflix开发。这里记录Spring Cloud中集成Zuul、Consul的使用。

how it works

主要原理图如下,可见其主要是在请求前后进行一系列的filter;作为gateway,在最前方接收前端对各服务的请求,因此压力较大,Zuul用了netty来实现高并发能力。

how to use

引入依赖(这里服务注册组件用consul):

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency> <!-- 如下依赖自身包含了spring boot starter web、actuator等依赖,故引如下依赖就是个SpringBoot项目了 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>

主类:

@SpringBootApplication
@EnableZuulProxy
public class ZuulMain {
public static void main(String[] args) {
SpringApplication.run(ZuulMain.class, args);
}
}

配置:

server:
port: 8084 spring:
application:
name: sensestudy-zuul cloud:
consul:
enabled: true
host: localhost
port: 8500
discovery:
# serviceName: ${spring.application.name}
tags: sensestudy, zuul
healthCheckInterval: 15s
fail-fast: false
prefer-ip-address: true
# health-check-path: ${server.servlet.context-path}/actuator/health zuul:
# prefix: /sensestudy # path统一加前缀
ignored-services: '*' # 默认路由规则为按服务id来查找目标服务的地址,此配置禁用该默认行为,并由下面的routes配置路由规则
routes:
sensestudy-acl: /acl/**
app1:
path: /coursecenter/**
serviceId: sensestudy-coursecenter
# url: http://www.baidu.com

说明:

默认路由规则:对于http://localhost:8080/sensestudy/myacl?userId=1,zuul会尝试从服务注册中心找id为myacl的服务并向其发起请求。通常通过ignored-services禁用默认规则并自己指定路由规则。

自定义路由规则:

sensestudy-acl: /acl/** :zuul会匹配符合后者指定的路径的请求,并转发到id为前者的服务上。这是最简写法,也可如上面配置中所示,用多行指定两者对应关系。

参考资料:

https://github.com/Netflix/zuul/wiki

https://www.cnblogs.com/leeSmall/p/8850215.html

Ribbon

主要作为负载均衡工具。Netflix开发。

Ribbon is a client side IPC library that is battle-tested in cloud. It provides the following features

  • Load balancing
  • Fault tolerance
  • Multiple protocol (HTTP, TCP, UDP) support in an asynchronous and reactive model
  • Caching and batching

参阅:https://github.com/Netflix/ribbon

Hystrix

容错框架。Netflix开发。

Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.

参阅:https://github.com/Netflix/Hystrixhttps://www.cnblogs.com/z-sm/p/12006347.html

Feign

Netflix开发的声明式、模板化的HTTP客户端, 可以让我们更快捷、优雅地调用HTTP API。

Spring Cloud Netflix整合了Spring Cloud Ribbon和Spring Cloud Hystrix。

原理:

  • 启动时,程序扫描所有包下所有@FeignClient注解的类,并将这些类注入到spring的IOC容器。当定义的Feign中的接口被调用时,通过JDK的动态代理来生成RequestTemplate。
  • RequestTemplate中包含请求的所有信息,如请求参数,请求URL等。
  • RequestTemplate生成Request,然后将Request交给client处理。这个client默认是JDK的HTTPUrlConnection,也可以是OKhttp、Apache的HTTPClient等。
  • 最后client封装成LoadBaLanceClient,结合Ribbon负载均衡地发起调用。

使用示例可见:https://www.cnblogs.com/z-sm/p/10018713.html

参阅:what-https://juejin.im/post/5ccbe82851882544da5008ce,原理-https://www.jianshu.com/p/ce6631e8c762

Spring Boot Actuator

Spring Boot 提供的对应用系统的自省和监控的集成功能,几乎监控了应用涉及的方方面面,如自动化配置信息、创建的 Spring beans 以及一些环境属性等。

很多其他应用依赖于actuator提供的监控信息,如Spring Boot Admin利用actuator来获取应用的各种状态信息展示在页面、Spring Cloud Consul利用actuator的health endpoints来对应用进行健康检查。

Actuator 监控分成两类:原生端点和用户自定义端点;自定义端点主要是指扩展性,用户可以根据自己的实际应用,定义一些比较关心的指标,在运行期进行监控。

原生端点是在应用程序里提供众多 Web 接口,通过它们了解应用程序运行时的内部状况。原生端点又可以分成三类:

  • 应用配置类:可以查看应用在运行期的静态信息:例如自动配置信息、加载的 springbean 信息、yml 文件配置信息、环境信息、请求映射信息;
  • 度量指标类:主要是运行期的动态信息,例如堆栈、请求连、一些健康指标、metrics 信息等;
  • 操作控制类:主要是指 shutdown,用户可以发送一个请求将应用的监控功能关闭。

actuator提供的endpoints(不同版本可能有所差别):

健康检查:

默认情况下,对一个项目健康检查时,如果项目中用到了Spring的MySQL、Redis、MongoDB等(即引入Spring的相关包)则会同时检查项目中相关组件的健康状态,只要有一个相关组件不健康,则认为该项目也不健康。当然,可以设置不同时检查相关组件,如management.health.redis.enabled=false。这是个大坑

上述的原理实际上是很多Spring组件都实现了HealthIndicator接口,健康检查时默认认为一个项目中的所有HealthIndicator返回结果为健康时该项目才为健康。Spring默认的HealthIndicator有很多,当然我们也可借助它实现自定义健康检查。

默认情况下,健康和不健康的http状态码分别为200、503

详情可参阅:

https://codecentric.github.io/spring-boot-admin/current/

http://www.ityouknow.com/springboot/2018/02/06/spring-boot-actuator.html

https://www.jianshu.com/p/1aadc4c85f51

Spring Boot Admin

用于管理和监控SpringBoot Web应用程序。 应用程序作为Spring Boot Admin Client向Spring Boot Admin Server注册发现。其实就是基于Actuator endpoints提供了个界面。

The UI is just a Vue.js application on top of the Spring Boot Actuator endpoints

提供的功能主要有:

显示健康状况
显示详细信息,例如:JVM和内存指标、数据源指标、缓存指标等
查看jvm system-和environment-properties
查看Spring Boot配置属性
支持Spring Cloud的postable / env-和/ refresh-endpoint
查看线程转储
查看http-traces
查看auditevents
查看http-endpoints
查看计划任务
状态变更通知(通过电子邮件,Slack,Hipchat,......)
状态更改的事件日志(非持久性)等

示意图:

Admin Server配置

1、依赖:

<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.0</version>
</dependency>

2、配置:

server:
port: 8091 spring:
application:
name: sensestudy-admin

3、主类:加@EnableAdminServer注解以启用AdminServer

@SpringBootApplication
@EnableAdminServer
public class AdminMain {
public static void main(String[] args) {
SpringApplication.run(AdminMain.class, args);
}
}

Admin Client配置

每个Spring Boot Web应用都可作为Admin Client。

两种配置方法:

第一种:引入spring-boot-admin-starter-client依赖、通过 spring.boot.admin.client.url=http://localhost:8091 指定admin server的地址即可。此法应用较少。

第二种:借助注册中心如Consul。以Consul为例,只要将Admin Server和Client都注册到同一个Consul中,Admin Server就会自动发现注册中心中的所有应用(Client无需做任何关于spring boot admin的配置:无需引入admin client依赖也无需配置admi server的地址)。推荐此法。Admin Server是通过actuator暴露的endpoints去获取到Client的信息并聚合显示的,故可引入actuator依赖否则获取到的信息有限。

1、Admin Server和Client都引入Consul依赖:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-actuator</artifactId>
</dependency>

2、 配置Consul和actuator暴露的endpoints:

spring:
application:
name: sensestudy-admin cloud:
consul:
enabled: true
host: localhost
port: 8500
discovery:
# serviceName: ${spring.application.name}
tags: sensestudy, admin
healthCheckInterval: 15s
fail-fast: false
prefer-ip-address: true
# health-check-path: ${server.servlet.context-path}/actuator/health management:
endpoints:
web:
exposure:
include: '*'

consul & actuator config

注:为了安全,提供外网访问的程序不应暴露所有endpoints;Admin Server自身若做了这些配置则也会出现在Admin监控列表中。

参考资料:

https://codecentric.github.io/spring-boot-admin/current/https://juejin.im/post/5c774737e51d4539982f3f6e

Spring Cloud组件使用/配置小记的更多相关文章

  1. Spring顶级项目以及Spring cloud组件

    作为java的屌丝,基本上跟上spring屌丝的步伐,也就跟上了主流技术. spring 顶级项目: Spring IO platform:用于系统部署,是可集成的,构建现代化应用的版本平台,具体来说 ...

  2. 跟我学SpringCloud | 第十九章:Spring Cloud 组件 Docker 化

    前面的文章<跟我学SpringCloud | 第十八篇:微服务 Docker 化之基础环境>我们介绍了基础环境系统和 JRE 的容器化,这一节我们介绍 Spring Cloud 组件的容器 ...

  3. SpringCloud升级之路2020.0.x版-9.如何理解并定制一个Spring Cloud组件

    本系列为之前系列的整理重启版,随着项目的发展以及项目中的使用,之前系列里面很多东西发生了变化,并且还有一些东西之前系列并没有提到,所以重启这个系列重新整理下,欢迎各位留言交流,谢谢!~ 我们实现的 S ...

  4. Spring Cloud Config(配置中心)

    每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 一.简介 Spring Cloud Config为分布式系统中的外部配置提供服务器和客 ...

  5. spring cloud Eureka client配置(consumer通过Eureka发起对provider的调用)

    参考:http://www.ityouknow.com/springcloud/2017/05/12/eureka-provider-constomer.html springboot版本:2.0.3 ...

  6. spring cloud config将配置存储在数据库中

    Spring Cloud Config Server最常见是将配置文件放在本地或者远程Git仓库,放在本地是将将所有的配置文件统一写在Config Server工程目录下,如果需要修改配置,需要重启c ...

  7. Spring Cloud Config的配置中心获取不到最新配置信息的问题

    Spring Cloud Config的配置中心获取不到最新配置信息的问题 http://blog.didispace.com/spring-cloud-tips-config-tmp-clear/

  8. 跟我学SpringCloud | 第六篇:Spring Cloud Config Github配置中心

    SpringCloud系列教程 | 第六篇:Spring Cloud Config Github配置中心 Springboot: 2.1.6.RELEASE SpringCloud: Greenwic ...

  9. Spring Cloud Config 实现配置中心,看这一篇就够了

    Spring Cloud Config 是 Spring Cloud 家族中最早的配置中心,虽然后来又发布了 Consul 可以代替配置中心功能,但是 Config 依然适用于 Spring Clou ...

随机推荐

  1. touch.js - 移动设备上的手势识别与事件库

    Touch.js 是移动设备上的手势识别与事件库, 由百度云Clouda团队维护,也是在百度内部广泛使用的开发工具.Touch.js手势库专为移动设备设计.Touch.js对于网页设计师来说,是一款不 ...

  2. 前端实现的canvas支持多图压缩并打包下载的工具

    # 技术栈 canvas jszip.js(网页端压缩解压缩插件JSZIP库) FileSaver.js(文件保存到本地库) 在线预览:http://htmlpreview.github.io/?ht ...

  3. Preface_英语

    这是一本游戏指南.没错,你没有 看错,这就是一本游戏指南.当然,这 本指南针对的只是名为"英文"的游戏. 把英文和电子游戏比较一下,我们 会发现,这两者有惊人的相似之处. 第一,它 ...

  4. 一文教您如何通过 Docker 搭建反向代理 Ngnix,并配置 Https SSL 证书

    欢迎关注个人微信公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注附送 100G 海量学习资源哟!! 个人网站: https://www.exception.site/docker/h ...

  5. C# in 参数修饰符

    in 修饰符记录: 新版C# 新增加的 in 修饰符:保证发送到方法当中的数据不被更改(值类型),当in 修饰符用于引用类型时,可以改变变量的内容,单不能更改变量本身. 个人理解:in 修饰符传递的数 ...

  6. Winform中设置ZedGraph当前所有曲线的颜色

    场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  7. Winforn中设置ZedGraoh的GraphPane恢复到初始比例大小

    场景 Winform中实现ZedGraph中曲线右键显示为中文: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100115292 ...

  8. Spring Boot 使用 Log4j2 & Logback 输出日志到 EKL

    文章目录 1.ELK 介绍 2.环境.软件准备 3.ELK 环境搭建 4.Spring Boot 配置示例 4.1.Log4j2 方式配置 4.2.Logback 方式配置 1.ELK 介绍 ELK ...

  9. 《深入理解Java虚拟机》并发(第12~13章)笔记

    volatile关键字的作用 所有变量的可见性--仅仅是修改后的值的可见性,不保证并发修改时新值和预期一致.即只保证读,不保证写. 禁止指令重排序--修饰的变量,读写不会指令重排.如变量isReady ...

  10. 场sharepoint2016数据库恢复站点

    前不久公司support方,不小心把IIS的应用删除了,算是灼急了,不过有过原来恢复的经历,似乎有了心理准备,可是这次比上次严重些.技术操作复杂些,不过通过此事,也是进一步了解了SP2016数据库结构 ...