Spring Cloud基础架构实战

Spring Cloud基础架构实战
Spring Cloud简介
Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种简单的开发方式。

Spring Cloud包含了多个子项目(针对分布式系统中涉及的多个不同开源产品),比如:Spring Cloud Config、Spring Cloud Netflix、Spring Cloud CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring Cloud Commons、Spring Cloud Zookeeper、Spring Cloud CLI等项目。

Spring Cloud基础架构实战
Spring Cloud子项目
① Spring Cloud Config:配置管理工具,支持使用Git存储配置内容,可以使用它实现应用配置的外部化存储,并支持客户端配置信息刷新,加密/解密配置内容等。

② Spring Cloud Netflix:核心组件,对多个Netflix OSS开源套件进行整合。

Eureka:服务治理组件,包含服务注册中心、服务注册与发现机制的实现。
Hystrix:容错管理组件,实现断路器模式,帮助服务依赖中出现的延迟和为故障提供强大的容错能力。
Ribbon:客户端负载均衡的服务调用组件。
Feign:基于Ribbon和Hystrix的声明式服务调用组件。
Zuul:网关组件,提供智能路由、访问过滤等功能。
Archaius:外部化配置组件。
③ Spring Cloud Bus:事件、消息总线,用于传播集群中的状态变化或事件,以触发后续的处理,比如用来动态刷新配置。

④ Spring Cloud Cluster:针对ZooKeeper、Redis、Hazelcast、Consul的选举算法和通用状态模式的实现。

⑤ Spring Cloud Consul:服务发现与配置管理工具。

⑥ Spring Cloud Stream:通过Redis、Rabbit或者Kafka实现的消费微服务,可以通过简单的声明式模型来发送和接收消息。

⑦ Spring Cloud Security:安全工具包,提供在Zuul代理中对OAuth2客户端请求的中继器。

⑧ Spring Cloud Sleuth:Spring Cloud 应用的分布式跟踪实现,可以完美整合 Zipkin。

⑨ Spring Cloud ZooKeeper:基于ZooKeeper 的服务发现与配置管理组件。

⑩ Spring Cloud Starters:Spring Cloud 的基础组件,它是基于Spring Boot 风格项目的基础依赖模块。

组件架构

Spring Cloud基础架构实战
基础架构项目简介(laaS-Service)
本次实战以Springboot+maven为基础进行快速配置,采用分布式微服务架构下的一站式解决方案Spring Cloud,集成组件Eureka+Config+Zuul+Feign/Ribbon+Hystrix+RabbitMQ+Redis+Sleuth+ZipKin。

开发环境:JDK1.8+MySQL5.7 + IDEA + RabbitMQ + Redis

Git地址:https://github.com/xuzhi199/lass

项目模块主要有:

工程名

说明

api-model

数据传输对象、常量等

commons

工具类、公共常量等

config-center

配置中心

file-center

文件中心

gateway-zuul

网关

log-center

日志中心

log-starter

日志组件,别的项目直接引入该依赖即可

manage-backend

后台管理

monitor-center

监控中心

oauth-center

认证中心

register-center

注册中心

user-center

用户中心、用户、角色、权限

项目包结构:

Spring Cloud基础架构实战

  1. 父pom.xml
    Spring Cloud基础架构实战
    这里配置的各个模块目录

Spring Cloud基础架构实战
这里定义了一些jar包的版本号

最外层pom.xml

org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-test test org.projectlombok lombok com.alibaba fastjson ${fastjson.version} org.apache.commons commons-lang3 org.apache.commons commons-collections4 ${commons-collections} org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.mybatis.spring.boot mybatis-spring-boot-starter ${mybatis.version} io.jsonwebtoken jjwt ${jwt.version} Spring cloud-Eureka注册中心

Spring Cloud基础架构实战
注册中心架构

  1. bootstrap.yml
    Spring Cloud基础架构实战
    给该应用起个名字

  2. 启动注册中心

Spring Cloud基础架构实战
直接运行下面类的main方法com.cloud.register.RegisterCenterApplication

  1. 访问http://localhost:8761
    8761是bootstrap.yml里配置的系统端口号

Spring Cloud基础架构实战
Application列下的,REGISTER-CENTER就是我们在bootstrap.yml里指定的应用名,注册中心给我们大写处理了。

Status下显示的是我们在bootstrap.yml里的instance-id,如下图,我们这里是应用名加端口号

Spring Cloud基础架构实战
Spring Cloud基础架构实战
该配置为true的话,是用ip注册,否则是主机名注册,强烈建议配置为true

Spring Cloud基础架构实战
点击会跳转到类似如下的地址http://192.168.0.111:8761/info用这种方式,可以看到服务的具体ip地址和端口

统一配置中心(config)

Spring Cloud基础架构实战
bootstrap.yml

Spring Cloud基础架构实战
通过spring.profiles.active这里可以指定配置文件在本地,还是classpath下还是在远程git上面,这里默认是放在了本地的classpath下,这里可根据实际项目需求和运维条件进行合理的选择配置方式。

{profile}

上图配置中的{profile},是由别的微服务指定的,如用户中心指定配置,用户中心里会有如下配置,

Spring Cloud基础架构实战
这里的profile: dev就会注入到

Spring Cloud基础架构实战
配置目录就成了classpath:/configs/dev

用户中心启动的时候,就会从配置中心拉取配置,目录就是

classpath:/configs/dev/user-center.yml

因此用户中心通过配置test或者production等等自定义的字符串,启动时会去找相应的配置,来达到分环境配置的目的,如

Spring Cloud基础架构实战
端口
Spring Cloud基础架构实战
这里配置成0,启动时,项目会随机一个端口号

注册到注册中心
Spring Cloud基础架构实战
这里需要注意,地址后面有个/eureka/

如果是多注册中心,那么通过逗号分隔

Spring Cloud基础架构实战
注册中心里的显示

Spring Cloud基础架构实战
因为我们是随机端口号,我们这里用了随机数字来显示

Spring Cloud基础架构实战
注意那个随机数字,并不是真正的端口号,点击跳转之后

http://192.168.0.104:53484/info

这里我们才能看到真正的端口号

用户中心
Spring Cloud基础架构实战
数据库脚本
在主模块下的sql文件夹下cloud_user.sql里是用户中心的数据脚本,包含建表语句和初始化数据。

bootstrap.yml
Spring Cloud基础架构实战
这里主要配置,配置中心的serviceId就是配置中心的spring.application.name,还有自己的profile,还有注册中心的url

user-center.yml
这里配置了用户系统具体的一些配置,比如数据库、mq、mybatis、日志级别等。

日志级别和文件配置
Spring Cloud基础架构实战
数据源配置
Spring Cloud基础架构实战
Rabbitmq配置
Spring Cloud基础架构实战
Mybatis配置
Spring Cloud基础架构实战
别名包有多个的话,逗号隔开,如下图

Spring Cloud基础架构实战
复杂sql写在mapper.xml文件里,在下图路径下

Spring Cloud基础架构实战
微信公众号配置
Spring Cloud基础架构实战
详细看下代码com.aorise.user.service.impl.WechatServiceImpl和

laaS-service\manage-backend\src\main\resources\static\pages\wechat\index.html

配置类
全局异常处理
Spring Cloud基础架构实战
抛出java.lang.IllegalArgumentException异常的接口将返回http状态码400

Rabbitmq的exchange声明
Spring Cloud基础架构实战
这里声明一个topic类型的exchange,发消息时用。

资源权限配置
Spring Cloud基础架构实战
Spring Cloud基础架构实战
将我们的项目作为资源服务器

Spring Cloud基础架构实战
是否启动权限注解支持

Spring Cloud基础架构实战
这里符合规则的url将不做权限拦截。

密码加密处理器
Spring Cloud基础架构实战
声明一个密码加密和校验处理器Bean,该bean是spring security自带的。

认证中心
Spring Cloud基础架构实战
数据库脚本
在主模块下的sql文件夹下cloud_oauth.sql里是认证中心的数据脚本,包含建表语句和初始化数据。

bootstrap.yml
除spring.application.name是oauth-center外,其他如用户中心的bootstrap.yml相同

oauth-center.yml
redis配置
Spring Cloud基础架构实战
如redis有密码,与host同层级加节点password,如

Spring Cloud基础架构实战
注意password冒号后加一个空格

数据库配置
Spring Cloud基础架构实战
token是否用jwt
Spring Cloud基础架构实战
false的话token是默认的uuid,true的话token将采用jwt

com.aorise.oauth.config.AuthorizationServerConfig里面

Spring Cloud基础架构实战
Spring Cloud基础架构实战
使用jwt时,需要配置这个签名key,具体可看下

com.aorise.oauth.config.AuthorizationServerConfig里面的

Spring Cloud基础架构实战
配置类
Spring Cloud基础架构实战
授权服务器配置
Spring Cloud基础架构实战
资源服务器
Spring Cloud基础架构实战
Session共享配置
Spring Cloud基础架构实战
用redis做session共享,在授权码模式下,可能会涉及参数code和state和redirect_url的传递,多台服务器下需要共享session。

目前该项目没用授权码模式,此处不设置也没问题。

文件中心
Spring Cloud基础架构实战
bootstrap.yml里spring.application.name为file-center其余跟用户中心的一样

file-center.yml
数据库和mq
Spring Cloud基础架构实战
上传文件大小限制
Spring Cloud基础架构实战
自定义配置-本地存储文件
Spring Cloud基础架构实战
上图path是上传文件存储根路径

上图prefix是前缀

上图urlPrefix是域名加前缀

如d:/localFile/aaa.png用url访问就是

http://api.gateway.com:8080/api-f/statics/aaa.png

阿里云存储文件
Spring Cloud基础架构实战
如要上传图片到阿里云,这里需要配置你的阿里云对象存储OSS相关配置,详细根据视频目录看下视频。

配置类
加载jar包外文件
Spring Cloud基础架构实战
上传文件存储路径肯定是在jar包外部的,这里不像传统war包是解压成文件夹的,因此这里要做个静态资源的映射处理。

Spring Cloud基础架构实战
这里将url前缀和存储路径做了个映射

资源服务器
Spring Cloud基础架构实战
这里要将静态资源下的路径放开权限

Spring Cloud基础架构实战
网关
Spring Cloud基础架构实战
bootstrap.yml里spring.application.name为gateway-zuul其余跟用户中心的一样

gateway-zuul.yml
路由规则
Spring Cloud基础架构实战
sensitiveHeaders过滤客户端附带的headers,如:

sensitiveHeaders: X-ABC
如果在发请求时带了X-ABC,那么X-ABC不会往下游服务传递。

自定义参数
Spring Cloud基础架构实战
这里有个cron定时任务表达式,每5分钟执行一次

com.aorise.gateway.filter.BlackIPAccessFilter

Spring Cloud基础架构实战
配置类
Spring Cloud基础架构实战
跨域配置
Spring Cloud基础架构实战
只需要在网关层配置,别的微服务不需要配置跨域

异常处理

Spring Cloud基础架构实战
这里主要处理FeignException,这个是feignclient调用时的异常,不处理的话将会抛出500服务端异常,这里只是将下游服务的原始http状态码还原。

日志组件log-starter
Spring Cloud基础架构实战
这里是模仿spring boot自动配置写的一个组件,就像spring boot里的各种starter,如

Spring Cloud基础架构实战
你只需要引入mybatis的starter,和数据源的配置,就可以用mybatis了。

spring.factories
Spring Cloud基础架构实战
这里配置自动配置的类

Spring Cloud基础架构实战
使用该组件
我们这里的log-starter是依赖rabbitmq的,只需要引入

Spring Cloud基础架构实战
再配置上mq信息,下图的aop类即可生效,就实现了aop日志拦截,将log信息发送到mq队列。

Spring Cloud基础架构实战
日志中心
Spring Cloud基础架构实战
bootstrap.yml里spring.application.name为log-center其余跟用户中心的一样

log-center.yml
Spring Cloud基础架构实战
主要是数据库、mq、mybatis的配置,elasticsearch不是必用的

Spring Cloud基础架构实战
配置类
Spring Cloud基础架构实战
开启异步线程池
Spring Cloud基础架构实战
声明队列
Spring Cloud基础架构实战
处理日志消息
Spring Cloud基础架构实战
从队列中处理消息,将日志存入数据库

日志存储mysql和elasticsearch切换
Spring Cloud基础架构实战
如想存储到elasticsearch的话,

注释掉LogServiceImpl上的@Primary和@Service

Spring Cloud基础架构实战
或者将@Primary移到EsLogServiceImpl上面

后台管理系统
Spring Cloud基础架构实战
bootstrap.yml里spring.application.name为manage-backend其余跟用户中心的一样

manage-backend.yml
数据库和mq
Spring Cloud基础架构实战
邮件配置
不发邮件的话,请忽略即可,如要发邮件,

如要使用邮件模块,请写上正确的username和password,并且将最后两行的注释配置打开,否则发邮件可能会失败,如下图

Spring Cloud基础架构实战
163邮箱如何开启POP3/SMTP/IMAP服务?

http://help.163.com/10/0312/13/61J0LI3200752CLQ.html

消息处理
Spring Cloud基础架构实战
用户系统删除角色时,会抛消息,后台系统将接收该消息,删除菜单与角色的关系

静态资源
Spring Cloud基础架构实战
该目录的静态文件和页面是后台管理服务一部分,可单独拿出来部署

Spring Cloud基础架构实战
这里定义了一个常量

Spring Cloud基础架构实战
监控中心
Spring Cloud基础架构实战
监控中心我们在bootstrap.yml指定了固定的端口号9001

访问http://localhost:9001即可查看监控界面

通知中心
Spring Cloud基础架构实战
notification-center.yml
数据库、mq、redis都和别的配置相似,主要说下阿里云短信配置

Spring Cloud基础架构实战
这些参数需要从阿里云管理控制台自己创建

Spring Cloud基础架构实战
这里是短信过期时间15分钟,和一天能发送的验证码个数上限。

Swagger配置
除注册中心、配置中心、监控中心不提供对外接口,别的项目都有

SwaggerConfig这个类,如下图

Spring Cloud基础架构实战
user-info-uri
在配置中心许多微服务的配置里都有

Spring Cloud基础架构实战
这里是从认证中心获取用户信息的地方,

对应oauth-center里的接口,下图

Spring Cloud基础架构实战
因为加了注解@EnableResourceServer的各微服务都是资源服务器

Spring Cloud基础架构实战
是需要校验用户权限的,通过user-info-uri可以从认证中心获取到用户的信息和权限,感兴趣的可以看下

org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter

commons模块
PermitAllUrl
com.aorise.common.constants.PermitAllUrl

该类主要定义了一些不需要权限拦截的url,

Spring Cloud基础架构实战
下图这几个是swagger文档需要放开的url,其余是监控中心需要放开的url

Spring Cloud基础架构实战
AppUserUtil
com.aorise.common.utils.AppUserUtil

该类是获取当前登录用户的工具类

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

blues199

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值