一.创建服务端Config模块

1.导入依赖

        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--config server依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>3.1.2</version>
        </dependency>

2.编写主启动类

注:记得添加@EnableConfigServer注解以开启ConfigServer服务

@SpringBootApplication
@EnableConfigServer//开启ConfigServer服务
public class Config_Server_3001 {
    public static void main(String[] args) {
        SpringApplication.run(Config_Server_3001.class,args);
    }
}

3.配置yml文件

#从Git上读取配置文件
server:
  port: 3001
spring:
  application:
    name: springcloud-config-server
  cloud:
    config:
      server:
        git:
          uri: git的http连接地址

4.读取远程配置文件

配置文件名为application.yml

访问格式

# 访问格式
#  /{application}/{profile}[/{label}]
#  /{application}-{profile}.yml
#  /{label}/{application}-{profile}.yml
#  /{application}-{profile}.properties
#  /{label}/{application}-{profile}.properties

 访问 http://localhost:3001/application-dev.yml

访问 http://localhost:3001/application-test.yml

可以看到分别访问了两套环境的配置

二.创建客户端Config模块

1.导入依赖

        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--config client依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
            <version>3.1.2</version>
        </dependency>
        <!--bootstrap依赖-->
        <!--springcloud2020.x.x之后的版本需要添加此依赖 因为官方重构了bootstrap引导配置加载的方式-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.1.2</version>
        </dependency>

        <!--热部署依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>2.6.7</version>
        </dependency>

2.编写主启动类

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

3.编写Controller层(用于展示通过服务端获取到的配置信息)

@RestController
public class ConfigClientController {
    @Value("${server.port}")
    private String port;
    @Value("${spring.application.name}")
    private String applicationName;
    @Value("${eureka.client.service-url.defaultZone}")
    private String eurekaServer;

    @RequestMapping("/Config")
    public String getConfig(){
        return "port: "+ port + "<br>" + "applicationName: " + applicationName + "<br>" + "eurekaServer: " + eurekaServer;
    }
}

4.配置yml文件

bootstrap.yml优先级高于application.yml

bootstrap.yml 应用于早期配置信息读取(系统级别配置)。一旦bootstrap.yml 被加载,其则内容将不可覆盖

bootstrap.yml

#系统级配置
#在Config Client中用于配置Config Server的相关配置
spring:
  cloud:
    config:
      uri: http://localhost:3001 #连接Git的server端口
      name: config-client #读取的配置文件名 无需后缀
      profile: dev #读取是何环境下的配置
      label: master #分支

application.yml

#用户级配置
spring:
  application:
    name: springcloud-config-client

远程配置文件

由于在bootstrap.yml中设置读取dev环境下的配置,此时应访问3101端口

可以看到访问成功,并正确返回了相关配置

若是访问3102端口呢?

显然无法访问,因为该端口的配置并未生效

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐