Spring Cloud教程 | 第四篇:远程配置中心账号密码设置以及高可用 | Spring Cloud Config

一、前言

第三篇中,我们搭建了远程配置中心项目,实现了客户端配置文件从git上拉取。远程配置中心保存各种敏感配置信息,需要通过账号密码配置来保护我们的配置数据的安全。

Spring Cloud版本 Spring Boot版本 JAVA IDE
Greenwich.SR1 2.1.6.RELEASE 1.8 IDEA

 

涉及项目:

alh-config-server(6001)

alh-eureka-server(6000)

完整教程全部章节目录:https://blog.csdn.net/tuoyun6647/article/details/93501029

 

二、Config Server配置

打开上篇创建的alh-config-server项目,引入spring-boot-starter-security依赖

        
            org.springframework.boot
            spring-boot-starter-security
        

完整pom如下



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.6.RELEASE
         
    
    com.kcsm.alh
    alh-config-server
    0.0.1-SNAPSHOT
    alh-config-server
    Config Centre

    
        1.8
        Greenwich.SR1
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-config-server
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-security
        
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


application.properties增加账号密码配置

spring.application.name=alh-config-server
server.port=6001

# 配置git仓库地址
spring.cloud.config.server.git.uri=https://code.aliyun.com/kcms-alh/alh-config-server.git
# 配置仓库的分支
spring.cloud.config.label=master
# 配置仓库路径
spring.cloud.config.server.git.search-paths=config-repos
# 访问git仓库的用户名
spring.cloud.config.server.git.username=xxxxx
# 访问git仓库的用户密码
spring.cloud.config.server.git.password=xxxxxxxx

#配置中心访问权限账号密码配置
spring.security.user.name=kcsm
spring.security.user.password=kcsm111

启动项目,访问http://localhost:6001/alh-eureka-server-dev.properties时需输入账号密码,此时服务端配置生效

 

三、Config Client配置

客户端配置文件中远程配置中心的连接地址增加远程配置中心的账号密码即可进行连接。alh-eureka-server项目的配置文件如下:

# 和git里的文件名对应
spring.application.name=alh-eureka-server
# 远程仓库的分支
spring.cloud.config.label=master
# dev开发环境、test测试环境、pro正式环境.local本地环境
spring.cloud.config.profile=dev

# 指明配置服务中心的网址
spring.cloud.config.uri= http://kcsm:kcsm111@localhost:6001/

固定格式为:http://配置中心账号:配置中心密码@配置中心地址/

 

四、Config Server高可用

思路:将其Config Server做成微服务,注册到服务注册中心,将其集群化,从而达到高可用。当其他配置客户端连接时,只需连接Config Server的服务名即可(alh-config-server)。 同时也意味着配置客户端需要从注册中心上拉取远程配置中心,因此注册中心的地址等参数只能相对写死(放在本地环境变量或者直接在配置文件中写)。

考虑远程配置中心并不是实时业务用的微服务,为了便于所有参数可统一由配置中心管理(包括服务注册中心参数),因此目前不搭建Config Server高可用。要了解的同学可参考https://blog.csdn.net/forezp/article/details/81041045

你可能感兴趣的:(Spring,Cloud)