栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

(九)Config:配置中心

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

(九)Config:配置中心

配置Git仓库

新建一个git仓库(gittee也行)
添加一个测试用的代码application.yml

---
spring:
    profiles: dev
a: 1
---
spring:
    profiles: test
a: 2
新建配置中心模块

pom


	
		org.springframework.cloud
		spring-cloud-config-server
	
	
		org.eclipse.jgit
		org.eclipse.jgit
	

配置文件

server:
  port: 9000
spring:
  application:
    name: config
  cloud:
    config:
      server:
        git: # git地址
          uri: https://gitee.com/zxing2021/spring-cloud-config-test.git

启动类

@SpringBootApplication
@EnableConfigServer
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class);
	}
}

测试

启动配置中心,打开URL:
dev环境:http://localhost:9000/application-dev.yml

test环境:http://localhost:9000/application-test.yml

master分支test环境:http://localhost:9000/master/application-test.yml

成功!

新建配置客户端模块

pom


	
		org.springframework.cloud
		spring-cloud-starter-config
	
	
		org.springframework.boot
		spring-boot-starter-web
	

配置文件,bootstrap.yml,不是application.yml

server:
  port: 9001
spring:
  cloud:
    config:
      name: application # 文件名
      profile: dev # profile
      label: master # 分支
      uri: http://localhost:9000 # 配置中心地址

启动类

@SpringBootApplication
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class);
	}
}

测试Controller,读取a的值并返回

@RestController
public class HelloController {

	@Value("${a}")
	private Integer a;

	@GetMapping("/")
	public Integer fun1() {
		return a;
	}
}

测试

启动配置中心,配置客户端
http://localhost:9001/

成功!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/692348.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号