Nacos usage and service registration

Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Download: github.com/alibaba/nac…

IO/zh-CN /docs/…

Configuring the mysql Database

  • ${NACOS_HOME}/conf file nacos-mysql. SQL initialization
  • Add mysql database configuration to application. Properties file in ${NACOS_HOME}/conf
spring.datasource.platform=mysql
db.num=1
db.url.0=JDBC: mysql: / / 127.0.0.1:3306 / trs_cloud_config? characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTime zone=UTC
db.user.0=root
db.password.0=rootroot
Copy the code

Start the NacOS service

Go to the ${NACOS_HOME}/bin folder, enter CMD in the directory path, and then enter the following startup command

startup.cmd -m standalone
Copy the code

Or change the startup mode to standalone in the startup. CMD file, and double-click startup. CMD

set MODE="standalone"
Copy the code

After the service, can be accessed through http://127.0.0.1:8848/nacos nacos consoles, console is mainly used to enhance the service list, health management, service management, a distributed configuration management controls, further help developers to reduce the cost of micro management service application architecture.

Nacos service registration

Nacos provides SDK and Open Api requests to complete service registration and discovery. Since the server only provides REST interfaces, SDK is essentially the encapsulation of HTTP requests.

Registered instance

Register service address messages with Nacos Server

OPAPI: /nacos/v1/ns/instance(POST)
SDK:
void registerInstance(String serviceName,String ip,in port) throws NacosException;
void registerInstance(String serviceName,String ip,in port,String clusterName) throws NacosException;
void registerInstance(String serviceName,Instance instance) throws NacosException;
Copy the code

Related parameters:

  • ServiceName: indicates the serviceName

  • IP: IP address of the service instance

  • Port: indicates the port number of the service instance

  • ClusterName: indicates the clusterName to which the service instance belongs

  • Instance: encapsulates IP, port, and clusterName as an object.

Call method:

NamingService namingService=NamingFactory.createNamingService(Systm.getProperty("serverAddr"));
namingService.registerInstance("server1"."127.0.0.1".9090."DEFAULT");
Copy the code