springCloud-远程配置中心

分布式、云服务 导致整个项目不同环境下的配置文件修改称为问题,为此springCloud做了配置中心这一块方便整体配置,springCloud默认远程是放到git上,然后每个服务都访问这个Git来获取配置,下面是基于Git的远程配置

创建maven项目,引入如下依赖



	4.0.0

	com.spring.cloud.conf
	springCloudConf
	0.0.1-SNAPSHOT
	jar

	springCloudConf
	springCloudConf

	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.0.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
		Greenwich.M1
	

	
		
			org.springframework.cloud
			spring-cloud-config-server
		
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-server
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				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
			
		
	




启动类添加两个注解

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

//开启配置中心
@EnableConfigServer
//开启注册客户端
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudConfApplication {

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

接下来是配置文件

#注册中心的地址
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
#服务对外暴露接口
server.port=8889
 #对外暴露服务名
spring.application.name=config-server
#远程配置中心配置(Git路径)
spring.cloud.config.server.git.uri=https://gitee.com/729564606.net/test
#searchPaths配置文件所在的文件夹在仓库中的路径, 在server端不需要指定具体配置文件名
#spring.cloud.config.server.git.searchPaths=spring-cloud/helloworldConfig

然后是Git上的配置文件

命名规范:

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

配置文件的Git地址是 https://gitee.com/729564606.net/test

我的配置文件名是application-test.properties

内容是 test.name=newTest123

当这些配置好后启动配置中心服务(注册中心不能关,会报找不到注册中心)

这是你在注册中心的页面上就能看到配置中心的服务已经启动

springCloud-远程配置中心_第1张图片

然后访问链接:http://127.0.0.1:8889/application-test.properties
(这个url对应/{application}-{profile}.properties)
会显示值:test.name: newTest123
或者访问:http://127.0.0.1:8889/application/test
(这个url的对应/{application}/{profile})
显示:springCloud-远程配置中心_第2张图片

这样springcloud的配置中心就配置完了

码云源码链接

你可能感兴趣的:(springCloud配置中心,springCloud)