栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

五、服务配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

五、服务配置

一、Spring Cloud Config 分布式配置中心

Spring Cloud Config

1. 分布式系统面临的配置问题

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。

2. 是什么

Spring Cloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置;

2.1 服务端和客户端
  • 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口;
  • 客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。
3. 能干嘛
  1. 集中管理配置文件;
  2. 不同环境不同配置,动态化的配置更新,分环境部署(如:dev、test、prod、beta、release);
  3. 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取自己的配置信息;
  4. 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置;
  5. 将配置信息以REST接口形式暴露(post、curl访问刷新均可)。
4. Config Server

实现Config Server通过GitHub获取配置信息

4.1 新建Module-cloud-config-center3344 4.2 POM


    
        cloud-2020
        com.qs.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    cloud-config-center3344

    
        
        
            org.springframework.cloud
            spring-cloud-config-server
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
       
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    


4.3 YML(修改hosts文件)

修改hosts文件

# 添加映射配置
127.0.0.1 config3344.com
server:
  port: 3344

spring:
  application:
    # 注册进Eureka服务器的微服务名
    name:  cloud-config-center
  cloud:
    config:
      server:
        git:
          # Git地址
          uri: https://gitee.com/qshome/springcloud-config.git
          # 搜索目录
          search-paths:
            - springcloud-config
      # 读取分支
      label: master

eureka:
  client:
    service-url:
      # 注册中心地址
      defaultZone: http://localhost:7001/eureka
4.4 主启动
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {

    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterMain3344.class, args);
    }
}
4.5 测试

http://config3344.com:3344/master/config-dev.yml

5. 配置读取规则

/{label}/{application}-{profile}.yml(label分支、name服务名、profiles环境)

  • master分支
    http://config3344.com:3344/master/config-dev.yml
    http://config3344.com:3344/master/config-test.yml
    http://config3344.com:3344/master/config-prod.yml
  • dev分支
    http://config3344.com:3344/dev/config-dev.yml
    http://config3344.com:3344/dev/config-test.yml
    http://config3344.com:3344/dev/config-prod.yml

/{application}-{profile}.yml
http://config3344.com:3344/config-dev.yml
http://config3344.com:3344/config-test.yml
http://config3344.com:3344/config-prod.yml

/{application}/{profile}[/{label}]
http://config3344.com:3344/config/dev/master
http://config3344.com:3344/config/test/master
http://config3344.com:3344/config/test/dev

6. Config Client

实现客户端3355访问Config Server 3344通过GitHub获取配置信息

6.1 新建Module-cloud-config-client3355 6.2 POM


    
        cloud-2020
        com.qs.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    cloud-config-client3355

    
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    


6.3 YML(bootstrap.yml)
  • bootstrap.yml是系统级的,优先级更加高;
  • applicaiton.yml是用户级的资源配置项;
  • Spring Cloud会创建一个"Bootstrap Context",作为 Spring应用的 Application Context的父上下文。初始化的时候,Bootstrap Context负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment。
  • Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap context和Application Context有着不同的约定,所以新增了一个bootstrap.yml文件,保证Bootstrap Context和Application Context配置的分离。
server:
  port: 3355

spring:
  application:
    name: cloud-config-client
  cloud:
    # Config客户端配置http://config3344.com:3344/master/config-dev.yml
    config:
      # 分支名称
      label: master
      # 配置文件名称
      name: config
      # 读取后缀名称
      profile: dev
      # 配置中心地址
      uri: http://localhost:3344

eureka:
  client:
    service-url:
      # 注册中心地址
      defaultZone: http://localhost:7001/eureka
6.4 主启动
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientMain3355.class, args);
    }
}
6.5 ConfigClientController
@RestController
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo() {
        return configInfo;
    }
}
6.6 测试

http://localhost:3355/configInfo

7. Config Client 动态刷新
  • 分布式配置的动态刷新问题
  1. 运维修改GitHub上的配置文件内容;
  2. 刷新3344,发现Config Server配置中心立刻响应;
  3. 刷新3355,发现Config Client客户端没有任何响应;
  4. 3355没有变化除非自己重启或者重新加载;
7.1 修改Module-cloud-config-client3355 7.2 POM

引入actuator监控


    org.springframework.boot
    spring-boot-starter-actuator

7.3 YML(bootstrap.yml)
# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
7.4 修改ConfigClientController

@RefreshScope

@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo() {
        return configInfo;
    }
}
7.5 手动刷新

curl -X POST “http://localhost:3355/actuator/refresh”

7.6 测试

http://config3344.com:3344/master/config-dev.yml ok
http://localhost:3355/configInfo ok

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/325114.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号