spring-cloud入门之spring-cloud-config(配置中心)

2025-05-27 0 82

前言

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件:spring-cloud-config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程git仓库中。

本节主要演示怎么用git仓库作为配置源。

开源地址:https://github.com/bigbeef

创建配置项目

在github中创建一个项目,专门用来保存我们所有项目的配置文件,项目是我的项目结构

配置项目地址:https://github.com/bigbeef/cppba-config

spring-cloud入门之spring-cloud-config(配置中心)

eureka-server.properties

?

1

2

3

4

5

6
eureka.client.register-with-eureka=false

eureka.client.fetch-registry=false

spring.application.name=eureka-server

server.port=18761

eureka.instance.hostname=peer1

eureka.client.serviceurl.defaultzone=http://peer1:18761/eureka/

创建spring-cloud-config-server项目

项目结构如图:

spring-cloud入门之spring-cloud-config(配置中心)

pom.xml核心代码

?

1

2

3

4

5

6
<dependencies>

<dependency>

<groupid>org.springframework.cloud</groupid>

<artifactid>spring-cloud-config-server</artifactid>

</dependency>

</dependencies>

springcloudconfigserverapplication.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
package com.cppba;

import org.springframework.boot.springapplication;

import org.springframework.boot.autoconfigure.springbootapplication;

import org.springframework.cloud.config.server.enableconfigserver;

@springbootapplication

@enableconfigserver

public class springcloudconfigserverapplication {

public static void main(string[] args) {

springapplication.run(springcloudconfigserverapplication.class, args);

}

}

application.properties

这个根据自己实际的git项目修改配置

?

1

2

3

4

5

6

7

8

9

10

11

12
server.port=8888

spring.application.name=config-server

spring.cloud.config.server.git.uri=https://github.com/bigbeef/cppba-config

spring.cloud.config.label=master

# spring.cloud.config.server.git.username=

# spring.cloud.config.server.git.password=

spring.cloud.config.server.git.searchpaths=\\

cppba-spring-cloud/*,\\

cppba-spring-cloud/eureka-client/*

spring.cloud.config.server.git.uri:配置git仓库地址
spring.cloud.config.server.git.searchpaths:配置仓库路径,以逗号隔开
spring.cloud.config.label:配置仓库的分支
spring.cloud.config.server.git.username:访问git仓库的用户名
spring.cloud.config.server.git.password:访问git仓库的用户密码

启动项目

访问地址:http://127.0.0.1:8888

http请求地址和资源文件映射如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

根据我们自己的配置,我们可以这样访问:http://127.0.0.1:8888/eureka-server/default/master

application -> eureka-server (应用名)
profile -> default (启用的配置,通常是后缀,下面解释)
label -> master (分支)

访问到的结果就是:

spring-cloud入门之spring-cloud-config(配置中心)

profile比较重要,可以理解成读取哪些配置文件,假如我不止一个配置文件,可能会有:

eureka-server.properties(这个是通用配置文件,默认都会加载),
eureka-server-mysql.properties,
eureka-server-oracle.properties,
eureka-server-jpa.properties,
eureka-server-mysql.properties……

我们可能会选择性的加载其中的部分properties配置文件,那我们可以这样写:http://127.0.0.1:8888/eureka-server/default,mysql,jpa/master

到此,我们的spring-cloud-config-server就简单搭起来,后面的章节我会教大家怎么在项目中读取配置

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

原文链接:https://www.jianshu.com/p/6d51157d22ed

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 spring-cloud入门之spring-cloud-config(配置中心) https://www.kuaiidc.com/76353.html

相关文章

发表评论
暂无评论