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.

  1. 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

  1. inbootstrap.propertiesTo 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
  • prefixThe default isspring.application.nameIs also available through the configuration itemspring.cloud.nacos.config.prefixTo configure.
  • spring.profiles.activeIs the profile corresponding to the current environment. For details, seeSpring Boot document.Note: whenspring.profiles.activeIf null, the corresponding concatenate-Also will not exist, the dataId splicing format becomes${prefix}.${file-extension}
  • file-exetensionFor the data format of the configuration content, you can pass the configuration itemspring.cloud.nacos.config.file-extensionTo configure. Currently only supportedproperties 和 yamlType.
  1. Native annotations via Spring Cloud@RefreshScopeAutomatic 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
  1. First by callingNacos Open APIPublish the configuration to Nacos Server: dataId isexample.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
  1. runNacosConfigApplication, the callcurl http://localhost:8080/config/get, returns the content istrue.
  2. Call againNacos Open APIPublish the configuration to Nacos Server: dataId isexample.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
  1. To visit againhttp://localhost:8080/config/get, the return content isfalse, indicating the procedureuseLocalCacheThe 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

  1. 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

  1. 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
  1. 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
  1. Start theProviderApplication 和 ConsumerApplication, the callhttp://localhost:8080/echo/2018, returns the content asHello Nacos Discovery 2018.