SpringCloud Alibaba

Blog Cloud

Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案。此项目包含开发分布式应用微服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。

依托 Spring Cloud Alibaba,您只需要添加一些注解和少量配置,就可以将 Spring Cloud 应用接入阿里微服务解决方案,通过阿里中间件来迅速搭建分布式应用系统。

代码地址:https://github.com/typ1805/blog-cloud

版本介绍

  • java.version 1.8
  • spring-boot.version:2.3.2.RELEASE
  • spring-cloud.version:Hoxton.SR8
  • com.alibaba.cloud.version:2.2.3.RELEASE
  • nimbus-jose-jwt.version:9.1.1
  • lombok.version:1.18.12
  • fastjson.version:1.2.62
  • druid.version:1.1.10
  • mybatis-plus.version:3.2.0
  • mysql.version:5.1.38

版本参考地址:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E


软件架构

一、组件

组件名称

版本号

描述

Nacos Discovery

----

注册中心

Nacos Config

----

配置中心

Dubbo Spring Cloud

----

服务通信

Spring Cloud Gateway

----

网关

Spring Cloud Security

----

安全认证

Spring Cloud Hystrix

----

服务熔断

Spring Cloud Sleuth + Zipkin

----

调用链监控

Spring Data Redis

----

NoSQL数据库

...

...

...

二、模块

模块名称

描述

备注

blog-gateway

网关

--

blog-auth

认证中心

--

blog-common

公共组件

--

blog-contract

Dubbo接口暴露

--

blog-user

用户权限管理

--

...

...

...


功能介绍

一、Nacos注册中心、配置中心

以网关配置为例(blog-gateway) 官网文档:

注册中心:https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc

配置中心:https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc

Nacos Server 启动后,进入 http://localhost:8848/nacos/index.html 查看控制台(默认账号名/密码为 nacos/nacos):

1、添加依赖

1<dependency> 2 <groupId>com.alibaba.cloud</groupId> 3 <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> 4</dependency> 5<dependency> 6 <groupId>com.alibaba.cloud</groupId> 7 <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> 8</dependency> 9

2、添加配置文件(bootstrap.yml)

1spring: 2 application: 3 name: blog-gateway 4 cloud: 5 nacos: 6 config: 7 server-addr: 127.0.0.1:8848 # Nacos服务地址 8 file-extension: yaml # 配置格式 9# shared-dataids: all-gateway.yaml # 全局引入的配置 10# refresh-dataids: all-gateway.yaml # 实现动态配置刷新 11 namespace: 57cace63-8a7e-4051-825b-ba5ff39f9911 # 命名空间 12 profiles: 13 active: dev # 环境配置 14

3、创建配置文件

在配置管理 》 配置列表 中添加 blog-gateway-dev.yaml 文件。

命名规则: 服务名称(blog-gateway) + 环境名称(dev、test) + 后缀(.yaml、.properties

4、注意事项

  • 命名规则必须一致,否则不生效;
  • 配置格式必须对应,否则不生效;
  • 如使用命名空间,则命名空间必须对应,否则不生效;
  • 如使用全局配置,必须配置(shared-dataids、refresh-dataids)全局引入的配置和实现动态配置刷新。

5、Nacos基本概念

5.1、命名空间(namespace)

命名空间可用于进行不同环境的配置隔离,一般一个环境划分一个命名空间。

5.2、配置分组(group)

配置分组用于讲不同的服务可以归类到同一分组,一般讲一个项目的配置分到一个组中。

5.3、配置集(dateId)

在系统中,一个配置文件就是一个配置集,一般微服务的配置就是一个配置集。

二、熔断&限流

1、添加依赖

1<dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> 4</dependency> 5<dependency> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-data-redis-reactive</artifactId> 8</dependency> 9

2、添加配置

1# 配置路由规则 2routes: 3 # 采用自定义路由ID 4 - id: blog-auth 5 # 采用 LoadBalanceClient 方式请求,以 lb:// 开头,后面的是注册在 Nacos 上的服务名 6 uri: lb://blog-auth 7 predicates: 8 - Path=/auth/** 9 filters: 10 - StripPrefix=1 11 - name: Hystrix # 熔断 12 args: 13 name: fallbackcmd 14 # fallback 时调用的方法 http://localhost:8080/fallback 15 fallbackUri: forward:/fallback 16 - name: RequestRateLimiter # 限流 17 args: 18 key-resolver: '#{@uriKeyResolver}' # 限流过滤器的 Bean 名称 19 redis-rate-limiter.replenishRate: 1 # 希望允许用户每秒处理多少个请求 20 redis-rate-limiter.burstCapacity: 3 # 用户允许在一秒钟内完成的最大请求数 21

3、熔断实现

3.1、Hystrix服务降级处理

1@Slf4j 2@Component 3public class HystrixFallbackHandler implements HandlerFunction<ServerResponse> { 4 5 @Override 6 public Mono<ServerResponse> handle(ServerRequest serverRequest) { 7 Optional<Object> originalUris = serverRequest.attribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR); 8 originalUris.ifPresent(originalUri -> log.error("网关执行请求:{}失败,hystrix服务降级处理", originalUri)); 9 10 HashMap<String, Object> response = new HashMap<>(); 11 response.put("message", "您的操作太频繁,请稍后重试"); 12 response.put("code", HttpStatus.INTERNAL_SERVER_ERROR.value()); 13 14 return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR.value()) 15 .contentType(MediaType.APPLICATION_JSON_UTF8) 16 .body(BodyInserters.fromObject(response)); 17 } 18 19} 20

3.2、服务降级处理(fallback)

1@Configuration 2@AllArgsConstructor 3public class RouterFunctionConfiguration { 4 5 private final HystrixFallbackHandler hystrixFallbackHandler; 6 7 @Bean 8 public RouterFunction routerFunction() { 9 return RouterFunctions.route( 10 RequestPredicates.path("/fallback") 11 .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), hystrixFallbackHandler); 12 } 13} 14

4、限流实现

1@Configuration 2public class UriKeyResolver implements KeyResolver { 3 4 /** 5 * @methodName:resolve 6 * @description:根据请求的 uri 限流 7 * @author:tanyp 8 * @dateTime:2020/11/25 11:16 9 * @Params: [exchange] 10 * @Return: reactor.core.publisher.Mono<java.lang.String> 11 * @editNote12 */ 13 @Override 14 public Mono<String> resolve(ServerWebExchange exchange) { 15 return Mono.just(exchange.getRequest().getURI().getPath()); 16 } 17 18} 19

限流过滤器的Bean名称(key-resolver: '#{@uriKeyResolver}')保持一致 UriKeyResolver.java

三、跨域配置

1@Configuration 2public class CorsConfig { 3 4 @Bean 5 public CorsWebFilter corsFilter() { 6 final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); 7 final CorsConfiguration config = new CorsConfiguration(); 8 // 允许cookies跨域 9 config.setAllowCredentials(true); 10 // #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin 11 config.addAllowedOrigin("*"); 12 // #允许访问的头信息,*表示全部 13 config.addAllowedHeader("*"); 14 // 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了 15 config.setMaxAge(18000L); 16 // 允许提交请求的方法,*表示全部允许 17 config.addAllowedMethod("OPTIONS"); 18 config.addAllowedMethod("HEAD"); 19 // 允许Get的请求方法 20 config.addAllowedMethod("GET"); 21 config.addAllowedMethod("PUT"); 22 config.addAllowedMethod("POST"); 23 config.addAllowedMethod("DELETE"); 24 config.addAllowedMethod("PATCH"); 25 source.registerCorsConfiguration("/**", config); 26 return new CorsWebFilter(source); 27 } 28} 29

四、链路追踪

使用 Spring Cloud Sleuth + Zipkin 作为微服务架构分布式服务跟踪。

1、SpringCloud Sleuth

为服务之间调用提供链路追踪。通过 Sleuth 可以很清楚的了解到一个服务请求经过了哪些服务,每个服务处理花费了多长。从而让我们可以很方便的理清各微服务间的调用关系。此外 Sleuth 可以帮助我们:

  • 耗时分析:通过 Sleuth 可以很方便的了解到每个采样请求的耗时,从而分析出哪些服务调用比较耗时;
  • 可视化错误:对于程序未捕捉的异常,可以通过集成 Zipkin 服务界面上看到;
  • 链路优化:对于调用比较频繁的服务,可以针对这些服务实施一些优化措施。

Spring Cloud Sleuth 可以结合 Zipkin,将信息发送到 Zipkin,利用 Zipkin 的存储来存储信息,利用 Zipkin UI 来展示数据。

2、Zipkin

Zipkin 是 Twitter 的开源分布式跟踪系统,它基于 Google Dapper 实现,它致力于收集服务的时序数据,以解决微服务架构中的延迟问题,包括数据的收集、存储、查找和展现。

2.1、下载Zipkin Server

https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/

2.2、启动Zipkin Server

下载完成之后,在下载的jar所在目录,执行 java -jar *****.jar 命令即可启动Zipkin Server

2.3、访问

http://localhost:9411 即可看到Zipkin Server的首页。

3、添加配置

1spring: 2 zipkin: 3 base-url: http://localhost:9411/ 4 sleuth: 5 sampler: 6 # 抽样率,默认是0.190%的数据会被丢弃) 7 # 这边为了测试方便,将其设置为1.0,即所有的数据都会上报给zipkin 8 probability: 1.0 9

4、Zipkin数据持久化

当前搭建Zipkin是基于内存的,如果Zipkin发生重启的话,数据就会丢失,这种方式是不适用于生产的,所以我们需要实现数据持久化。 Zipkin给出三种数据持久化方法:

  • MySQL:存在性能问题,不建议使用
  • Elasticsearch
  • Cassandra

相关的官方文档:https://github.com/openzipkin/zipkin#storage-component 介绍Elasticsearch实现Zipkin数据。

持续更新中...

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )