SpringCloud入门(九)-分布式配置中心

本文介绍如何使用 Spring Cloud Config 实现分布式系统的统一配置管理。通过搭建配置中心服务端与客户端,实现配置文件的集中式管理和动态更新。文章详细讲解了配置中心的部署步骤及关键配置项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在分布式系统中,由于访服务数量很多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件,在Spring Cloud中,有分布式配置中心组件Spring Cloud Config,它支持配置服务放在配置服务的内存中(本地)。也支持放在远程Git仓库中,在Spring Cloud Config组件中,分两个角色,一是Config Server,二时Config Client。

分布式配置中心服务端

创建一个工程名为hello-spring-cloud-config的项目

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <parent>
        <groupId>com.funtl</groupId>
        <artifactId>hello-spring-cloud-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
    </parent>
 
    <artifactId>hello-spring-cloud-config</artifactId>
    <packaging>jar</packaging>
 
    <name>hello-spring-cloud-config</name>
    <url>http://www.funtl.com</url>
    <inceptionYear>2018-Now</inceptionYear>
 
    <dependencies>
        <!-- Spring Boot Begin -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot End -->
 
        <!-- Spring Cloud Begin -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!-- Spring Cloud End -->
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.funtl.hello.spring.cloud.config.ConfigApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

主要增加了注解@EnableConfigServer

package com.funtl.hello.spring.cloud.config;


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


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

application.yml

spring:
  application:
    name: hello-spring-cloud-config
  cloud:
    config:
      label: master     #分支
      server:
        git:
          uri: https://gitee.com/***/spring-cloud-config.git   
          search-paths: respo  #项目
          username: ***   #gitlab仓库账号
          password: ***   #gitlab仓库密码
server:
  port: 8888         #必须为8888除非创建bootstarp.properties文件
    #在其中添加server.port=需要设置的端口号
    #bootstrap.properties是spring boot的默认文件
    #优先级较高
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

相关配置说明:

  • spring.cloud.config.label:配置仓库的分支

  • spring.cloud.config.server.git.uri:配置Git仓库地址

  • spring.cloud.config.server.git.search-paths:配置仓库路径

  • spring.cloud.config.server.git.username:访问Git仓库的账号

  • spring.cloud.config.server.git.password:访问Git仓库的密码

注意事项:

  • 如果是使用GitLab作为仓库,spring.cloud.config.server.git.uri的末尾需要加上.git

新建一个仓库spring-cloud-config,然后创建respo目录,复制feign服务的application.yml并上传。

服务测试

启动后浏览器访问 http://localhost:8888/web-admin/feign/master

出现这个页面表明配置中心服务端配置成功。

分布式配置中心客户端

以Feign服务为例,其他服务也是主要操作。

主要增加了spring-cloud-starter-comfig依赖

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

application.yml

修改配置文件为:

spring:
  cloud:
    config:
      uri: http://localhost:8888
      name: web-admin-feign
      label: master
      profile: dev

相关配置说明:

  • spring.cloud.config.uri:配置服务中心的地址

  • spring.cloud.config.name:配置文件的前缀

  • spring.cloud.config.label:配置仓库的分支

  • spring.cloud.config.profile:配置文件的环境标识

    dev:表示开发环境

    test:表示测试环境

    prod:表示生成环境

修改配置文件仓库中的Feign的配置文件为

开发环境:web-admin-feign-dev.yml

spring:
  application:
    name: hello-spring-cloud-web-admin-feign
  thymeleaf:
    cache: false
    mode: LEGACYHTML5
    encoding: UTF-8
    servlet:
      content-type: text/html
server:
  port: 8765
feign:
  hystrix:
    enabled: true
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/   #Eureka服务注册中心地址

生产环境:web-admin-feign-prod.yml

spring:
  application:
    name: hello-spring-cloud-web-admin-feign
  thymeleaf:
    cache: false
    mode: LEGACYHTML5
    encoding: UTF-8
    servlet:
      content-type: text/html
server:
  port: 7765
feign:
  hystrix:
    enabled: true
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/   #Eureka服务注册中心地址

关注我

 觉得有点东西就点一下“赞和在看”吧!感谢大家的支持了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值