SpringCloud极简入门>配置服务#config

本文介绍了如何使用Spring Cloud Config搭建配置中心,配置从Git仓库加载,并在微服务间共享。首先创建`server-config`服务,配置相关pom.xml、application.yml,启用Eureka和Config Server。验证配置中心正常工作后,再在`server-order`服务中添加Config Client依赖,配置bootstrap.yml以从`server-config`读取配置。最后展示了通过@Value和@ConfigurationProperties两种方式在代码中读取配置,并提供测试接口验证。

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

简介

通过config组件,可以将各个微服务的配置存放于git、gitee或者本地文件,并且可以配合bus组件,当修改远程配置文件后,不需要挨个重启部署相关服务,本篇介绍config的引入,和微服务如何读取远程配置;

前置内容:
SpringCloud极简入门>链路追踪#zipkin

实战

远程配置文件地址:https://gitee.com/catface7/spring-cloud-config/blob/main/dev/server-order-dev.yml;
注意,如果是《server-order》服务的配置,则git上的配置文件必须以“server-order”开头;

1、创建《server-config》服务

1.1、pom配置

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>cc.catface</groupId>
        <artifactId>simple-eureka</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>server-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>server-account</name>
    <description>Demo project for Spring Boot</description>
    
    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

		<!--添加config-server依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
</project>

1.2、application添加注解

@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer//支持config组件

1.3、application.yml配置

server:
  port: 8050

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7000/eureka/

spring:
  application:
    name: server-config
  cloud:
    config:
      server:
        git:
          #配置文件所在git
          uri: https://gitee.com/catface7/spring-cloud-config.git
          #配置文件所在目录
          search-paths: dev
          username: catface7
          password: ******
          default-label: main
      #git分支
      label: main

1.4、验证《server-config》服务

  • 访问http://localhost:8050/version/dev,有返回

      {"name":"version","profiles":["dev"],"label":null,"version":"a8edb84950bfb2ef565928b9638adf820ae84181","state":null,"propertySources":[]}
    
  • 访问http://localhost:8050/server-order/dev,有返回配置信息

      {"name":"server-order","profiles":["dev"],"label":null,"version":"a8edb84950bfb2ef565928b9638adf820ae84181","state":null,"propertySources":[{"name":"https://gitee.com/catface7/spring-cloud-config.git/dev/server-order-dev.yml","source":{"data.env":"dev123","data.user.username":"catface","data.user.password":"root"}}]}
    

至此,《server-config》就可以读取git上的配置信息了;

2、在《server-order》中读取《server-config》拉取的git配置

2.1、《server-order》添加pom依赖

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

2.2、新增bootstrap.yml

  • application.yml

    server:
      port: 7002
    
    spring:
      application:
        name: server-order
      zipkin:
        base-url: http://localhost:9411
    
  • bootstrap.yml

    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7000/eureka/
    
    #读取server-config服务上当前服务的git配置
    spring:
      cloud:
        config:
          label: main
          profile: dev
          discovery:
            enabled: true
            service-id: server-config
    

2.3、代码中分别通过@Value和@ConfigurationProperties实例化配置内容

推荐使用@ConfigurationProperties方式读取配置;

  • @Value

    @Component
    public class GitConfig {
    
    	//toString
        @Value("${data.env}")
        private String env;
    
        @Value("${data.user.username}")
        private String username;
    
        @Value(("${data.user.password}"))
        private String password;
    }
    
  • @ConfigurationProperties
    如果字段没有setter方法,读取为null;

    @Component
    @ConfigurationProperties(prefix = "data")//配置项前缀
    public class GitAutoRefreshConfig {
    	//setter&getter&toString
        private String env;
        private User user;
    
        public static class User {
            private String username;
            private String password;
        }
    }
    

2.4、添加测试接口

@RestController
@RefreshScope
public class GitConfigController {

	//测试@Value方式读取配置
    @GetMapping("testCfg")
    public String testCfg() {
        return "testCfg--" + new Date().toLocaleString();
    }

    @Autowired
    private GitConfig gitConfig;

    @GetMapping("cfgV")
    public String cfgV() {
        return gitConfig.toString();
    }
	
	//测试@ConfigurationProperties方式读取配置
    @Autowired
    private GitAutoRefreshConfig gitAutoRefreshConfig;

    @GetMapping("cfgP")
    public String cfgP() {
        return gitAutoRefreshConfig.toString();
    }
}

2.5、请求查看配置

在这里插入图片描述

总结

在git上创建了yml配置文件,新增《server-config》服务作为读取git配置的服务端,其他client通过读取《server-config》获取对应的配置;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值