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

微服务架构下的主要组件

服务注册组件: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. SAS——proc format的其他应用:invalue,picture,default,mult,prefix,noedit,_same_,_error_,other

    一. proc format; invalue $test (default=200) "1"=_same_ "2"="Black" &qu ...

  2. JeeSite | 访问控制权限

    在各种后台系统中都会涉及到权限的管控,从功能权限的管控,到数据权限的管控,都是为了让系统的在使用的过程中更加的安全.功能权限管控是对针对不同的角色可以进行不同的功能操作,而数据权限管控是针对不同的角色 ...

  3. [STL] UVA 10815 安迪的第一个字典 Andy's First Dictionary

    1.set 集合 哦....对了,set有自动按照字典序排序功能..... 声明和插入操作 #include <cstdio> #include <vector> #inclu ...

  4. CentOS下安装FreeTDS

    导读 官方网站:http://www.freetds.org 下载地址:http://ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable ...

  5. 原创的离线版 Redis 教程,给力!

    嗯,你没看错,松哥又给大家送干货来了.这次是可以离线阅读的 PDF 版教程哦. 之前一直有小伙伴问我有没有 Redis 的电子书,老实说,有是有,但是公开给大家分享,其实有一点点风险,毕竟这都是有版权 ...

  6. Spring Cloud Ribbon客户端负载均衡(四)

    序言 Ribbon 是一个客户端负载均衡器(Nginx 为服务端负载均衡),它赋予了应用一些支配 HTTP 与 TCP 行为的能力,可以得知,这里的客户端负载均衡也是进程内负载均衡的一种.它在 Spr ...

  7. SATA接口、PCI/PCIe、NVMe的介绍

    SATA接口.PCI/PCIe.NVMe的介绍 SATA接口 SATA是Serial ATA的缩写,即串行ATA. SATA已经完全取代旧式PATA(Parallel ATA或旧称IDE)接口的旧式硬 ...

  8. C#之初识异步

    什么是异步 举个例子:小明的妈妈让小明烧一壶水,水烧开后要倒进水壶里,同时还需要把家里打扫一下. 小明的操作流程一:烧水---->等待至水烧开----->水倒进水壶里--------> ...

  9. 【机器学习笔记】Python机器学习基本语法

    本来算法没有那么复杂,但如果因为语法而攻不下就很耽误时间.于是就整理一下,搞python机器学习上都需要些什么基本语法,够用就行,可能会持续更新. Python四大类型 元组tuple,目前还没有感受 ...

  10. spring boot入门,看这篇文章就够了

    一.SpringBoot入门 1.基本介绍 简化Spring应用开发的一个框架.整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 优点: 快速创建独立运行的Spring项目以及与主 ...