Create client projects and read the service-oriented configuration center (consult + spring cloud config)

Time:2019-12-2

Create client project and read the service configuration center

Register the configuration center with the service center (consul)

POM file add dependency:

org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.cloud
            spring-cloud-starter-consul-discovery
            2.1.3.RELEASE

Profile add content:

spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
#Service name registered to consumer
spring.cloud.consul.discovery.serviceName=spring-cloud-config

Start consul service discovery:

@SpringBootApplication
//Start configuration center
@EnableConfigServer
//Start service discovery
@EnableDiscoveryClient
public class SpringCloudConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConfigServerApplication.class, args);
    }

}

Start the server of the configuration center and view the consul background:
Alt text

Register and deploy two configuration centers

Start two configuration centers with the command and configuration file parameters and register the same service name to form high availability. Execute in the spring cloud config server project root directory:

mvn install

Copy the application.properties file in the spring cloud config server project to the directory / spring cloud config server / target /, rename it to application-1.properties and application-2.properties, and then modify the port number and application name in the configuration 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 configuration centers by executing 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

Start the server of the configuration center and view the consul background:
Alt text

Create client project to read configuration center

Alt text
Alt text
Alt text
Alt text

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
#Settings do not need to be registered in consumer
spring.cloud.consul.discovery.register=false

Create a new configuration file bootstrap.properties:

#Configuration item name defined in profile name
spring.cloud.config.name=ConfigDepot
#Environment name defined in profile name
spring.cloud.config.profile=Test
spring.cloud.config.label=master
#Turn on service discovery in configuration center
spring.cloud.config.discovery.enabled=true
#Name of configuration center registered in Service Center
spring.cloud.config.discovery.serviceId=spring-cloud-config

The content of the configuration center needs to be loaded before application.properties, so the configuration information about the configuration center should be written in the bootstrap.properties configuration file, because the bootstrap.properties configuration file will be loaded before application.properties.

Add interface class for reading configuration:

@RestController
public class ConfigTestController {

    //Configuration information is read by @ value annotation, and configuration items are read by ${configuration items}
    @Value("${bluersw.config}")
    private String configBluersw;

    @RequestMapping("/ConfigTest")
    public String ConfigTest(){
        return this.configBluersw;
    }
}

Start client test

Visit 127.0.0.1:9006 / configtest to get the configuration information Test-3 (I changed it several times):
Alt text

At this time, modify the content configured in Git warehouse and refresh the page, the content of the page will not change, because the configuration content is loaded when the program starts, and the content change in configuration center will not be automatically reflected in the client program, but the configuration content can be reloaded by calling the client’s refresh interface, which we will show in the bus part.

Source code

GitHub warehouse: https://github.com/sunweisheng/spring-cloud-example

Recommended Today

Memcached practice foundation

I. memcached practice foundation 1. Introduction to memcached Free and open source, high performance, distributed memory object caching system Effect: Data cache Storage session SMS verification code storage installMemcached is based on libevent, so libevent needs to be installed before memcached is installed 2. Memcached common commands Set commandStore the value (data value) in the […]