Nacos for Dynamic Naming and Configuration

Introduction        

Nacos is a beneficial tool for dynamic naming and configuration setting in Microservices. Developed by Alibaba in the early 2010s, Nacos was fast adopted by plenty of big web companies like Alibaba and Tencent. This tutorial will introduce the usage and function of Nacos in practical examples.

Installation

Nacos supports all main-stream platforms like Windows and Linux; It also could be deployed on docker for more straightforward usage. You can visit Nacos official GitHub website (https://github.com/alibaba/nacos) for more information. Here, I will use Windows as an example.

After unzipping the installation file, we can find many different kinds of configuration files. Firstly, we should open the application file using a text editor.

#*************** Spring Boot Related Configurations ***************#
### Default web context path:
server.servlet.contextPath=/nacos
### Default web server port:
server.port=8848

The server port is what we need to remember to log in Nacos server. 8848 is the default port which is the altitude of Mountain Everest. 

Afterward, we can enter the Nacos server in the browser using  http://localhost:8848/nacos

Java Code

After starting our Nacos server, we can open serval Tomcat servers in Java.

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        username: nacos
        password: nacos

spring.application.name=nacosone

First, this is the setting needed to connect to the Nacos server. We can attach this to our properties or yaml files. Then the most crucial part is that we need to give each server an application name for the Nacos to identify each one. After starting our nacosone server, we can visit Nacos index to see what happened.

 Here, we can see the server name, group name and the number of servers within the same name. The reason why we can assign the same name on different servers is that we can apply load balance using Nacos.

Load Balance

@RestController
public class NginxOneController {

    @GetMapping("three")
    public String three(){
        return  "three";
    }

}

//The code below belongs to another server with the same name


@RestController
public class NginxOneController {

    @GetMapping("three")
    public String three(){
        return  "three3";
    }

}

Here are two bare servers that contain a simple controller. One thing worth mentioning is that those two servers need to have the same application name. Finally, we can use OpenFeign as a tool to do the load-balancing job.

@RestController
public class ClientController {

    @Resource
    private IClientService clientService;

    @GetMapping("testInfo")
    public String test(){
        return clientService.three();
    }


}

There are many other features of Nacos, like exception handling and configuration management. I will talk about them in the following tutorial!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值