博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第六篇: 分布式配置中心(Greenwich版本)
阅读量:4180 次
发布时间:2019-05-26

本文共 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/

你可能感兴趣的文章
SpringBoot | 配置logback-spring.xml
查看>>
SpringBoot | 第一章:构建第一个SpringBoot工程
查看>>
SpringBoot | 第二章:配置多环境以及上传文件
查看>>
Spring Data JPA |自定义非实体类的映射
查看>>
SpringBoot | 常用注解记录
查看>>
JavaBean对象转换EntityUtils工具类
查看>>
Maven常用命令
查看>>
SpringBoot | 运行报错,无法加载oracle连接驱动
查看>>
为什么阿里巴巴禁止在 foreach 循环里进行元素的 remove/add 操作
查看>>
AWS EC2如何从普通用户切换为root用户
查看>>
click方法不生效的
查看>>
mysql排行榜并列与不并列
查看>>
SpringBoot | Mybatis申明为Mapper文件
查看>>
JPA主键生成策略
查看>>
byte数组和InputStream的相互转换
查看>>
InputStream,InputStreamReader和Reader之间的区别与关系
查看>>
Java中System.arraycopy方法的使用
查看>>
tk.mybatis的使用记录
查看>>
遍历获取目录下的所有文件
查看>>
从指定服务器路径下载文件
查看>>