springcloud系列12 config的使用

config组件分为server端和client端

config的原理:

就是当我们将配置文件放置在git上面,那么configserver就会去拉取相关配置文件至本地:

可以看到我本地是拉去了配置文件:

讲讲整合案例吧:

首先需要在pom.xml文件引入pom依赖,然后在启动类上加入相关配置:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <!--千万注意,不要写成spring-cloud-netflix-eureka-client,写成这样不会报错,但是注册不了服务-->
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
</dependencies>

,然后启动类:

package com.cxy;

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

/***
 * @ClassName: a
 * @Description:
 * @Auther: cxy
 * @Date: 2019/7/18:22:53
 * @version : V1.0
 */
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class,args);
    }
}

可以看到需要加入到erueka中,所以要加入配置:

applictaion.yml

eureka:
  client:
    serviceUrl:
     defaultZone: http://admin:admin@127.0.0.1:8761/eureka/  #eureka注册中心地址
spring:
  application:
    name: config
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/chenxiufen/springcloudconfig
          username: 1508485087@qq.com
          password: cxy640612
          basedir: D:ideaspringcloud
server:
  port: 8085

可以看到配置:basedir 表示配置文件存放的目录,不能选取当前目录,如果选择项目为目录,那么就会将将项目进行与git同步,就清空代这样就server端就完成了但是在启动之前需要先启动eureka注册中心:

启动之后,我们可以通过浏览器取验证相关文件:

可以看到结尾不同那么就会出现不同.

注意事项:

比如我码云上有一个配置就是那个order.yml文件:

因为设置时候必须要加上环境:

可以随便加上一个就可以访问,因为源码规则这么定义的:

/{name}-{profile}.yml
/{label}/{name}-{profile}.yml

name 服务名

profile 环境

lable 分支(,brach)

如果使用第一种,默认使用的就是master


可以看到这个解释:
原文地址:https://www.cnblogs.com/xiufengchen/p/11217037.html