Road springcloud learning: (e) springcloud integrated SpringCloudConfig distributed configuration center

SpringCloudFamily bucket in a distributed configuration center SpringCloudConfig, it uses gitto manage the configuration file, after modifying the configuration files only need to call one interface can make the new configuration take effect, very convenient.

SpringCloudConfig divided into two parts, the server and the client, the server is used to 提供profile information, and the client is used to 使用profile information, we then began to integrate.

A, SpringCloudConfig server

1. Create a newModule

 2. Select springboot project

 3. a name

 4. Choose a configuration server center

5. Select the client registry

 6. application.properties configuration file

# Service port 
server.port = 8088 
# fill out the registration center server address 
eureka.client.service -url.defaultZone = HTTP: // localhost: 8081 / Eureka 
# Register Configuration Center alias 
spring.application.name = Service- config 
# configure storage address (git) 
spring.cloud.config.server.git.uri = HTTPS: // gitee.com/XXX/spring-cloud-config 
# storage folder 
spring.cloud.config.server.git.search -paths = MyConfig 
# git main branch 
spring.cloud.config.label = master

7. Configure service-config startup file

// open registry client 
@EnableEurekaClient
 // turn on the hub server 
@EnableConfigServer

 

Two, git warehouse operations

1. Create a new cartridge git

 Fill in the information

 2. New Folder

 Name the folder

 3. New File

 Written information

Name the file there is a standard, [service name - environmental properties] or your server can not read the configuration file, let's look at service-athe profile name

 So our configuration file should be called

service-objcat-a-dev. properties

 

Third, start the service access

 http://localhost:8088/service-objcat-a-dev.properties

 http://localhost:8088/service-objcat-a-dis.yml

 Problem-free operation

 

Four, SpringCloudConfig server

To service-aopen a distributed configuration service

1. First, the service-a pomwas added dependencies

<! - distributed configuration dependent center client -> 
<dependency> 
     <the groupId> org.springframework.cloud </ the groupId> 
     <the artifactId> Spring-Cloud Client-config-</ the artifactId> 
</ dependency>

2. modify the configuration file

The application.ymlmodified bootstrap.ymlhere to talk about, these two names are the application configuration file, but bootstrap.ymlwill be executed first, followed by the central server configuration specification is a distributed configuration in bootstrap.ymlthe configuration, maybe profiles can coexist here in order to facilitate sake, so directly renamed, we continue to

# Distributed Configuration Center 
  Cloud: 
    config: 
      Profile: dev 
      Discovery: 
        Enabled: to true 
        Service -id: Service-config

Note must be used with an alias name of the server configuration consistent above you,  profiledo not write without basis, you need to write application environment configuration file, remember our configuration file naming it [ 服务名-环境.yml]

3. There, you've completed the configuration, we have to write interfaces to test it!

@Value("${name}")
    private String name;

@RequestMapping("/hello")
    public String hello() {
        return name;
    }

@valueIs read from the configuration file in a field, we are namein this field is the existence of the service side, so if you can read it, as evidenced by a distributed configuration center can be used, then we have to run the service

 Access Interface

http://localhost:8082/hello

4. Dynamic Change

Now our profile can not be done remotely dynamic change, we need to do is refresh field line, do not need to restart the server, we first of all to service-aimport 监控模块packages

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

5. modify the configuration file, add the following fields in the profile expose allendpoints

management:
  endpoints:
    web:
      exposure:
        include: "*"

6. Set the refreshing notes @RefreshScope in the controller, only the configured values ​​will refresh annotated controller is refreshed.

 7. Restart Service

 8. Modify configuration information on the git

 9. Access again, found no change

 10. It should be a call to refresh the interface, you must pay attention to the use of postthe request , or you can use the command line postman.

Command Line: 

curl -X POST http://localhost:8082/actuator/refresh

postman: 

http://localhost:8082/actuator/refresh

 11. Access Interface again, success

 

Guess you like

Origin www.cnblogs.com/zhainan-blog/p/11648157.html