Then open the webpage of Nacos service and open configuration management->; Configure the list, and then click the Add button on the right to add it.
Data id: the name corresponding to spring:application:name in the bootstrap.yml configuration file;
Group: specify grouping (convenient for project configuration management in different environments, because I belong to the test here, so I fill in the test corresponding to Group in the above configuration file);
Description: Description of the configuration;
Configuration format: the format of the configuration file should be consistent with the suffix format in the data ID (I use yml here, so I choose yaml below. Note that the attribute can also be selected in this location, but it must match the value of file-extension in the bootstrap.yml file above);
Configuration content: specific configuration content (here, the author copied all the configurations in application.yml in the project);
When testing and starting the consumer service, it is empty in application.yml, and the project startup port is still 90 1 1 in the Nacos configuration, indicating that the project successfully relies on the configuration center of Nacos, and other services can do the same:
Add a test controller, and then add the comment @RefreshScope, indicating that the configuration data in this controller is automatically refreshed.
Edit the configuration file consumer in Nacos, add related parameter types: test, access controller, and return to test. The effect is as follows:
Change the type: test of the consumer.yml file in Nacos to type: prod, and revisit the corresponding controller without restarting the project. The effect is as follows:
Because Dubbo is a common dependency among all services, cloud-common is introduced, and detailed versions can search their own projects in mvnrepository.
After introducing dependency, it is necessary to write configuration file in consumer service, register Dubbo service with Nacos, and add the following contents, where subscribed-services refers to producer service, PROT:- 1 refers to random port, and registry:address refers to registration center corresponding to Dubbo, so this should be set as Nacos.
Next, add interface service, the project type is Maven project, and add an interface in the project. This module is introduced into pom.xml files of cloud providers and cloud consumers.
The method corresponding to the interface is implemented in the actual service of the producer.
Introduce the service into the controller of the service consumer, and add @Reference annotation for the service. When introducing jar packages, pay attention to the ones with Dubbo, not the Jdk native ones.
Write and test the interface called by Dubbo in consumer service. The test results are as follows: