0
点赞
收藏
分享

微信扫一扫

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


简介

通过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、请求查看配置

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

总结

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


举报

相关推荐

0 条评论