springcloud config 配置中心,读取本地配置文件

1、环境&版本:

       spring.boot.version = 1.5.4.RELEASE

       spring.cloud.version = 1.3.1.RELEASE

       jdk.version = 1.8

2、服务端搭建(demo-springcloud-config-server):

      pom.xml 添加如下依赖:     

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

     创建启动类:Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableConfigServer
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

    修改配置文件:application.properties

server.context-path=/
server.port=8888

# 读取本地配置文件
spring.profiles.active=native
# 自动抓取src/main/resources/config/文件夹下的配置
spring.cloud.config.server.native.searchLocations=classpath:/config

    在config文件夹下创建config-client-dev.properties 和 config-client-product.properties,内容分别如下:

    config-client-dev.properties

env.name=dev
env.password=dev123456

   config-client-product.properties

env.name=product
env.password=product123456

   启动服务(运行Application.java)

   访问http://localhost:8888/config-client/product,结果如下:

{
  "name": "config-client",
  "profiles": [
    "product"
  ],
  "label": null,
  "version": null,
  "state": null,
  "propertySources": [
    {
      "name": "classpath:/config/config-client-product.properties",
      "source": {
        "env.password": "product123456",
        "env.name": "product"
      }
    }
  ]
}

    访问http://localhost:8888/config-client-product.properties,结果如下:

env.name: product
env.password: product123456

     以上两种方式通用,都可以被客户端识别;修改config-client-product.properties文件,刷新上面两个url,都可立即看到变化;

    配置匹配规则:

/{application}/{profile}[/{label}]

/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.yml
/{label}/{application}-{profile}.properties

     其中:

application=config-client
profile=product
lable=""

     也可以认为是:

application=config
profile=client-product
lable=""

3、客户端搭建(demo-springcloud-config-client):     pom.xml 添加如下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

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

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

     创建启动类(Application.java):

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

     创建Controller:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class HomeController {

    @Value("${env.name:World!}")
    private String bar;

    @RequestMapping("/app")
    String hello() {
        return "Hello " + bar + "!";
    }
}

     添加配置文件(application.properties):

server.context-path=/
server.port=8080

# default true
management.security.enabled=false
endpoints.restart.enabled=true
spring.application.name:config-client
spring.cloud.config.env:dev
spring.cloud.config.profile:dev
spring.cloud.config.uri:http://localhost:8888

     启动服务(运行Application.java),访问http://localhost:8080/app(需要服务端开启),可以读取在server的配置了。如果服务端配置发生变化,可以在客户端刷新,获取最新配置,POST请求接口:http://localhost:8080/refresh,客户端配置生效。
源码:https://github.com/xiaojianhx/demo-springcloud

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值