spring cloud config-配置中心

1.新建远程git创库,如下

2.配置中心服务端config-server

需要的依赖:

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

启动类注释

/**
 * by maohx
 * 配置 服务
 */
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigApplication {

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

添加配置

server:
  port: 8004

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8001/eureka/

spring:
  zipkin:
    base-url: http://localhost:9411
  sleuth:
    sampler:
      probability: 1.0

  cloud:
    config:
      server:
        git:
          #配置git仓库地址
          uri: http://admin@192.168.28.130:81/r/demo-config-repo.git
          #配置git仓库账号
          username: maohx
          #配置git仓库密码
          password: 123456aB
          #指定搜索根路径下的所有目录,若有多个路径使用逗号分隔
          search-paths: /**

启动服务,测试

http://localhost:8004/config-client/dev

<Environment>
<name>config-client</name>
<profiles>
<profiles>dev</profiles>
</profiles>
<label/>
<version>b069d06ffe85f7004ba5c2adf36905706b1e65bd</version>
<state/>
<propertySources>
<propertySources>
<name>
http://admin@192.168.28.130:81/r/demo-config-repo.git/config-client-dev.yml
</name>
<source>
<param.test>hello dev !</param.test>
</source>
</propertySources>
</propertySources>
</Environment>

或者

http://localhost:8004/config-client-dev.yml

param:
  test: hello dev !

3.配置客户端,config-client

添加依赖

        <!--config-client-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--config-client-->

添加配置,

特别注意:下面这些与 Spring Cloud Config 相关的配置属性必须配置在 bootstrap.yml 中,config 部分内容才能被正确加载。因为 config 的相关配置会先于 application.yml,而 bootstrap.yml 的加载也是先于 application.yml

spring:
  cloud:
    config:
      # 配置中心的具体地址,即 config-server
      uri: http://localhost:8004
      # 对应配置文件config-client-dev.yml的 {application} 部分
      name: config-client
      # 对应 {profile} 部分
      profile: dev
      # 对应 {label} 部分,即 Git 的分支(默认是master)。如果配置中心使用的是本地存储,则该参数无用
      label: master

启动类

@EnableEurekaClient
@SpringBootApplication
public class TestApplication {

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

在 Controller 中使用@Value注解来获取 Server 端参数的值

package com.test.controller;

import com.common.model.Result;
import com.common.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping(value = "/config_client", produces = "application/json;charset=UTF-8")
@Api("配置中心读取")
public class ConfigClientController {

    @Value("${param.test:error}")
    private String test;

    /**
     * by maohx
     * 读取配置中心配置信息
     * @return 读取配置中心配置信息
     * @throws Exception e
     */
    @ApiOperation(value = "读取配置中心配置信息", notes = "读取配置中心配置信息")//描述方法用途
    @GetMapping(value = "/test")
    public Result param() throws Exception{
        log.info("test=" + test);
        return ResultUtil.success("test=" + test);
    }

}

测试

http://localhost:8003/config_client/test

posted @ 2020-07-10 11:20  mhx_pyw  阅读(160)  评论(0)    收藏  举报