springboot使用管理员(管理员)实现注册发现与负载均衡的方法

  介绍

小编给大家分享一下springboot使用管理员(管理员)实现注册发现与负载均衡的方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获、下面让我们一起去了解一下吧!

最简单的实现服务高可用的方法就是集群化,也就是分布式部署,但是分布式部署会带来一些问题。比如:

1,各个实例之间的协同(锁)

2,负载均衡

3,热删除

这里通过一个简单的实例来说明如何解决注册发现和负载均衡。

,

1,先解决依赖,这里只给出zk相关的依赖,砰的一声。xml如下

 <依赖>
   org.apache.zookeeper 
  动物园管理员
  <版本> 3.4.8> 的依赖
  <依赖>
   org.apache.curator 
  curator-recipes 
  <版本> 2.9.1版本> 的依赖
  
  <依赖>
   org.apache.curator 
  curator-client 
  <版本> 2.9.1版本  

,

2, ZkClient

这里使用的是馆长,馆长是对管理员的简单封装,提供了一些集成的方法,或者是提供了更优雅的api,举例来说

zk的创建(路径、模式、acl数据)方法==馆长创建().withMode(模式).forPath(路径)调用链

<>之前包com.dqa.prometheus.client.zookeeper;进口org.apache.curator.RetryPolicy;进口org.apache.curator.framework.CuratorFramework;进口org.apache.curator.framework.CuratorFrameworkFactory;进口org.apache.curator.retry.ExponentialBackoffRetry;进口org.apache.zookeeper.CreateMode;进口org.slf4j.Logger;进口org.slf4j.LoggerFactory;进口java.net.InetAddress;进口java.util.ArrayList;进口并不知道;公共类ZkClient{私人最后日志记录器=LoggerFactory.getLogger (this.getClass ());私人CuratorFramework客户;私人字符串zookeeperServer;私人int sessionTimeoutMs;私人int connectionTimeoutMs;私人int baseSleepTimeMs;私人int maxRetries;公共空间setZookeeperServer(字符串zookeeperServer) {。zookeeperServer=zookeeperServer;   }公共字符串getZookeeperServer(){返回zookeeperServer;   }公共空setSessionTimeoutMs (int sessionTimeoutMs) {。sessionTimeoutMs=sessionTimeoutMs;   }公共int getSessionTimeoutMs(){返回sessionTimeoutMs;   }公共空setConnectionTimeoutMs (int connectionTimeoutMs) {。connectionTimeoutMs=connectionTimeoutMs;   }公共int getConnectionTimeoutMs(){返回connectionTimeoutMs;   }公共空setBaseSleepTimeMs (int baseSleepTimeMs) {。baseSleepTimeMs=baseSleepTimeMs;   }公共int getBaseSleepTimeMs(){返回baseSleepTimeMs;   }公共空setMaxRetries (int maxRetries) {。maxRetries=maxRetries;   }公共int getMaxRetries(){返回maxRetries;   }公共空init () {   RetryPolicy RetryPolicy=new ExponentialBackoffRetry (baseSleepTimeMs maxRetries);   客户=CuratorFrameworkFactory.builder () .connectString (zookeeperServer) .retryPolicy (retryPolicy)   .sessionTimeoutMs (sessionTimeoutMs) .connectionTimeoutMs (connectionTimeoutMs) .build ();   client.start ();   }公共无效停止(){   client.close ();   }公共CuratorFramework getClient(){返回客户端;   {尝试{}公共无效注册()   字符串rootPath=" +“服务”;   .getHostAddress字符串hostAddress=InetAddress.getLocalHost () ();   字符串serviceInstance=捌章廾仔匏埂?“-”+ hostAddress +“-”;   .creatingParentsIfNeeded client.create () () .withMode (CreateMode.EPHEMERAL_SEQUENTIAL)。forPath (rootPath + " + serviceInstance);   }捕捉(异常e) {   记录器。错误(“注册出错”,e);   }   }<字符串>公共列表调用getChildren (String路径){   列表<字符串> childrenList=new ArrayList <> ();尝试{   childrenList=client.getChildren () .forPath(路径);   }捕捉(异常e) {   记录器。错误(“获取子节点出错”,e);   }返回childrenList;   }公共int getChildrenCount (String路径){返回调用getChildren(路径).size ();   {}公共列表<字符串> getinstance()返回的getChildren(“/服务”);   }公共int getInstancesCount(){返回getinstance () .size ();   }   }

2,配置如下

<>之前包com.dqa.prometheus.configuration;进口com.dqa.prometheus.client.zookeeper.ZkClient;进口org.springframework.beans.factory.annotation.Value;进口org.springframework.context.annotation.Bean;进口org.springframework.context.annotation.Configuration;      @Configurationpublic类ZkConfiguration {   @ value (" $ {zookeeper.server} ")私人字符串zookeeperServer;   @ value ((" $ {zookeeper.sessionTimeoutMs} "))私人int sessionTimeoutMs;   @ value (" $ {zookeeper.connectionTimeoutMs} ")私人int connectionTimeoutMs;   @ value (" $ {zookeeper.maxRetries} ")私人int maxRetries;   @ value (" $ {zookeeper.baseSleepTimeMs} ")私人int baseSleepTimeMs;      @ bean (initMethod=癷nit”, destroyMethod=巴V埂?公开ZkClient ZkClient () {   ZkClient ZkClient=new ZkClient ();   zkClient.setZookeeperServer (zookeeperServer);   zkClient.setSessionTimeoutMs (sessionTimeoutMs);   zkClient.setConnectionTimeoutMs (connectionTimeoutMs);   zkClient.setMaxRetries (maxRetries);   zkClient.setBaseSleepTimeMs (baseSleepTimeMs);返回zkClient;   }      }

springboot使用管理员(管理员)实现注册发现与负载均衡的方法