Spring Cloud Config
程序员文章站
2022-07-14 22:54:31
...
1、配置中心将需要用到的配置统一放在git或svn仓库中,通过Cloud的服务获取配置,初始化到各自的应用中
2、配置Config-Server,以git为例
①配置用于存放配置文件的git仓库
指定存储库以及配置文件夹地址
如图,将配置文件放在git仓库config-repo的cloud-config文件夹下,四套配置文件分别放置一个参数设置用于测试
trison-config-dev.properties
env=dev
trison-config-ga.propertiesenv=ga
trison-config-st.propertiesenv=st
trison-config-uat.propertiesenv=uat
②应用配置
pom.xml配置
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
applicaition.properties
server.port=8099
spring.application.name=spring-cloud-config-server
aaa@qq.com:trisonTestGit/config-repo.git
spring.cloud.config.server.git.search-paths=cloud-config
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
ConfigServerApplication.java
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
3、启动服务,查看应用读取配置情况
仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
其中label代表分支名称
4、Config-Client配置
pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.properties
spring.application.name=spring-cloud-config-client
server.port=8098
bootstrap.properties
spring.cloud.config.name=trison-config
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8099/
spring.cloud.config.label=master
特别注意:上面这些与spring-cloud相关的属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。因为config的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties。
ConfigClientApplication.java
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
ConfigClientController.java
@RestController
public class ConfigClientController {
@Value("${env}")
private String env;
@RequestMapping("/env")
public String env() {
return this.env;
}
}
启动应用访问
上一篇: 金额---元转分
下一篇: java中金额转换分转元 元转分