Nacos
What is nacos
Nacos (Dynamic Naming and Configuration Service) is a dynamic service that is easier to build cloud native applications Now, configuration management and service management center
Nacos is the combination of registry + configuration center, which is equivalent to Eureka+Config+Bus+zik in netflix version
What pain points have been solved
- The configuration we encountered in the netflix version before needs to manually distinguish messages,
- It eliminates the need for us to build a registration center. Ali provides us with an out of the box distribution version. We only need simple configuration and direct script startup
- You can directly view the cluster information and call the link
How to get and community documents
Download address: https://github.com/alibaba/Nacos
Chinese document address: https://nacos.io/zh-cn/index.html
Official document address: https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html#_spring_cloud_alibaba_nacos_discovery
Comparison of mainstream registries

Install nacos
Environmental Science:
- java 8
- Maven
Unzip the installation package and directly run startup.cmd under the bin directory For version 2.xx, you need to modify the stand-alone version, and then start startup.cmd -m standalone with the command Direct access after successful command operation http://localhost:8848/nacos It's that simple

As a service registry
We create two new modules nacos based service provider cluster: cloudalibaba provider payment9001 nacos based service provider cluster: cloudalibaba provider payment9002
cloudalibaba-provider-payment9001
The main dependent packages required by nacos
<dependency> <groupId>com.alibaba.cloudgroupId> <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-actuatorartifactId> dependency>
Core profile
server: port: 9001 spring: application: name: nacos-payment-provider cloud: #Register with nacos nacos: discovery: server-addr: localhost:8848 #Configure Nacos address management: endpoints: web: exposure: include: '*'
Configuration considerations
- Be sure to import service exposure
- Add the address of nacos during configuration
Write a method test
package com.atguigu.springcloud.alibaba.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class PaymentController { @Value("${server.port}") private String serverPort; @GetMapping(value = "/payment/nacos/{id}") public String getPayment(@PathVariable("id") Integer id) { return "nacos registry, serverPort: "+ serverPort+"\t id"+id; } }
Annotate the main startup class
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class PaymentMain9001 { public static void main(String[] args) { SpringApplication.run(PaymentMain9001.class,args); } }
Start the service and you can see our service in nacos

Another cluster module refers to the writing method of 9001
As a service configuration center
Let's build a demo to experience the dynamic configuration of nacos Configuration module: cloudalibaba config Nacos client3377
Basic configuration
Import the required jar package
<dependencies> <dependency> <groupId>com.alibaba.cloudgroupId> <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId> dependency> <dependency> <groupId>com.alibaba.cloudgroupId> <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-actuatorartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-devtoolsartifactId> <scope>runtimescope> <optional>trueoptional> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> <optional>trueoptional> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> dependencies>
Write configuration file A concept is introduced here
bootstrap has higher startup priority than application To ensure that the configuration on our nacos is loaded successfully
server: port: 3377 spring: application: name: nacos-config-client cloud: nacos: discovery: server-addr: localhost:8848 #Service registry address config: server-addr: localhost:8848 #Configuration center address file-extension: yaml #Specifies the configuration of yaml format
Write a controller to view the test information
package com.atguigu.springcloud.alibaba.controller; 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.RestController; @RestController @RefreshScope public class ConfigClientController { @Value("${config.info}") private String configInfo; @GetMapping("/config/info") public String getConfigInfo() { return configInfo; } }
RefreshScope we can use the cloud's native annotations to dynamically view and refresh the configuration,
Create a configuration on nacos

Configuration rules in nacos

After startup, you can change the configuration on nacos
nacos configuration persistence
We can try it out. nacos comes with an embedded database derby We can modify the settings here to our own mysql First go to the nacos folder and find the nacos sql script

Import into mysql to create a database nacos_config

Then find the application file Modify configuration
spring.datasource.platform=mysql db.num=1 db.url.0=jdbc:mysql://127.0.0.1:3306/nacos_config_tmall?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&serverTimezone=UTC db.user=root db.password=root
After starting nacos, you will see a new naocs, and various configurations we will create later will be saved in the database we created
Summary
After using Eureka and nacos, we feel that nacos is an excellent registry. We can start quickly and save a lot of configuration. This technology simplifies the cumbersome configuration we need to use the registry and dynamic configuration to view the link,