欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring cloud config 使用

程序员文章站 2022-07-15 08:57:49
...

参考: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. 添加配置文件
    spring cloud config 使用

  3. server端代码
    pox.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
</dependencies>
配置文件application.yml
server:
  port: 8888

spring:
  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
@EnableConfigServer
public 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-client
server:
  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
 */
@RestController
public class HelloController {
    @Value("${neo.hello}")
    private String sayHello;

    @RequestMapping("/hello")
    public String sayHello(){
        return this.sayHello;
    }
}

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

相关标签: spring-clo