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

微服务架构下的主要组件

服务注册组件: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. PHP-内嵌foreach的巧妙优化

    1.没有想好使用什么话题做开场白,不说多废话直接上代码了. 这是tp5.1的api接口里的代码,$user_list 是二维数组只有 1104一维数组数据   $friend_list 也是二维数组, ...

  2. HBase开发错误记录(java.net.UnknownHostException: unknown host: hadoop111)

    windows下开发HBase应用程序,HBase部署在linux环境中, 在运行调试时可能会出现无法找到主机,类似异常信息如下: java.net.UnknownHostException: unk ...

  3. Linux安装部署jdk+tomcat+mysql

    Linux安装部署测试环境 1. JDK安装下载 安装 yum search jdk 找到合适的jdk版本,以下图中的版本为例 yum install java-1.8.0-openjdk.x86_6 ...

  4. Mongodb--内存管理MMAP

    MongoDB使用的是内存映射存储引擎,即Memory Mapped Storage Engine,简称MMAP. MMAP可以把磁盘文件的一部分或全部内容直接映射到内存,这样文件中的信息位置就会在内 ...

  5. $.Ajax、$.Get、$.Post代码实例参数解析

    $.ajax 语法: $.ajax({name:value, name:value, ... }) 示例: $.ajax({ url: "/testJson", type: &qu ...

  6. 个性化你的 Git Log 的输出格式

    个性化你的 Git Log 的输出格式

  7. Caused by: java.lang.ClassNotFoundException: org.springframework.data.repository.config.BootstrapMode

    1.起因,启动SpringBoot2.0的时候报了这个错误.说找不到类,咱也是刚学SpringBoot2.0,咱也不懂,咱也不知道问谁,研究一翻,找不到原因就百度了. 参考链接:https://blo ...

  8. Gtksharp编译时提示下载gtk文件问题

    Gtksharp编译时提示下载gtk文件问题 1.昨天晚上新建gtksharp项目之后,安装gtksharp之后,编译时无法成功,提示无法下载gtk-3.24.zip 2.记得前几天,另一个项目可以生 ...

  9. Java编程基础——流程控制

    Java编程基础——流程控制 摘要:本文主要介绍Java编程中的流程控制语句. 分类 流程控制指的是在程序运行的过程中控制程序运行走向的方式.主要分为以下三种: 顺序结构:从上到下依次执行每条语句操作 ...

  10. CSS 选择器大全

    在CSS中,选择器是用于选择要设置样式的元素的模式. 选择器 例子 描述 .class .intro 选择class=“intro”的所有元素 #id #firstname 选择id=“firstna ...