spring cloud config 使用

来源:互联网 发布:淘宝网络环境异常 编辑:程序博客网 时间:2024/04/30 10:50

参考:http://www.ityouknow.com/springcloud/2017/05/22/springcloud-config-git.html


server

  1. 创建保存配置文件的git仓库
    https://gitee.com/jiyangM/spring-cloud-config/tree/master/config
  2. 添加配置文件
    这里写图片描述

  3. server端代码
    pox.xml

<dependencies>    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-config-server</artifactId>    </dependency></dependencies>
配置文件application.yml
server:  port: 8888spring:  cloud:    config:      server:        git:          uri: https://gitee.com/jiyangM/spring-cloud-config          search-paths: config          username: 18600884376@163.com          password: 1993Jiyang
/** * Created by jiyang on 15:16 2017/12/15 */@SpringBootApplication@EnableConfigServerpublic class Application {    /**     * application.java 文件不能直接放在main/java文件夹下,必须要建一个包把他放进去     * @param args     */    public static void main(String[] args){        SpringApplication.run(Application.class,args);    }}

启动,测试,访问 :
http://localhost:8888/neo-config/test
会返回配置信息。

注意: neo-config/test 对应git仓库中的neo-config-test.yml文件


client

pom.xml

<dependencies>    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-config</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-test</artifactId>        <scope>test</scope>    </dependency></dependencies>

application.yml

spring:  application:    name: spring-cloud-config-clientserver:  port: 8002

bootstrap.yml

spring:  cloud:    config:      name: neo-config      profile: dev      uri: http://localhost:8888/      label: master

测试类

/** * Created by jiyang on 12:16 2017/12/19 */@RestControllerpublic class HelloController {    @Value("${neo.hello}")    private String sayHello;    @RequestMapping("/hello")    public String sayHello(){        return this.sayHello;    }}

启动,测试,访问 :http://localhost:8002/hello