Spring-cloud 微服务架构搭建 02 – config-server 集成git动态刷新配置及安全管理

释放双眼,带上耳机,听听看~!

文章目录

    1. sping-cloud config简介
        1. sping-cloud config 服务特点
        1. Config-Server 服务端搭建
        1. Config-Client 端搭建
        1. 动态刷新配置测试
        1. config-server配置RSA加密

1. sping-cloud config简介

微服务的体系中,配置文件的统一管理是非常有必要的,我们需要替代人为手动维护配置文件的责任,因为在大型的微服务体系中我们需要维护的配置成百上千份,很容易发生配置漂移。此时,Spring-Cloud Config可以给我们提供配置的统一管理和分发,以及自动化刷新配置的需求。

2. sping-cloud config 服务特点

2.1. 分离性

Config配置中心支持将配置文件存入本地或者无缝衔接git管理的特性能够集中进行版本的控制和配置文件的管理;

2.2. 抽象性

我们在获取配置文件时,不需要自己进行配置文件读取操作,可以通过http请求配配置中心提供的服务进行配置文件读取,并且集成bus总线可以动态IDE刷新配置,并且刷新所有的服务实例无需重启机器;

2.3. 集中性

所有的配置文件信息集中存储在配置中心,有效的进行数据的版本统一管理。

2.4. 稳定性

作为spring旗下项目,与eureka、consul等紧密结合,可实现高可用部署,降低服务奔溃的风险,实现拉取一份配置文件本地缓存等特性来降低风险,保证了高可用和冗余。

3. Config-Server 服务端搭建

注:本文项目采用idea工具进行搭建

  • 使用idea自身的spring initializr进行项目的初始化,集成git使用的rabbitmq请自行搜索安装,如果是mac,请直接使用brew install rabbitmq即可

  • 初始化完成项目之后进行pom文件导入


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1<!-- config-server 启动引入 -->
2<dependency>
3    <groupId>org.springframework.cloud</groupId>
4    <artifactId>spring-cloud-config-server</artifactId>
5</dependency>
6<!-- security 安全控制 -->
7<dependency>
8    <groupId>org.springframework.boot</groupId>
9    <artifactId>spring-boot-starter-security</artifactId>
10</dependency>
11<!-- eureka 服务注册 -->
12<dependency>
13    <groupId>org.springframework.cloud</groupId>
14    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
15</dependency>
16<!-- 消息总线 配置动态配置中心 -->
17<dependency>
18    <groupId>org.springframework.cloud</groupId>
19    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
20</dependency>
21
22
  • 修改application.yml文件,添加如下配置:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
1spring:
2  application:
3    name: config-server
4#  profiles:
5#    active: native    #开启本地配置 默认为git
6#  cloud:
7#    config:
8#      server:
9#        native:
10#          search-locations: classpath:config-repo
11## git管理配置
12  cloud:
13    config:
14      retry:
15        initial-interval: 3000
16        multiplier: 1.5
17        max-interval: 20000
18        max-attempts: 6
19      server:
20        git:
21          uri: #个人git地址
22          search-paths: '{application}'
23          username: # 你个人的git账户
24          password: # git密码
25          default-label: master
26  rabbitmq:
27    host: localhost
28    port: 5672
29    username: guest #默认密码
30    password: guest #默认密码
31  # 安全认证
32  security:
33    user:
34      name: luhanlin
35      password: ***
36      
37## 暴露所有监控端口 主要暴露动态刷新接口
38management:
39  endpoints:
40    web:
41      exposure:
42        include: "*"
43  endpoint:
44    health:
45      show-details: ALWAYS
46
47
  • 修改bootstrap.yml文件,链接eureka-config,添加如下配置:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1#服务注册中心配置
2eureka:
3  client:
4    serviceUrl:
5      defaultZone: http://localhost:8761/eureka/  #,http://xxxxx:8761/eureka/
6      # 指定多少秒从注册中心获取一次实例服务列表 默认 30秒
7      # 减少值可以解决服务注册慢问题,但一般不要设置太小
8      registry-fetch-interval-seconds: 20
9  instance:
10    # 获取实例ip地址
11    prefer-ip-address: true
12    # 心跳包发送时间 默认 30秒
13    lease-renewal-interval-in-seconds: 60
14    # 最后一次心跳时间间隔以下值之后清楚实例列表中的值,不应该小于心跳检测时间 默认90秒
15    lease-expiration-duration-in-seconds: 100
16#    instance-id: config-server
17
18
  • 最后在Config服务启动实例上面添加一下注解:


1
2
3
4
5
6
7
8
9
10
11
1@EnableDiscoveryClient  // 开启服务注册
2@EnableConfigServer     // 开启配置中心服务端
3@SpringBootApplication
4public class ConfigServerApplication {
5
6    public static void main(String[] args) {
7        SpringApplication.run(ConfigServerApplication.class, args);
8    }
9}
10
11

最后启动Eureka—server,在启动Config-Server就可以了,注意:rabbitmq服务需要启动,不然会报错。

4. Config-Client 端搭建

  • 此处我以Demo-Service项目为例。

  • 首先我们构建一个maven工程,初始化完成后进行pom引入(没有加入boot监控依赖):


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1<!-- Eureka客户端启动类 -->
2<dependency>
3    <groupId>org.springframework.cloud</groupId>
4    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
5</dependency>
6<!-- Config客户端启动类 -->
7<dependency>
8    <groupId>org.springframework.cloud</groupId>
9    <artifactId>spring-cloud-config-client</artifactId>
10</dependency>
11<!-- 消息总线 配置动态配置中心 -->
12<dependency>
13    <groupId>org.springframework.cloud</groupId>
14    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
15</dependency>
16
17
  • 修改我们的配置文件Bootstrap.yml, 配置如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
1spring:
2  application:
3      name: demo
4  # 配置中心服务的地址
5  cloud:
6    config:
7      discovery:
8        # 默认false,设为true表示使用注册中心中的config-server配置而不自己配置config-server的uri
9        enabled: true
10        service_id: config-server
11      fail-fast: true
12      name: demo
13      profile:  dev # 要读取的配置文件profile属性
14      # 使用git管理配置文件时指定
15      label: master
16      username: luhanlin
17      password: ***
18# 指定服务注册中心的位置。
19eureka:
20  client:
21    serviceUrl:
22      defaultZone: http://localhost:8761/eureka/
23  instance:
24    hostname: localhost
25    preferIpAddress: true
26management:
27  endpoints:
28    web:
29      exposure:
30        include: "*"
31  endpoint:
32    health:
33      show-details: ALWAYS
34
35
  • 配置完后需要在Client启动类上加上一下注解:


1
2
3
4
1@RefreshScope   # GIT动态刷新注解
2@EnableDiscoveryClient # 服务发现
3
4

5. 动态刷新配置测试


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
1// 此类进行配置文件的信息读取,**使用对象进行测试会更加准确**
2@Data
3public class CustomBean {
4    private String id;
5    private String name;
6    private String version;
7}
8
9@Configuration  // 配置类,如不熟悉可以先行进行spring-b
10public class BusinessConfig {
11
12    @Bean
13    @ConfigurationProperties(prefix="custom.bean")
14    public CustomBean initCustomBean(){
15        log.info(">>>>>>>>>>> CustomBean >>>>>>>>>>>>>>> 配置成功!!!");
16        return new CustomBean();
17    }
18}
19
20@RestController  // 对外api提供,最后返回配置中信息
21@RequestMapping(value = "/test")
22public class TestController {
23
24    @Autowired
25    private CustomBean customBean;
26
27    @GetMapping(value = "/hello")
28    public ResultInfo hello(){
29        return  ResultUtil.success(customBean.getVersion());
30    }
31}
32
33// 配置文件(demo-dev.yml,位于个人仓库中)添加如下配置:
34custom:
35  bean:
36    id: 100
37    name: luhanlin
38    version: 1.0.1
39
40

1
2
3
4
5
6
7
1{
2    "code": "I0000",
3    "msg": "请求成功",
4    "data": "1.0.1"
5}
6
7
  • 修改配置仓库中的配置文件,并提交到push到远程git仓库


1
2
3
4
5
6
7
1custom:
2  bean:
3    id: 100
4    name: luhanlin
5    version: 1.0.9
6
7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1@Configuration
2@EnableWebSecurity
3public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
4
5    /**
6     * 高版本的丢弃了
7     *
8     * security:
9     *   basic:
10     *    enabled: true
11     *
12     * 配置,应该使用以下方式开启
13     *
14     * @param http
15     * @throws Exception
16     */
17    @Override
18    protected void configure(HttpSecurity http) throws Exception {
19        // Configure HttpSecurity as needed (e.g. enable http basic).
20        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
21        http.csrf().disable();
22        //注意:为了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 这种方式登录,所以必须是httpBasic,
23        // 如果是form方式,不能使用url格式登录
24        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
25    }
26}
27
28

1
2
3
4
5
6
7
1{
2    "code": "I0000",
3    "msg": "请求成功",
4    "data": "1.0.9"
5}
6
7
  • 配置成功

6. config-server配置RSA加密

  • 由于我们配置文件时很多需要加密的信息不宜暴露给外界,需要进行各种加密操作,其中spring-cloud提供了对称加密和RSA非对称加密的形式,此处我们使用RSA加密的方式,通过Config暴露的entrypt接口进行加密,我们只需要在配置文件中使用{cipher},如:“{cipher}shdshdswwhxxxxxxxx66x65xsdsdsd”,其中在yml配置文件中一定要使用双引号包含起来,不然会出错,详细的RSA配置大家可以自行搜索博客进行配置。

本文github代码地址
spring-cloud 基础模块搭建 – 欢迎指正

给TA打赏
共{{data.count}}人
人已打赏
安全经验

Google AdSense 全面解析(申请+操作+作弊+忠告)

2021-10-11 16:36:11

安全经验

安全咨询服务

2022-1-12 14:11:49

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索