SpringCloud Config Server 配置中心服务端工程

梦里梦外; 2022-05-16 06:25 300阅读 0赞

SpringCloud Config Server 配置中心服务端工程

概述

该工程是配置服务端工程,用于统一管理各个工程中的配置,进而达到配置文件与应用程序的解耦

实现步骤分析

引入相关依赖依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-config-server</artifactId>
  5. </dependency>
  6. </dependencies>

创建application.yml文件

  1. server:
  2. port: 7100
  3. spring:
  4. application:
  5. name: springcloud-config-server
  6. cloud:
  7. config:
  8. server:
  9. git:
  10. uri: https://github.com/myNameIssls/springcloud-study # git服务器上配置文件地址

注意:这里需要在相应的git服务器创建配置文件仓库,配置文件需要放置配置文件仓库的根目录

创建springcloud-config-server启动类

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.config.server.EnableConfigServer;
  4. @EnableConfigServer
  5. @SpringBootApplication
  6. public class Application {
  7. public static void main(String[] args) throws Exception {
  8. SpringApplication.run(Application.class, args);
  9. }
  10. }

配置文件访问规则

  1. /{application}/{profile}[/{label}]
  2. /{application}-{profile}.yml
  3. /{label}/{application}-{profile}.yml
  4. /{application}-{profile}.properties
  5. /{label}/{application}-{profile}.properties

测试

启动本项目在浏览器中访问:

  1. http://localhost:7100/application/dev
  2. http://localhost:7100/application/dev/master
  3. http://localhost:7100/application-dev.yml
  4. http://localhost:7100/master/application-dev.yml

结果便是git服务器上application-dev.yml的内容,结果如下图:
这里写图片描述

源代码地址:
https://github.com/myNameIssls/springcloud-study/tree/master/springcloud-config-server

参考链接:
http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__quick_start.html
http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_config_server.html
https://www.cnblogs.com/lfalex0831/p/9206605.html

发表评论

表情:
评论列表 (有 0 条评论,300人围观)

还没有评论,来说两句吧...

相关阅读