This is the 17th day of my participation in the August Text Challenge.More challenges in August
The premise condition
Start by downloading Nacos and starting Nacos Server.
Start Configuration Management
After starting Nacos Server, J can start the Nacos configuration management service for Spring Cloud applications.
- Add dependencies:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${latest.version}</version>
</dependency>
Copy the code
Note: Version 2.1.X. lease corresponds to Spring Boot version 2.1.x. Release corresponds to Spring Boot 2.2. x, and 1.5.x. release corresponds to Spring Boot 1.5.x.
For more version mappings, see version Description Wiki
- in
bootstrap.properties
To configure the address and application name of the Nacos Server
Spring. Cloud. Nacos. Config. Server - addr = 127.0.0.1:8848 spring. Application. The name = exampleCopy the code
Note: You need to configure Spring.application.name because it forms part of the Nacos configuration management dataId field.
In Nacos Spring Cloud, the full format of dataId is as follows:
${prefix}-${spring.profiles.active}.${file-extension}
Copy the code
prefix
The default isspring.application.name
Is also available through the configuration itemspring.cloud.nacos.config.prefix
To configure.spring.profiles.active
Is the profile corresponding to the current environment. For details, seeSpring Boot document.Note: whenspring.profiles.active
If null, the corresponding concatenate-
Also will not exist, the dataId splicing format becomes${prefix}.${file-extension}
file-exetension
For the data format of the configuration content, you can pass the configuration itemspring.cloud.nacos.config.file-extension
To configure. Currently only supportedproperties
和yaml
Type.
- Native annotations via Spring Cloud
@RefreshScope
Automatic configuration update:
@RestController @RequestMapping("/config") @RefreshScope public class ConfigController { @Value("${useLocalCache:false}") private boolean useLocalCache; @RequestMapping("/get") public boolean get() { return useLocalCache; }}Copy the code
- First by callingNacos Open APIPublish the configuration to Nacos Server: dataId is
example.properties
, the content ofuseLocalCache=true
The curl -x POST "http://127.0.0.1:8848/nacos/v1/cs/configs? dataId=example.properties&group=DEFAULT_GROUP&content=useLocalCache=true"Copy the code
- run
NacosConfigApplication
, the callcurl http://localhost:8080/config/get
, returns the content istrue
. - Call againNacos Open APIPublish the configuration to Nacos Server: dataId is
example.properties
, the content ofuseLocalCache=false
The curl -x POST "http://127.0.0.1:8848/nacos/v1/cs/configs? dataId=example.properties&group=DEFAULT_GROUP&content=useLocalCache=false"Copy the code
- To visit again
http://localhost:8080/config/get
, the return content isfalse
, indicating the procedureuseLocalCache
The value has been dynamically updated.
Enabling service discovery
This section demonstrates how to enable Nacos’s service discovery capabilities in your Spring Cloud project by implementing a simple Echo Service, as shown below:
See nacos-spring-cloud-discovery-example for a complete example code
- Add dependencies:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${latest.version}</version>
</dependency>
Copy the code
Note: Version 2.1.X. lease corresponds to Spring Boot version 2.1.x. Release corresponds to Spring Boot 2.2. x, and 1.5.x. release corresponds to Spring Boot 1.5.x.
For more version mappings, see version Description Wiki
- Configure service providers so that they can register their services with Nacos Server through Nacos’s service registry discovery feature.
I. Set the Nacos server address in application.properties:
Server port = 8070 spring. Application. Name = service provider - spring. Cloud. Nacos. Discovery. The server - addr = 127.0.0.1:8848Copy the code
Ii. Enable the service registration discovery function through the Spring Cloud native annotation @enableDiscoveryClient:
@SpringBootApplication @EnableDiscoveryClient public class NacosProviderApplication { public static void main(String[] args) { SpringApplication.run(NacosProviderApplication.class, args); } @RestController class EchoController { @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET) public String echo(@PathVariable String string) { return "Hello Nacos Discovery " + string; }}}Copy the code
- Configure the service consumer so that it can get the service it wants to invoke from Nacos Server through Nacos’s service registry discovery feature.
I. Set the Nacos server address in application.properties:
Server port = 8080 spring. Application. Name = service - consumer spring. Cloud. Nacos. Discovery. The server - addr = 127.0.0.1:8848Copy the code
Ii. Enable the service registration discovery function by using the Spring Cloud native annotation @enableDiscoveryClient. Add @loadBalanced to the RestTemplate instance to enable @loadBalanced integration with the Ribbon:
@SpringBootApplication @EnableDiscoveryClient public class NacosConsumerApplication { @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(NacosConsumerApplication.class, args); } @RestController public class TestController { private final RestTemplate restTemplate; @Autowired public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate; } @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET) public String echo(@PathVariable String str) { return restTemplate.getForObject("http://service-provider/echo/" + str, String.class); }}}Copy the code
- Start the
ProviderApplication
和ConsumerApplication
, the callhttp://localhost:8080/echo/2018
, returns the content asHello Nacos Discovery 2018
.