本文共 6238 字,大约阅读时间需要 20 分钟。
一、新建个项目,并创建个父工程。
pom.xml内容如下:
4.0.0 com.example cloud-config 0.0.1-SNAPSHOT jar cloud-config cloud-config project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.1.0.RELEASE config-server config-client UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
二、创建个config-server子工程并继承父工程
2.1、pom.xml的配置如下:
4.0.0 config-server 0.0.1-SNAPSHOT jar config-server config-server project for Spring Boot com.example cloud-config 0.0.1-SNAPSHOT UTF-8 UTF-8 1.8 Greenwich.M1 org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone false
2.2、application.properties文件配置如下:
server.port=8007spring.application.name=config-server#github地址spring.cloud.config.server.git.uri=https://github.com/xiaobu1994/SpringCloudConfig/#分支spring.cloud.config.label=master#仓库路径spring.cloud.config.server.git.search-paths=respo#账户名和密码spring.cloud.config.server.git.username=spring.cloud.config.server.git.password=
2.3、ConfigServerApplication 文件配置如下:
package com.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;/** * @author xiaobu EnableConfigServer开启配置服务器的功能 */@EnableConfigServer@SpringBootApplicationpublic class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); }}
2.4、访问 出现如下效果
三、创建另外个子工程config-client
3.1、 pom.xml配置如下:
4.0.0 config-client 0.0.1-SNAPSHOT jar config-client config-client project for Spring Boot com.example cloud-config 0.0.1-SNAPSHOT UTF-8 UTF-8 1.8 Greenwich.M1 org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone false
3.2、bootstrap.properties配置如下(必须是以bootstrap命名,它比application.properties要先加载):
server.port=8008#bootstrap和application 先加载bootstrap 再加载application。 切忌在这个地方要用bootstrap#最终访问的文件为${git.uri}/${spring.application.name}-${spring.cloud.config.profile}.properties#即https://github.com/xiaobu1994/SpringCloudConfig/config-client-test.propertiesspring.application.name=config-client#配置环境spring.cloud.config.profile=test#分支spring.cloud.config.label=master#配置服务中心网址spring.cloud.config.uri=http://localhost:8007/
3.3、ConfigClientApplication文件定义如下:
package com.example;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@SpringBootApplicationpublic class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); }@Value("${name}")String name;@RequestMapping("/getResult")public String getResult(){ return name;}}
该文件的内容为:
name = xiaobu version1.0
3.4、访问出现如下结果:
转载地址:http://zygai.baihongyu.com/