Dubbo connection to zookeeper registry because of a broken network cause thread infinite wait problem "go"

Source: Internet
Author: User

Recently maintained the system switched the network environment, from Unicom to the telecommunications network, because some filtering rules caused the system not connected to the Zookeeper server (application system machine in Shenzhen, Network for telecommunications lines, zookeeper server in Beijing, network for China Unicom Line), Because I am not a maintenance personnel also do not understand the operation and maintenance related technology, so the troubleshooting for a long time also do not know the reason, finally helpless under the Shenzhen side of the network cut back to the Unicom, the system back to normal.

But because this accident embodies a very serious problem, that is, when the zookeeper registry is not connected to the Dubbo thread will wait indefinitely, because the system has some scheduled tasks will be more frequent to open a new thread connection Dubbo, so the result is tomcat a while the thread pool is full, Other features that do not rely on Dubbo are also blocked from being used.

So we need to solve a problem, that is, in the case of a broken network to ensure that the application can run (there are many features do not rely on the external network), at first I thought Dubbo should have the Zkclient connection related timeout configuration, the results have been found for a long time did not find, Later debug Dubbo source code found there is no set timeout, zkclient default timeout time is integer.max_value, almost equal to infinite wait, so helpless under had to rewrite the Dubbo zookeeperclient implementation, Fortunately the extensibility of Dubbo is very good, the SPI-based extension is very convenient, here is my extension code:

1, add a file Com.alibaba.dubbo.remoting.zookeeper.ZookeeperTransporter placed in the project's Meta-inf/dubbo/internal folder, Dubbo will automatically scan to this file. The content of the file is simple: zkclient=com.gwall.zookeeper.zkclientzookeepertransporter 
replaces Dubbo's original default implementation with my implementation.  
2, Zkclientzookeepertransporter is not the code I want to replace, I want to replace the Zkclientzookeepertransporter member variable zookeeperclient, Only Dubbo on the Zookeepertransporter added SPI annotations, so have to do so, Zkclientzookeepertransporter code copied over.  
3, use your own zkclientzookeeperclient in Zkclientzookeepertransporter, the code is as follows:

Package Com.gwall.zookeeper;import java.util.list;import org. i0itec.zkclient.izkchildlistener;import org. i0itec.zkclient.izkstatelistener;import org. i0itec.zkclient.zkclient;import org. i0itec.zkclient.exception.zknonodeexception;import org. I0itec.zkclient.exception.zknodeexistsexception;import Org.apache.zookeeper.watcher.event.keeperstate;import Com.alibaba.dubbo.common.url;import Com.alibaba.dubbo.remoting.zookeeper.childlistener;import Com.alibaba.dubbo.remoting.zookeeper.statelistener;import com.alibaba.dubbo.remoting.zookeeper.support.abstractzookeeperclient;/** * Modify the Zkclientzookeeperclient provided by Dubbo * The main purpose is to increase the time-out of a connection zookeeper, to avoid the zkclient default infinite wait * @author LONG.ZR * */public class Zkclientzookeeperclient extends    abstractzookeeperclient<izkchildlistener> {private final zkclient client;    Private volatile keeperstate state = keeperstate.syncconnected;        Public zkclientzookeeperclient (URL url) {super (URL); Set the time-out to 5000 milliseconds client = new Zkclient (Url.getbacKupaddress (), 5000); Client.subscribestatechanges (New Izkstatelistener () {public void handlestatechanged (Keeperstate state) throws                Exception {ZkclientZookeeperClient.this.state = state;                if (state = = keeperstate.disconnected) {statechanged (statelistener.disconnected);                } else if (state = = keeperstate.syncconnected) {statechanged (statelistener.connected); }} public void Handlenewsession () throws Exception {statechanged (Statelistener.rec            onnected);    }        });        } public void Createpersistent (String path) {try {client.createpersistent (path, true); } catch (Zknodeexistsexception e) {}} public void Createephemeral (String path) {try {cl        Ient.createephemeral (path);       } catch (Zknodeexistsexception e) {}} public void Delete (String path) { try {client.delete (path);            } catch (Zknonodeexception e) {}} public list<string> GetChildren (String path) {try {        return Client.getchildren (path);        } catch (Zknonodeexception e) {return null;    }} public Boolean isconnected () {return state = = keeperstate.syncconnected;    } public void Doclose () {client.close (); } public Izkchildlistener Createtargetchildlistener (String path, final Childlistener listener) {return new IZKC                    Hildlistener () {public void Handlechildchange (String parentpath, list<string> currentchilds)            Throws Exception {listener.childchanged (Parentpath, currentchilds);    }        }; } public list<string> Addtargetchildlistener (String path, final Izkchildlistener listener) {return client    . Subscribechildchanges (path, listener); } public void Removetargetchildlistener (String Path, Izkchildlistener listener) {client.unsubscribechildchanges (path, listener); }}

Framework/Platform Composition:
MAVEN+SPRINGMVC + Mybatis + Shiro (permissions) + Tiles (template) +activemq (Message Queuing) + Rest (service) + WebService (service) + EHcache (cache) + Quartz (timed schedule) + HTML5 (supports PC, IOS, Android)

User Rights system:
Organizational structure: role, user, user group, organization; permission points: Pages, methods, buttons, data permissions, hierarchical authorizations

New Project Management experience:
Rapid prototyping system, component tree, version control, module porting, collaborative development, real-time monitoring, release management

Sustainable integration:
All components are portable, customizable, expandable, and development results accumulate, creating a virtuous cycle of sustainable development

Platform Platform Support:
Windows XP, Windows 7, Windows 10, Linux, Unix

Server container:
Tomcat 5/6/7, Jetty, JBoss, WebSphere 8.5

Dubbo connection to zookeeper registry because of a broken network cause thread infinite wait problem "go"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.