远程仓库受网络影响,可以从本地(配置中心)
注册中心与配置中心合并在一起,可能导致页面没有样式,所以分开来。
添加依赖
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
org.baozi
cloud-config
1.0
pom
config-registration
config-server
config-clients
1.8
Greenwich.SR1
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
4.0.0
org.baozi
cloud-config
1.0
..
config-registration
4.0.0
org.baozi
cloud-config
1.0
..
config-server
org.springframework.cloud
spring-cloud-config-server
4.0.0
org.baozi
cloud-config
1.0
..
config-clients
注册中心
spring.application.name=config-registration
server.port=9000
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class ConfigRegistrationApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigRegistrationApplication.class, args);
}
}
配置中心
spring.application.name=config-server
server.port=9500
eureka.client.service-url.defaultZone=http://localhost:9000/eureka
# 连接到远程仓库
spring.cloud.config.server.git.uri=https://gitee.com/baozi-baozi/cloud-config.git
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer // 开启配置中心
//@EnableDiscoveryClient // 这个可以省略
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
访问规则:
1. application:上面文件中的config-clients
2. profile:上面文件中的dev,如果没有就是default
3. labei:就是分支,不写默认为master
4. /application/profile[/label]
http://localhost:9500/config-clients/default
http://localhost:9500/config-clients/default/master
http://localhost:9500/config-clients/dev
http://localhost:9500/config-clients/dev/master
5. /application-profile.[yml | properties | json ]
http://localhost:9500/config-clients-default.properties
http://localhost:9500/config-clients-default.yml
http://localhost:9500/config-clients-default.json
http://localhost:9500/config-clients-dev.properties
http://localhost:9500/config-clients-dev.yml
http://localhost:9500/config-clients-dev.json
6. /label/application-profile.[yml | properties | json ]
在前一个基础上加上分支。
配置客户端
bootstrap.properties
# 配置文件对应的application部分, 同时也是spring.application.name的名字
spring.application.name=config-clients
server.port=10000
# 可以写在配置中心的配置文件中
eureka.client.service-url.defaultZone=http://localhost:9000/eureka
# config-server的url, 可以写多个。
# spring.cloud.config.uri=http://localhost:9500/
# 可以用服务发现的方式代替写多个uri
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-server
# profile部分
#spring.cloud.config.profile=default
spring.cloud.config.profile=dev
# label部分
spring.cloud.config.label=master
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientsApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientsApplication.class, args);
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.MessageFormat;
@RestController
public class ClientsController {
// ~方式一: 如果远程仓库延迟比较高,运行时会报错,采用本地文件比较好
// -------------------------------
@Value("${user.name}")
private String name;
@Value("${user.age}")
private String age;
@GetMapping("/m1")
public String m1() {
return MessageFormat.format("m1 name: {0}, age: {1}", name, age);
}
// ~方式二
// -------------------------------
@Autowired
private Environment environment;
@GetMapping("/m2")
public String m2() {
String name = environment.getProperty("user.name", "undefined");
String age = environment.getProperty("user.age", "-1");
return MessageFormat.format("m2 name: {0}, age: {1}", name, age);
}
}