Create a client project and read the configuration of the service center
The distribution center is registered to the service center (Consul)
POM file to add dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
Add the contents of the configuration file:
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
#注册到consul的服务名称
spring.cloud.consul.discovery.serviceName=spring-cloud-config
Start Consul service discovery:
@SpringBootApplication
//启动配置中心
@EnableConfigServer
//启动服务发现
@EnableDiscoveryClient
public class SpringCloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerApplication.class, args);
}
}
Boot configuration center server and view Consul background:
Sign deploy two distribution center
After using the commands and configuration file parameters configured to start two centers and register with the formation of a highly available service name, performed in the spring-cloud-config-server project root directory:
mvn install
Copy the spring-cloud-config-server document to project application.properties / spring-cloud-config-server / target / duplicate directory, and renamed application-1.properties and application-2.properties, then modify the configuration application name and port number in the file:
server.port=9004
spring.application.name=spring-cloud-config-server-01
server.port=9005
spring.application.name=spring-cloud-config-server-02
Start two distribution center execute the following command in the / target / directory:
java -jar spring-cloud-config-server-0.0.1-SNAPSHOT.jar --spring.config.location=application-1.properties
java -jar spring-cloud-config-server-0.0.1-SNAPSHOT.jar --spring.config.location=application-2.properties
Boot configuration center server and view Consul background:
Creating a client reads the configuration center project
Modify the configuration file application.properties:
spring.application.name=spring-cloud-config-client
server.port=9006
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
#设置不需要注册到 consul 中
spring.cloud.consul.discovery.register=false
New Profile bootstrap.properties:
#配置文件名称中定义的配置项目名称
spring.cloud.config.name=ConfigDepot
#配置文件名称中定义的环境名称
spring.cloud.config.profile=Test
spring.cloud.config.label=master
#开启配置中心的服务发现
spring.cloud.config.discovery.enabled=true
#配置中心注册在服务中心的名字
spring.cloud.config.discovery.serviceId=spring-cloud-config
Content distribution center requirements before being loaded on application.properties, so the configuration information about the configuration center in bootstrap.properties to write the configuration file, because bootstrap.properties configuration file will be loaded before the application.properties.
Adding configure the interface class to read:
@RestController
public class ConfigTestController {
//配置信息通过@Value注解读取,配置项用${配置项}读取
@Value("${bluersw.config}")
private String configBluersw;
@RequestMapping("/ConfigTest")
public String ConfigTest(){
return this.configBluersw;
}
}
Start the client test
Access 127.0.0.1:9006/ConfigTest, get information on the configuration Test-3 (middle I changed several times):
At this point modify the contents Git repository configuration, and then refresh the page content of the page will not change, because the configuration of the content is loaded when the program starts, the configuration change is central to not automatically reflected in the client program, but you can reload the configuration interface refresh content by calling the client, this section demonstrates what we put bus.
Source
Github repository: https: //github.com/sunweisheng/spring-cloud-example