SpringCloud-Config的总结
程序员文章站
2022-07-03 19:56:22
...
SpringCloud-Config学习
一,概述
一图胜千言.
二,实战
1,远程配置中心文件项目
- 项目名称:config-repo
- 配置文件:application.properties
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
- dev 环境的配置文件
demo.env=dev
- test 环境的配置文件
demo.env=test
- 说明:该项目本质是git项目,只有上面的配置文件其他不需要配置,只是简单的配置文件仓库,目的是提供公共配置中心.
2,配置服务器项目
- 项目名称:config-server
- 端口号:8091
- pom文件中的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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>
- application.properties
server.port=8091
spring.application.name=springcloud_config3
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
#服务的git仓库地址
spring.cloud.config.server.git.uri=https://git.yomob.com/wangwg/config-repo.git
#spring.cloud.config.server.git.uri=https://github.com/ziwenwang356/gitskills
#配置文件所在的目录
spring.cloud.config.server.git.search-paths=/config-repo
#配置文件所在的分支
spring.cloud.config.label=master
#git仓库的用户名
spring.cloud.config.server.git.username=wangwgxyzwwww.com
#git仓库的密码
spring.cloud.config.server.git.password=123456789
- 程序入口
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class SpringcloudConfig3Application {
public static void main(String[] args) {
SpringApplication.run(SpringcloudConfig3Application.class, args);
}
}
- 可以这么测试
http://localhost:8091/master/config-client.properties
or
http://localhost:8091/config-client-dev.properties
3,获取配置配置中心客户端
- 项目名称:config-client
- 端口:8083
- pom文件中的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
- application.properties
server.port=8083
spring.application.name=config-client
spring.profiles.active=dev
spring.cloud.config.uri=http://localhost:8091
- bootstrap.yml
spring:
application:
name: config-client
profiles:
active: dev
cloud:
config:
uri: http://localhost:8091
- 程序入口
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
- 测试代码
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Wengang Wang
* @since May 22, 2018
*/
@RestController
public class DemoController {
@Value("${demo.env}")
private String env;
@GetMapping("/hello")
public String hello() {
return "Hello! It's " + env;
}
}
- 按照如下的方式可以看到效果,明白其中的原理
按照如下的方式进行测试:(它就首先去访问springcloud_config3的服务,然后springcloud_config3去访问config-repo的文件中的参数)
http://localhost:8083/hello