spring cloud config 使用

参考: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: 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值