This is the 12th day of my participation in Gwen Challenge

Author: JavaGieGie

Wechat official account: Java development zero to one

preface

Last time I taught you how to use EasyCode to solve the needs of a week in 1 hour. I wonder if you have the opportunity to try a wave. Today, I will introduce Nacos, a configuration center and registry in one piece. This article can be said to be single old iron Gospel, after learning you can proudly and next to the little sister colleagues say, hi, you know what Nacos is, not an accident soon can take off single, hold beauty to 😁.

The body of the

What is Nacos

Nacos is dedicated to helping you discover, configure, and manage microservices. Nacos provides an easy-to-use feature set that helps you quickly achieve dynamic service discovery, service configuration, and help you build, deliver, and manage microservices platforms more agile and easily.

What Nacos does

Nacos can dynamically configure files in application.yml without restarting the service. It also works with Dubbo, similar to Zookeeper. The main functions are as follows. Interested partners can visit the official website of Nacos at the end of this article:

  • Service discovery and monitoring: support DNS and RPC-based service discovery;
  • Dynamic service configuration: After the configuration file is modified, applications and services do not need to be redeployed.
  • DNS service: Implements load balancing at the middle layer, flexible routing policies, traffic control, and simple DNS resolution services on the data center Intranet.

Comparison of several registries:

Dynamic configuration process description

  1. Download Nacos

Demo environment: Win10, Jdk8

Click Download to download 2.0.2 Nacos. After downloading, unpack it to a local directory. The following two steps are required for configuration:

  • Add conf/nacos-mysql. SQL to the nacos database.
  • Modify the database file in conf/application.properties
spring.datasource.platform=mysql
db.num=1
db.url.0=JDBC: mysql: / / 127.0.0.1:3306 / nacos? characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=root
Copy the code
  1. Start the Nacos service

Open the bin directory in the Nacos installation directory. As a Windows user, double-click startup. CMD to start the service

Linux/Unix/Mac

Startup command (standalone stands for standalone mode, not cluster mode):

sh startup.sh -m standalone
Copy the code

If you are using Ubuntu, or if you run the script, an error message [[symbol cannot be found, try the following:

bash startup.sh -m standalone
Copy the code

Windows

Startup command (standalone stands for standalone mode, not cluster mode):

startup.cmd -m standalone
Copy the code
  1. Open the Nacos url

Browser input address * * * * http://127.0.0.1:8848/nacos/index.html can open service graphical page, the default user name password (nacos/nacos),

As shown, the interface is very clean and comfortable. On the left side, configuration Management is used to add configuration files. Click New Configuration

Click New configuration, enter several configuration items, and click “Publish” to complete the configuration:

  • Data ID: indicates the name of the configuration file. You can configure dev and prod to distinguish environments.
  • Group: The default Group name is used if this parameter is not changed.
  • Configuration format: Set this parameter based on site requirements.

3. Create a SpringBoot project

A new project [NACOS -] with Gie has been completed. We need to configure the following parameters:

  • Startup class: Add@NacosPropertySource(dataId = "application-dev.yml", autoRefreshed = true), application-dev.yml is the Data Id just configured.
  • The properties, application of addNacos. Config. Server - addr = 127.0.0.1:8848Is the address of the Nacos service just started;
  • Pom file:
<properties>
    <nacos-config-spring-boot.version>0.2.1</nacos-config-spring-boot.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>nacos-config-spring-boot-starter</artifactId>
        <version>${nacos-config-spring-boot.version}</version>
    </dependency>

    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>nacos-config-spring-boot-actuator</artifactId>
        <version>${nacos-config-spring-boot.version}</version>
    </dependency>
</dependencies>
Copy the code
  • The test class
@RestController
@ResponseBody
public class NacosController {

    @NacosValue(value = "${name}", autoRefreshed = true)
    private String name;

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public String get(a) {
        returnname; }}Copy the code

Start the project and test the configuration center

Start SpringBoot project, the browser type http://127.0.0.1:8080/get, will be displayed Nacos just configuration of govemment, modify the configuration file in Nacos content, don’t need to restart the project, visit the address again, will find that the content has been updated.

conclusion

The configuration management usage of Nacos was briefly shared today, and the configuration center will be covered in the next chapter. Since we chose this road, we can not have been immersed in the old technology stop, after all, the Internet industry technology update iteration is very fast, we should pay attention to the foundation of the appropriate use of some good new technology (although not new, but I was recently used…. Bow his head shyly), so that even if the interview is asked a little confidence in the heart.

Patience to see the last partner, must be a good comrade who loves learning, but also to spend Gie to share the technology very much agree (shameless +1), look at the time there are still 20 minutes to zero morning, another full day (almost did not breathe up), sleep sleep, dog life matter.

Pay attention to avoid getting lost

The above is the whole content of this issue, if there is any mistake, please leave a message for advice, thank you very much. I’m Gie, feel free to leave a comment if you have any questions, and we’ll see you next time at 🦮.

The article continues to update, you can wechat search Java development zero to one for the first time to read, and you can get interview materials learning video, interested partners welcome to pay attention to, learn together, ha 🐮🥃.

Original is not easy, how can you bear to whoring for nothing, if you think this article is a little useful to you, thank old tie for this article to like, comment or forward, because this will be my output more quality articles of power, thank you!

Official Demo: github.com/nacos-group…

Official Manual: nacos. IO/zh-CN /docs/…