一、简介
前面归纳了一些Spring Cloud组件的核心原理及用法,主要是开发这一块的Eureka、Ribbon、Feign、Hystrix、Zuul、Config等组件和其相关的扩展。当然Spring Cloud还有一些偏运维的zipkin(Sleuth 服务链路追踪)、Hystrix Dashboard/Turbine(断路器监控方面)等等组件,可以用来查看服务之间的调用、异常啊等功能,后面有时间了再回来梳理。
这里主要根据开发组件,讲解企业里的Spring Cloud+Mybatis(mysql多数据源,分页插件的用法、jasypt的加密等)的架构体系
本章案例 源码下载:链接: https://pan.baidu.com/s/1OjdiUiD5ZfUd4F2YTeT7Sg 提取码: n99m
二、项目结构
本章先主讲正常够用的,通过mybatis持久mysql库的结构,后面再逐步补充定时器任务xxl-job工程,及Redis、MongoDB、Elasticsearch等的结合使用。
话不多说,先上结构图:
三、注册中心
该工程中,注册中心配置了用户安全认证,代码可以看源码,详细解说可以回顾一下《Spring Cloud教程 | 第一篇:Eureka 服务的注册与发现(Eureka Server与Eureka Client)》、《Spring Cloud教程 | 第二篇:Eureka Server的用户安全认证》,后面访问量增大,需要集群也可以回顾《Spring Cloud教程 | 第九篇:Eureka高可用的注册中心——集群配置》。
这里简单说下注意点:
pom.xml记得加安全认证的依赖包
<!--加入服务认证(密码),需要引入security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
application.yml文件配置认证密码、注册地址等
# 配置端口
server:
port: 9000
# 配置注册中心服务配置
eureka:
server:
enable-self-preservation: false # 关闭自我保护模式(缺省为启动,比如api提供者关闭了,但是eureka仍然会保留)
eviction-interval-timer-in-ms: 10000 # 清理间隔(毫秒,默认 60秒),将有问题的api提供者或app消费者剔除掉
instance:
hostname: localhost
prefer-ip-address: true
client:
#声明自己是服务端
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://root:123456@${eureka.instance.hostname}:${server.port}/eureka/
# 配置中心命名
spring:
application:
name: eureka-server
# 安全认证的配置
security:
basic:
enabled: true #开启认证
user:
name: root #用户名
password: 123456 #密码
关闭csrf:
前面第三篇讲的是新建一个配置类WebSecurityConfig 继承 WebSecurityConfigurerAdapter,这里我们直接在Application启动类中写个内置类就好了
@SpringBootApplication
// 注册中心服务注解
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
/**
* 开启注册中心安全认证(这里写一个内置类即可,也可单独新建一个类)
*/
@EnableWebSecurity
static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable(); //关闭csrf
http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); //开启认证
}
}
}
四、配置中心
该工程,大致与《Spring Cloud教程 | 第七篇:高可用的分布式配置中心(Spring Cloud Config)之本地VS远程Git仓库》相同,pom.xml依赖包后,Application启动类中的@EnableConfigServer注解,剩下的就是application.yml中配置注册中心和远程git仓库地址的配置了(如果想config有需要事实刷新的,可以安照《Spring Cloud教程 | 第八篇:消息总线(Spring Cloud Bus)下的及时刷新》,结合rabbitMQ或kafka等消息线刷新)
建议使用git仓库地址就好了
server:
port: 8000
eureka:
client:
serviceUrl:
defaultZone: http://root:123456@localhost:9000/eureka/
instance:
prefer-ip-address: true
spring:
application:
name: config-server #应用名
cloud:
config:
label: master # 配置仓库的分支
server:
git:
uri: https://github.com/liujun19921020/SpringCloudDemo.git # 配置git仓库地址
searchPaths: ConfigDemo # 配置仓库路径
username: # 访问git仓库的用户名
password: # 访问git仓库的用户密码
native:
search-locations: D:/javaProject/ConfigDemo/ #本地配置文件位置
五、通用方法集合
cloud-commons这个模块没啥好讲的,就是集中一些在项目中都会用到的工具、方法,在需要用到的项目工程中打包成jar集成使用。
例1:集成的commons-http目录下,存放一些常用的response,拥有接口返回的常量,或者json对象格式化等
和:
例2:集成的commons-utils目录下,存放一些常用的类辅助工具等。
Jasypt是一个加解密工具,这里主要做config文件中保存的数据库账号、密码加解密的作用,会在项目工程模块讲解的。
六、项目工程集合
因为cloud-modules中会集成很多小的项目工程,我们将项目工程常用的feign、Mybatis Plus、Mysql Driver、alibaba 数据库连接池、Log4j、springboot分页插件、fastJSON、jasypt加密配置......等等组件加载到其pom.xml中,避免里面的每个小项目要再配一遍的重复动作,具体组件可根据实际情况增删。
<!--配置中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</exclusion>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- SpringBoot把容器修改为Jetty 适合长连接应用 默认Tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--feign应用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<exclusions>
<exclusion>
<artifactId>HdrHistogram</artifactId>
<groupId>org.hdrhistogram</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- Mybatis Plus