企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
## 一、构建Config Server 和Eureka Server一样,netflix出品的Config Server也是基于SpringBoot项目的。所以我们在spring-cloud新建一个module:zimug-server-config。 ![](https://img.kancloud.cn/52/ce/52cef7781a70419cb641bf4bcbc20394_489x493.png) Spring Boot项目构建完成之后,调整一下它的pom.xml。 * 让它的父项目为dongbb-cloud,这样可以继承父项目的版本号管理策略。 * 父项目dongbb-cloud新增module,`<module>zimug-server-config</module>` * 通过maven坐标引入关键类库:spring-cloud-config-server ~~~ <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>dongbb-cloud</artifactId> <groupId>com.zimug</groupId> <version>1.0</version> </parent> <groupId>com.zimug.dongbb.cloud</groupId> <artifactId>zimug-server-config</artifactId> <version>1.0</version> <name>zimug-server-config</name> <description>Spring Cloud Config Server</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> </project> ~~~ 在项目启动类上面加上`@EnableConfigServer`注解 ![](https://img.kancloud.cn/43/dd/43dd1034b030da96d4441bad4881fb7d_548x221.png) 在application.yml中进行config server的基本配置。 ~~~ server: port: 8771 #端口自定义 spring: application: name: zimug-server-config #config server项目名称 cloud: config: server: git: uri: https://gitee.com/hanxt/dongbb-cloud searchPaths: zimug-server-config-repo username: password: ~~~ * spring.cloud.config.server.git.uri:配置git仓库位置的http访问地址 * spring.cloud.config.server.git.searchPaths:配置仓库路径下的相对搜索位置,可以配置多个。因为我们上一节把配置文件放在了zimug-server-config-repo目录下,所以配置该目录 * spring.cloud.config.server.git.username:git仓库的用户名 * spring.cloud.config.server.git.password:git仓库的用户密码 ![](https://img.kancloud.cn/0e/91/0e913b273491bcd03b28f4773fe398f1_773x297.png) ## 二、config server访问测试 config server构建完成之后,我们可以通过浏览器URL访问测试,读取配置文件。其配置文件的读取与URL之间的映射关系如下: ~~~ /{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties ~~~ * {application} 就是应用名称,对应到配置文件上来,就是配置文件的名称部分,例如我上面创建的配置文件。 * {profile} 就是配置文件的版本,我们的项目有开发版本、测试环境版本、生产环境版本,对应到配置文件上来就是以 application-{profile}.yml 加以区分,例如application-dev.yml、application-sit.yml、application-prod.yml。 * {label} 表示 git 分支,默认是 master 分支,如果项目是以分支做区分也是可以的,那就可以通过不同的 label 来控制访问不同的配置文件了。 所以我们的这两个配置文件,可以通过如下的URL查看配置信息(以aservice-sms-dev.yml为例): ![](https://img.kancloud.cn/cd/07/cd07856d36fd85852058d9a244fe7e52_321x72.png) * [http://localhost:8771/aservice-sms-dev.yml](http://localhost:8771/aservice-sms-dev.yml) * [http://localhost:8771/master/aservice-sms-dev.yml](http://localhost:8771/master/aservice-sms-dev.yml) * [http://localhost:8771/aservice-sms/dev/master](http://localhost:8771/aservice-sms/dev/master) * [http://localhost:8771/aservice-sms/dev](http://localhost:8771/aservice-sms/dev) 通过访问以上地址,如果可以正常返回数据,则说明配置中心Config Server服务端一切正常。至此,说明config server和git远程仓库之间的配置同步已经通了(红色边框部分)。 ![](https://img.kancloud.cn/16/3b/163b9a55db891bccdf6de6e3f75506ac_721x283.png) 但是,大家可以明显的感觉到这里遗留了一个问题:那就是任何一个人都可以通过浏览器URL去访问任何一个项目的配置文件。关于Spring Cloud config配置的安全与权限管理做的肯定是没有apollo那么好,但是也是有一些可以自己实现的安全认证方式,否则就太不安全了。我们后面的章节会讲到。