当前位置:网站首页>Explanation of service registration and discovery API of Nacos series

Explanation of service registration and discovery API of Nacos series

2022-06-13 05:38:00 codain

Nacos Provided by itself SDK and API To complete service registration and discovery ,SDK In essence, it is aimed at HTTP Requested one layer encapsulation , Because the server only provides REST Interface , So we take the specification , To get to know nacos Core interface of :

1、 Registration instance

1、 effect : Register the service address information to Nacos Server in

2、API:/nacos/v1/ns/instance   (POST)

3、SDK:

void registerInstance(String serviceName , String ip , int port) throws NacosException;

void registerInstance(String serviceName, String ip,int port,String clusterName) throws NacosException;

void registerInstance(String serviceName,Instance instance) throws NacosException;

4、 Explain :

serviceName: The service name , Equivalent to the configuration file spring.application.name

ip: Service instance ip

port: Service instance port

clusterName: Cluster name , Identify which cluster the current service instance belongs to

instance: Instance attributes , In fact, the above parameters are encapsulated into an object

5、 Call mode

NamingService naming=NamingFactory.createNamingService(System.getProperty("serveAddr"));
naming.registerInstance("nacos_name","192.168.80.1",8080,"DEFAULT");

2、 Get all instances

1、 effect : According to the service name from Nacos Get the instance collection of all services on the registry

2、API: /nacos/v1/ns/instance/list    (GET)

3、SDK:

List<Instance> getAllInstances(String serviceName) throws NacosException;
List<Instance> getAllInstances(String serviceName,List<String> cluster) throws NacosException;

4、 Explain :

serviceName: The service name

cluster: Cluster list , The input is one String aggregate

5、 Call mode :

NamingService naming=NamingFactory.createNamingService(System.getProperty("serveAddr"));
System.out.println(naming.getAllInstances("nacos_name),true);

3、 Service monitoring

1、 effect : In order to make the client aware of the changes of the service provider instance in real time , amount to Eureka Heartbeat mechanism in

2、API:/nacos/v1/ns/instance/list   (GET) 

3、SDK:

void subscribe(String serviceName,EventListener listener) throws NacosException;
void subscribe(String serviceName,List<String> clusters,EventListener listener) throw NacosException;

4、 Explain :

EventListener: When an exception occurs in the provider service instance , For example, the upper and lower limits , An event callback will be called

There are two ways to monitor : The first one is : Client calls /nacos/v1/ns/instance/list Conduct scheduled polling

                              The second kind : be based on DatagramSocket Of UDP agreement , Realize the active push of the server

原网站

版权声明
本文为[codain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280508407410.html