spring-cloud-config 搭建-入门级(一)

栏目: Java · 发布时间: 6年前

内容简介:我的git上的路径:
工程名 作用 端口
config-server config的配置服务端 配置了8080dev, 8082prd
config-client 为了测试调用的demo 8889
eureka-provider 实际项目演练的工程 配置了8080dev, 8082prd
eureka-server eureka 的服务注册中心 8761

本文github地址

远端配置文件

github.com/xueshuiyy/s…

配置client和server端

github.com/xueshuiyy/s…

明确远端配置文件工程的路径和文件名

我的git上的路径:

spring-cloud-config 搭建-入门级(一)

DEMO 搭建过程

父工程

父工程引入server和client都需要的starter-config

<!-- 引入spring cloud 的config依赖 -->
<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
复制代码

必须要指定springcloud的版本!

<!-- 依赖管理 -->
<!-- spring-cloud-version 在properties里配置:
<spring-cloud-version>Greenwich.SR1</spring-cloud-version>
-->
<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>${spring-cloud-version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>
复制代码

Server端

1. 在pom中 指定好父工程

<parent>
	<groupId>lin.springcloud</groupId>
	<artifactId>spring-cloud-config-lin</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>
复制代码

2. 依赖config-server

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-config-server</artifactId>
</dependency>
复制代码

主类添加@EnableConfigServer注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

}
复制代码

配置文件

server.port: 8888

# 配置git仓库地址
spring.cloud.config.server.git.uri=https://github.com/xueshuiyy/spring-cloud-config-center.git
# 配置仓库路径,下图圈红的就是这个文件夹名
spring.cloud.config.server.git.search-paths=clientconfig
# 配置仓库的分支
spring.cloud.config.label=master
# 访问git仓库的用户名
spring.cloud.config.server.git.username=xueshuiyy
# 访问git仓库的用户密码 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写
spring.cloud.config.server.git.password=
复制代码

Client端

配置依赖

由于父工程已经配置了 spring-cloud-starter-config 的依赖了,所以Client就不需要在配置了。只是需要用rest测试一下,所以引入web的依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
复制代码

配置文件

做一个bootstrap.yml(properties)的文件,application.properties文件就可写可不写了,bootstrap 的配置文件会覆盖掉application的配置文件。联想到双亲委派机制(向上委托,向下加载),bootstrap-> extend -> app

server.port=8889
# 和git里的文件名对应,下图的圈红
spring.application.name=config-client
# 远程仓库的分支
spring.cloud.config.label=master
# dev 开发环境配置文件 |  test 测试环境  |  pro 正式环境
# 和git里的文件名对应,我们可以配置多个文件来区分不同环境的配置
spring.cloud.config.profile=dat
# 指明配置服务中心的网址
spring.cloud.config.uri= http://localhost:8888/
复制代码
spring-cloud-config 搭建-入门级(一)

搭建完毕的访问地址

server端访问地址

/{application}/{profile}[/{label}] http://localhost:8888/config-client/dev

/{application}-{profile}.yml /{application}-{profile}.properties http://localhost:8888/config-client-dev.properties

/{label}/{application}-{profile}.properties /{label}/{application}-{profile}.yml http://localhost:8888/master/config-client-dev.properties

spring-cloud-config 搭建-入门级(一)

证明配置服务中心可以从远程程序获取配置信息。

client端访问地址

http://localhost:8889/hi

spring-cloud-config 搭建-入门级(一)

搭建后优化

1 先把所有的properties 改成 yml

2 优化配置中心的配置文件为一个

spring-cloud-config 搭建-入门级(一)

这样配置完毕后,启动的端口也完全由 client的配置文件 bootstrap.xml 配置的环境来决定了


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据库索引设计与优化

数据库索引设计与优化

【美】Tapio Lahdenmaki、【美】Michael Leach / 曹怡倩、赵建伟 / 电子工业出版社 / 2015-6 / 79.00元

《数据库索引设计与优化》提供了一种简单、高效、通用的关系型数据库索引设计方法。作者通过系统的讲解及大量的案例清晰地阐释了关系型数据库的访问路径选择原理,以及表和索引的扫描方式,详尽地讲解了如何快速地估算SQL 运行的CPU 时间及执行时间,帮助读者从原理上理解SQL、表及索引结构、访问方式等对关系型数据库造成的影响,并能够运用量化的方法进行判断和优化,指导关系型数据库的索引设计。 《数据库索......一起来看看 《数据库索引设计与优化》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

html转js在线工具
html转js在线工具

html转js在线工具