美文网首页
配置中心

配置中心

作者: perfect_jimmy | 来源:发表于2017-05-19 09:55 被阅读114次
    • git仓库中创建一个文件夹cloud-config-repo,在文件夹下面创建三个properties:cloud-config-dev.properties,cloud-config-test.properties,cloud-default.properties,上传到gitlab远程仓库
    Paste_Image.png
    • 新建server端服务

    pom.xml添加依赖:

      <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</artifactId>
                <version>1.3.0.RELEASE</version>
       </dependency>
    

    目录结构如下:

    Paste_Image.png

    注意:
    1.bootstrap.propertis优先于application.properties加载
    2.bootstrap.propertis写不变的配置,application.properties写可变配置

    application.properties配置:

    #server port
    server.port=8087
    

    bootstrap.properties配置:

    #服务名字
    spring.application.name=CloudConfigServer
    #gitlab仓库地址
    spring.cloud.config.server.git.uri=http://xxx.xxx.xx.xx/xxx/testconfig.git
    #说明在此路径下搜索需要的配置
    spring.cloud.config.server.git.searchPaths=cloud-config-repo
    #版本
    spring.cloud.config.label=master
    #gitlab访问权限
    spring.cloud.config.server.git.username=user
    spring.cloud.config.server.git.password=admin
    

    代码:

    @SpringBootApplication
    @EnableConfigServer
    public class App 
    {
        public static void main( String[] args )
        {
             SpringApplication.run(App.class, args);
        }
    }
    

    关键注解:@EnableConfigServer

    访问:注意访问规则

    访问master上的test properties:http://localhost:8087/cloud-config/test

    Paste_Image.png

    访问master上的dev properties:http://localhost:8087/cloud-config/dev

    Paste_Image.png

    访问master上的default properties:http://localhost:8087/cloud/default

    Paste_Image.png

    访问1.0分支上的test properties:http://localhost:8087/cloud-config/test/1.0

    Paste_Image.png
    • 新建客户端服务

    pom.xml添加依赖

    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
            <version>1.3.0.RELEASE</version>
    </dependency>
    

    目录结构如下:

    Paste_Image.png

    application.properties配置:

    #server port
    server.port=8088
    

    bootstrap.properties配置:

    #服务名字
    spring.application.name=CloudConfigClient
    #配置中心服务地址
    spring.cloud.config.uri=http://localhost:8087/
    #
    spring.cloud.config.name=cloud-config
    #版本
    spring.cloud.config.label=master
    #
    spring.cloud.config.profile=test
    

    代码:

    服务启动:
    @SpringBootApplication
    @RestController
    public class AppClient{
        public static void main( String[] args ){
             SpringApplication.run(AppClient.class, args);
        }
    }
    访问示例:
    @RestController
    @RefreshScope
    public class HelloController {
        @Value("${test:default-value}")
        String str;
        
        @RequestMapping("/hello")
        public String hello(){
            return "Hello"+str;
        }
    }
    

    @Value("${test:default-value}")读取配置

    访问:

    Paste_Image.png

    实测:server端配置访问master,client配置访问1.0分支,测获取到的配置是1.0分支上的配置

    相关文章

      网友评论

          本文标题:配置中心

          本文链接:https://www.haomeiwen.com/subject/iaoexxtx.html