preface
In the last article 10. Config of microservice global configuration center In, we implemented the unified configuration center. However, there is a problem: in the actual development, the configuration file of the project needs to change frequently. At this time, we need to restart the nodes (clients) in the microservice one by one after modifying the configuration file at the remote end. Is there a method that does not need to restart the node (client)? The answer is yes. Spring cloud provides us with manual refresh and automatic refresh methods; Now let's look at how to refresh the unified configuration center by manual refresh.
Manually refresh configuration
As we mentioned above, the so-called manual refresh configuration is that when the configuration in the remote Git warehouse changes, you can directly read the modified configuration information of the remote without restarting the microservice. However, since it is a manual refresh, it is necessary to perform the steps manually.
Implementation steps
1.@RefreshScope
This annotation is used to refresh the information in the current scope domain to the latest configuration without restarting the microservice. Specific usage: add this annotation to the java class (usually the Controller file) that uses the relevant attributes of the configuration file
2.TestController.java
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @Author Christy * @Date 2021/7/13 9:48 **/ @RestController @RequestMapping("/test") @RefreshScope public class TestController { private static final Logger log = LoggerFactory.getLogger(TestController.class); @Value("${username}") private String username; @GetMapping("/hello") public String sayHello(){ log.info("Hello " + username + ", Welcome!"); return "Hello " + username + ", Welcome!"; } }
3. Open all web endpoint exposure
In addition to the @ RefreshScope annotation we mentioned above, we also need to open all web endpoint exposures on all client s (micro service nodes)
# Configure registry spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 # Tell the service id of the current Config Client unified configuration center in the registry spring.cloud.config.discovery.service-id=CONFIGSERVER # Open the current Config Client and obtain it from the registry according to the service id spring.cloud.config.discovery.enabled=true # Get that configuration file 1 Determine branch 2 Determine file name 3 Determine the environment spring.cloud.config.label=master spring.cloud.config.name=configclient spring.cloud.config.profile=dev management.endpoints.web.exposure.include=*
4. Start
After modifying the above configuration, we start consumer, Config Server and Config client in turn. Browser input http://localhost:8911/test/hello , the results are as follows:
5. Modify remote configuration file
In the pre configuration file above, the remote configuration file we loaded is configclient-dev.properties. Now we change the value of username to developer
6. Manually refresh the configuration file
You can't visit directly at this time http://localhost:8911/test/hello Otherwise, the result is the same as before. At this step, we need to implement http://localhost:8911/actuator/refresh .
First, let's access it in the way of Postman. The results are as follows:
The returned result also tells us that the username attribute in the configuration file has been updated;
It should be noted that this method must be accessed in POST mode;
If there are multiple clients (clusters), each client needs to access the URL in POST mode
We can also access this address from the command line: curl -X POST http://localhost:8911/actuator/refresh , the results are as follows:
You can see that this result is the same as that in Postman;
This command can only be executed once. If you execute it again in Postman or the command line, you will get an empty result, as shown in the following figure:
7. Test
After the above work is finished, we can test the final effect; Enter in the browser http://localhost:8911/test/hello , you can see that the result has become the value we just modified, as shown in the following figure:
Existing problems
Although this method does refresh the configuration file without restarting the micro service, we also said that once the configuration file of a module (micro service cluster) is modified, all nodes in the cluster must perform the above refresh operation. This is still troublesome in dozens or hundreds of micro service clusters. In the next section, we will learn how to automatically refresh the configuration file.
The source code of this series of topics has been uploaded to gitee: https://gitee.com/tide001/springcloud_parent , welcome to download and communicate