SpringCloudConfig就是通常意义上的配置中心,把应用原本放在本地文件的配置抽取出来放在中心服务器,从而能够提供更好的管理、发布能力。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
注意:springboot的版本是2.1.8.RELEASE,springcloud的版本是Greenwich.SR2
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/langli/springcloudconfig.git #项目路径
search-paths: springcloudconfig #项目名称
default-label: master #代码分支,可以不写
username: langli2049@163.com #gitee账号
password: 12345678 #gitee密码
server:
port: 8001
@EnableConfigServer
data:
env: config-eureka-dev
user:
password: 1986
username: xiaoli
(2)启动config server项目,使用浏览器访问http://localhost:8001/application-dev.yml ,便可以看到如下结果:
<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-actuator</artifactId>
</dependency>
注意:springboot的版本是2.1.8.RELEASE,springcloud的版本是Greenwich.SR2
spring:
profiles:
active: dev
---
spring:
profiles: prod
application:
name: application
cloud:
config:
uri: http://localhost:8001
label: master
profile: prod
---
spring:
profiles: dev
application:
name: application
cloud:
config:
uri: http://localhost:8001
label: master
profile: dev
server:
port: 8002
management:
endpoint:
shutdown:
enabled: false
endpoints:
web:
exposure:
include: "*"
data:
env: NaN
user:
username: NaN
password: NaN
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "data")
//采用这种方式能做到即刻拿到最新配置数据
public class GitAutoRefreshConfig {
private String env;
private UserInfo user;
public GitAutoRefreshConfig(String env, UserInfo user) {
this.env = env;
this.user = user;
}
public GitAutoRefreshConfig() {
}
public String getEnv() {
return env;
}
public void setEnv(String env) {
this.env = env;
}
public UserInfo getUser() {
return user;
}
public void setUser(UserInfo user) {
this.user = user;
}
@Override
public String toString() {
return "GitAutoRefreshConfig{" +
"env=‘" + env + ‘\‘‘ +
", user=" + user +
‘}‘;
}
}
class UserInfo {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserInfo{" +
"username=‘" + username + ‘\‘‘ +
", password=‘" + password + ‘\‘‘ +
‘}‘;
}
}
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
//这种方式不能即刻拿到最新数据,只能重启
public class GitConfig {
@Value("${data.env}")
private String env;
@Value("${data.user.username}")
private String username;
@Value("${data.user.password}")
private String password;
public GitConfig() {
}
public String getEnv() {
return env;
}
public void setEnv(String env) {
this.env = env;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "GitConfig{" +
"env=‘" + env + ‘\‘‘ +
", username=‘" + username + ‘\‘‘ +
", password=‘" + password + ‘\‘‘ +
‘}‘;
}
}
import net.wanho.cloud.configclient.config.GitAutoRefreshConfig;
import net.wanho.cloud.configclient.config.GitConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@RestController
public class GitController {
@Autowired
private GitConfig gitConfig;
@Autowired
private GitAutoRefreshConfig gitAutoRefreshConfig;
@GetMapping(value = "show")
public Object show(){
return gitConfig;
}
@GetMapping(value = "autoShow")
public Object autoShow(){
return gitAutoRefreshConfig;
}
}
(1)依次重启config server,config client
(2)浏览器中访问http://localhost:8002/show,看到如下结果:
(3)浏览器中访问http://localhost:8002/autoShow,看到如下结果:
(4)修改application-dev.xml中的username为zhangli,并推送到gitee,再次刷新以上两个url,没有效果
(5)以POST方式访问http://localhost:8002/actuator/refresh
(6)访问http://localhost:8002/autoShow能看到zhangli,访问http://localhost:8002/show只能看到之前的xiaoli
注意:post方式的刷新只对GitAutoRefreshConfig 起作用;不能每次push代码都是手动刷新,此时应该在gitee中配置webhook钩子。
以上就是springcloud config的使用。
原文:https://www.cnblogs.com/alichengxuyuan/p/12581320.html