《SpringCloud微服务 二》之 Spring Cloud Config 配置中心

《SpringCloud微服务 二》之 Spring Cloud Config 配置中心



二、spring cloud config 配置中心


在微服务中,各个应用各自配置维护是个麻烦事,config配置中心是统一管理各个应用配置,方便统一


2.1 config 服务端


第一步:创建一个git仓库专门管理各个应用的配置文件


仓库:spring-cloud-learn-git-config


仓库下创建应用的配置文件 books-service-dev.yml


内容:

book:
  id: 202201*DEV
  name: 《萧十一郎 * DEV》
  author: 李寻欢DEV


第二步:添加依赖pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>


第三步:添加主配置 application.yml

 
server:
  port: 7070
  servlet:
    context-path: /
    
  
spring:
  application:
    name: config-server
    
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/tianwyam/spring-cloud-learn-git-config.git

首先要建立一个存放配置文件的git仓库 spring-cloud-learn-git-config ,统一管理各个应用的配置文件


注意:若是配置文件git仓库是私有的,则需要配置用户名和密码,公共的则只需要配置URI地址即可


springcloud-config 它配置文件规则是 :

分支(label)-》应用名称(spring.application.name)-环境(profile).yml 或者是 properties文件

若是 spring.application.name 没有设置,则默认是 application

比如:master 下 config-server-DEV.yml



第四步:启动类上添加 开启config服务注解 @EnableConfigServer

// 开启配置中心服务
@EnableConfigServer
@SpringBootApplication
public class LearnSpringCloudConfigApplication {
	
	public static void main(String[] args) {
		SpringApplication.run(LearnSpringCloudConfigApplication.class, args);
	}

}

最后启动应用后,就可以查看存放在git仓库里面的各个应用的配置文件

其实配置中心服务端就是一个映射git仓库的配置,进行统一管理


访问:http://localhost:7070/{name}/{profile}/{label}


比如:books-service 这个应用的配置

访问地址:http://localhost:7070/books-service/dev/master

结果:

{
	"name": "books-service",
	"profiles": ["dev"],
	"label": "master",
	"version": "b765aa3e430758ac610906a85825c46e78686667",
	"state": null,
	"propertySources": [{
		"name": "https://gitee.com/tianwyam/spring-cloud-learn-git-config.git/books-service-dev.yml",
		"source": {
			"book.id": "202201*DEV",
			"book.name": "《萧十一郎 * DEV》",
			"book.author": "李寻欢DEV"
		}
	}, {
		"name": "https://gitee.com/tianwyam/spring-cloud-learn-git-config.git/application-dev.yml",
		"source": {
			"student.name": "李寻欢-DEV",
			"student.age": 26
		}
	}]
}



2.2 config 客户端


客户端就是各个应用配置服务端地址,在启动时会先去拉取配置中心里面的配置,优先采用


1.引入依赖 pom.xml

<!-- springcloud config client -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>


spring-cloud-starter-config 包含了 spring-cloud-config-client 的jar

2.添加配置 bootstrap.yml

spring:
  cloud:
    config:
      label: master
      profile: dev
      uri:
      - http://localhost:7070

注意:springcloud config的服务端的配置需要在 bootstrap.yml 或者 bootstrap.properties文件里面配置,这样项目启动最先加载此配置文件,然后去远端配置中心拉取系统所需要的配置

若是配置在application.yml文件中的话,config-server的配置无法生效

拉取各个应用的配置文件的规则是:applicationName-profile.yml 或者 applicationName-profile.properties



3.应用中使用配置

@Data
@ConfigurationProperties(prefix = "book")
public class GitConfigBookBean {
	
	private String id ;
	
	private String name ;
	
	private String author ;
	
}

也可以使用 @Value("${book.id}") 方式取值

开启配置,读取配置文件

@EnableEurekaClient
@SpringBootApplication
// 开启配置,读取配置文件
@EnableConfigurationProperties(value = GitConfigBookBean.class)
public class BookServiceApplication {

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


方式很多种,都是可以去配置中心拉取到相应的配置的


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天涯共明月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值