spring cloud config 学习日记 使用SVN作为仓库

什么是spring cloud config?

我们开发多个产品或者项目的时候总会有很多配置文件,包括开发,测试,生产的时候就有不同的配置文件数据库要连接到不同的地址。那我们一般的情况就是在项目中直接使用配置文件,到时候发布版本的时候使用生产的配置文件,当然使用脚本自动打包一般情况下是不会出现配置文件弄错的情况。可能在打包服务器上需要保存很多的配置文件。

那spring cloud config解决了什么问题?我们可以将我们的配置文件放在git或者svn上进行方便的管理,可以创建spring cloud config server 让其他的应用可以读取到配置文件。那么对于项目来说可以读取指定的配置文件去加载项目,还能通过命令直接刷新项目的配置文件。

spring cloud config 有两个角色分为server 和client 。那很好理解了,client读取server的配置文件获取配置启动项目。


创建 spring cloud config server


        UTF-8
    

   
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Edgware.SR1
                pom
                import
            
        
    
    
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-config-server
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
        
            org.tmatesoft.svnkit
            svnkit
        
    

配置以上配置,增加 application.properties配置文件

server.port = 8761
#应用名称
spring.application.name=configserver
############SVN为配置中心####################
#指定配置中心使用svn管理
spring.profiles.active=subversion
#微服务svn配置中心
spring.cloud.config.server.svn.uri=https://127.0.0.1/svn/project/spring-cloud/configfile/
#svn repo 可访问的用户名密码
spring.cloud.config.server.svn.username=userName
spring.cloud.config.server.svn.password=password
#指定svn目录下的某个文件夹作为配置仓库  默认为trunk
spring.cloud.config.server.default-label=config

这边我们使用svn作为仓库,当然也可以使用git 作为仓库。那这边svn有几个参数需要注意,我在学习的时候也踩了坑,倒腾了好久才配置正常。

spring-cloud/configfile/是所有配置文件的根目录,其中可以放置其他label的文件夹,spring.cloud.config.server.default-label 这个参数配置的是config,那么到时候会加载spring-cloud/configfile/config下的配置文件

spring cloud config 学习日记 使用SVN作为仓库_第1张图片spring cloud config 学习日记 使用SVN作为仓库_第2张图片

通过图片大家就看的很明白了。

添加启动的类:

@SpringBootApplication
@EnableConfigServer
@EnableAutoConfiguration  
@ComponentScan({"com","tcm"})//设置扫描的包
public class ConfigMain {
    public static void main(String[] args) {
        SpringApplication.run(ConfigMain.class, args);
    }
}

添加@EnableConfigServer 作为 configServer启动

那么基本的环境就搭建好了,如何查看是否启动成功了?客户端是否能够读取到数据那么可以验证一下。可以通过访问properties文件:http://127.0.0.1:8761/cloud-config-demo.properties查看是否能够正常访问。

spring cloud config 学习日记 使用SVN作为仓库_第3张图片

大家可以看到spring cloud config server已经启动成功了。

那么客户端改如何配置呢,作为客户端那么肯定需要使用config 的client去访问config server的配置文件,那么项目需要添加配置文件:

        
            org.springframework.cloud
            spring-cloud-starter-config
        

配置文件添加如下配置,读取config server的配置文件,

spring.cloud.config.profile=web
spring.cloud.config.uri=http://127.0.0.1:8761
spring.cloud.config.name=cloud-config

以上配置会读取cloud-config-web.properties配置文件启动项目

如果修改svn中配置文件,然后通过/refresh 对项目进行请求,那么项目的配置文件就会刷新。

那么单个config server 启动是没有问题,那如何做config server的集群和高可用的呢?见下回文章。


更多成长经历分享请关注公众号:

spring cloud config 学习日记 使用SVN作为仓库_第4张图片

你可能感兴趣的:(spring,cloud)