本文主要是介绍Spring Cloud版本的若依(3)--SpringCloud Config,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
集中管理配置文件
不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
将配置信息以REST接口的形式暴露
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"><parent><artifactId>cloud-zero</artifactId><groupId>com.wind</groupId><version>1.0.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-zero-config</artifactId><dependencies><!--配置中心 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><!--web 模块 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><!--排除tomcat依赖 --><exclusion><artifactId>spring-boot-starter-tomcat</artifactId><groupId>org.springframework.boot</groupId></exclusion></exclusions></dependency><!--undertow容器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-undertow</artifactId></dependency><!-- spring-boot-devtools --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional> <!-- 表示依赖不会传递 --></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><fork>true</fork> <!-- 如果没有该配置,devtools不会生效 --></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build>
</project>
application.yml
server:port: 8888spring:application:name: ruoyi-configprofiles:active: native# 配置中心cloud:config:server:native:search-locations: classpath:/config/# 注册中心配置
eureka:client: #客户端注册进eureka服务列表内service-url: defaultZone: http://eureka7001.com:7001/eureka#defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ instance:instance-id: ${spring.application.name}:${server.port}prefer-ip-address: true #访问路径可以显示IP地址 # 暴露监控端点
management:endpoints:web:exposure:include: '*'
RuoyiConfigApp
@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class RuoyiConfigApp
{public static void main(String[] args){SpringApplication.run(RuoyiConfigApp.class, args);}
}
匹配规则
Config支持我们使用的请求的参数规则为:
/ { 应用名 } / { 环境名 } [ / { 分支名 } ]
/ { 应用名 } - { 环境名 }.yml
/ { 应用名 } - { 环境名 }.properties
/ { 分支名 } / { 应用名 } - { 环境名 }.yml
/ { 分支名 } / { 应用名 } - { 环境名 }.properties
客户端
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-client</artifactId></dependency>
ConfigClientApp
package com.wind.cloud.zero.config;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApp
{public static void main(String[] args){SpringApplication.run(ConfigClientApp.class);}
}
TestController
package com.wind.cloud.zero.config.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestController
{@Value("${config.version}")private String version;@GetMapping("/test")public String test(){return version;}
}
这篇关于Spring Cloud版本的若依(3)--SpringCloud Config的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!