spring:
profiles:
active: dev
---
spring:
profiles: dev
application:
name: springcloud-config-dev
---
spring:
profiles: prod
application:
name: springcloud-config-prod
spring:
profiles:
active: dev
---
server:
port: 8989
spring:
profiles: dev
application:
name: springcloud-configclient-dev
eureka:
client:
service-url:
defaultZone: http://eureka02.server.com:7002/eureka/,http://eureka03.server.com:7003/eureka/,http://eureka01.server.com:7001/eureka/
---
server:
port: 9898
spring:
profiles: prod
application:
name: springcloud-configclient-prod
eureka:
client:
service-url:
defaultZone: http://eureka02.server.com:7002/eureka/,http://eureka03.server.com:7003/eureka/,http://eureka01.server.com:7001/eureka/
<?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>SpringCloud</artifactId>
<groupId>com.gton</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>SpringCloud_Config</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springcloud -git配置中心依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.2.8.RELEASE</version>
</dependency>
<!--添加完善监控信息||actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
server:
port: 3344
spring:
application:
name: springcloud-config-server
cloud:
config:
server:
git:
uri: https://gitee.com/gtnotgod/springcloud-netflix-config.git
怎么读取远程配置:
# http://localhost:3344/application-prod.yml
# HTTP服务具有以下格式的资源:
#
# /{application}/{profile}[/{label}]
# /{application}-{profile}.yml
# /{label}/{application}-{profile}.yml
# /{application}-{profile}.properties
# /{label}/{application}-{profile}.properties
开启配置中心
@SpringBootApplication
@EnableConfigServer //开启配置中心
public class SpringCloudConfig {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfig.class, args);
}
}
访问:http://localhost:3344/application-dev.yml
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--添加完善监控信息||actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--COnfig 客户调用服务 starter-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.2.8.RELEASE</version>
</dependency>
application.yml
# bootstrap.yml 系统级的配置 application.yml 用户级的配置
spring:
application:
name: springcloud-config-client
cloud:
config:
uri: http://localhost:3344
name: config-client # 在git上读取的资源名称
profile: dev # 访问环境
label: master # 访问分支
Controller
@RestController
public class ConfigClientController {
@Value("${spring.application.name}")
private String applicationName;
@Value("${eureka.client.service-url.defaultZone}")
private String eurekaServer;
@Value("${server.port}")
private String serverPort;
@RequestMapping("getCloudConfig")
public Map<String, Object> getConfigByCloud() {
Map<String, Object> resp = new HashMap<>();
resp.put("applicationName", applicationName);
resp.put("eurekaServer", eurekaServer);
resp.put("serverPort", serverPort);
return resp;
}
}
http://localhost:8989/getCloudConfig
<!--提取必要依赖版本信息-->
<properties>
<!--项目编码设置-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!--项目java版本-->
<java.version>1.8</java.version>
<mysql.version>5.1.47</mysql.version>
<druid.version>1.1.24</druid.version>
<mybatis.spring.boot.version>2.1.4</mybatis.spring.boot.version>
<lombok.version>1.18.12</lombok.version>
</properties>
<!--dependencyManagement中定义的只是依赖的声明,并不实现引入,因此子项目需要显式的声明需要用的依赖。-->
<dependencyManagement>
<dependencies>
<!--springboot依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.10.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--springcloud依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR11</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</version>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
原文:https://www.cnblogs.com/gtnotgod/p/14969654.html