Skip to content

Commit

Permalink
feat: zookeeper支持优雅停机
Browse files Browse the repository at this point in the history
  • Loading branch information
zyseap committed Apr 26, 2016
1 parent ee89da0 commit b3b0681
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@


/**
*
* Register service to Restery center.
*
* @author fishermen
Expand All @@ -33,14 +32,14 @@ public interface RegistryService {

/**
* register service to registry
*
*
* @param url
*/
void register(URL url);

/**
* unregister service to registry
*
*
* @param url
*/
void unregister(URL url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public void onValueChanged(String key, Boolean value) {
unavailable(null);
}
}

}
});
}
Expand Down Expand Up @@ -154,24 +153,21 @@ public Collection<URL> getRegisteredServiceUrls() {
@Override
public void available(URL url) {
LoggerUtil.info("[{}] Url ({}) will set to available to Registry [{}]", registryClassName, url, registryUrl.getIdentity());
if(url != null) {
if (url != null) {
doAvailable(removeUnnecessaryParmas(url.createCopy()));
} else {
doAvailable(null);
}
}



@Override
public void unavailable(URL url) {
LoggerUtil.info("[{}] Url ({}) will set to unavailable to Registry [{}]", registryClassName, url, registryUrl.getIdentity());
if(url == null) {
if (url != null) {
doUnavailable(removeUnnecessaryParmas(url.createCopy()));
} else {
doUnregister(null);
doUnavailable(null);
}

}

protected List<URL> getCachedUrls(URL url) {
Expand Down Expand Up @@ -221,7 +217,7 @@ protected void notify(URL refUrl, NotifyListener listener, List<URL> urls) {

/**
* 移除不必提交到注册中心的参数。这些参数不需要被client端感知。
*
*
* @param url
*/
private URL removeUnnecessaryParmas(URL url) {
Expand All @@ -244,5 +240,4 @@ private URL removeUnnecessaryParmas(URL url) {

protected abstract void doUnavailable(URL url);


}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/**
* @author maijunsheng
* @version 创建时间:2013-6-14
*
*
*/
@SpiMeta(name = "localSwitcherService")
public class LocalSwitcherService implements SwitcherService {
Expand Down Expand Up @@ -94,8 +94,8 @@ public void registerListener(String switcherName, SwitcherListener listener) {
synchronized (listenerMap) {
if (listenerMap.get(switcherName) == null) {
List listeners = Collections.synchronizedList(new ArrayList());
listeners.add(listener);
listenerMap.put(switcherName, listeners);
listeners.add(listener);
} else {
List listeners = listenerMap.get(switcherName);
if (!listeners.contains(listener)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,23 @@

package com.weibo.motan.demo.client;

import com.weibo.motan.demo.service.MotanDemoService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.weibo.motan.demo.service.MotanDemoService;

public class DemoRpcClient {

public static void main(String[] args) throws InterruptedException {

ApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] { "classpath:motan_demo_client.xml" });
public static void main(String[] args) throws InterruptedException {

MotanDemoService service = (MotanDemoService) ctx
.getBean("motanDemoReferer");
for(int i = 0; i < Integer.MAX_VALUE; i++){
System.out.println(service.hello("motan" + i));
Thread.sleep(500);
}
System.out.println("motan demo is finish.");
System.exit(0);

}

}
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:motan_demo_client.xml"});

MotanDemoService service = (MotanDemoService) ctx.getBean("motanDemoReferer");
for (int i = 0; i < Integer.MAX_VALUE; i++) {
System.out.println(service.hello("motan" + i));
Thread.sleep(500);
}
System.out.println("motan demo is finish.");
System.exit(0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@
-->

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:motan="http://api.weibo.com/schema/motan"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:motan="http://api.weibo.com/schema/motan"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd">

<!-- 注册中心配置 使用不同注册中心需要依赖对应的jar包。-->
<!--<motan:registry regProtocol="local" name="registry"/>-->
<motan:registry regProtocol="consul" name="registry" address="127.0.0.1:8500"/>
<!--<motan:registry regProtocol="zookeeper" name="registry" address="127.0.0.1:2181"/>-->
<!-- 注册中心配置 使用不同注册中心需要依赖对应的jar包。-->
<!--<motan:registry regProtocol="local" name="registry"/>-->
<!--<motan:registry regProtocol="consul" name="registry" address="127.0.0.1:8500"/>-->
<motan:registry regProtocol="zookeeper" name="registry" address="127.0.0.1:2181"/>

<!-- motan协议配置 -->
<motan:protocol default="true" name="motan" haStrategy="failover"
loadbalance="roundrobin" maxClientConnection="10" minClientConnection="2"/>
<!-- motan协议配置 -->
<motan:protocol default="true" name="motan" haStrategy="failover"
loadbalance="roundrobin" maxClientConnection="10" minClientConnection="2"/>

<!-- 通用referer基础配置 -->
<motan:basicReferer requestTimeout="200" accessLog="false"
retries="2" group="motan-demo-rpc" module="motan-demo-rpc"
application="myMotanDemo" protocol="motan" registry="registry"
id="motantestClientBasicConfig" throwException="false" check="true" />
<!-- 通用referer基础配置 -->
<motan:basicReferer requestTimeout="200" accessLog="false"
retries="2" group="motan-demo-rpc" module="motan-demo-rpc"
application="myMotanDemo" protocol="motan" registry="registry"
id="motantestClientBasicConfig" throwException="false" check="true"/>

<!-- 具体referer配置。使用方通过beanid使用服务接口类 -->
<motan:referer id="motanDemoReferer"
interface="com.weibo.motan.demo.service.MotanDemoService"
connectTimeout="300" requestTimeout="300" basicReferer="motantestClientBasicConfig"/>
<!-- 具体referer配置。使用方通过beanid使用服务接口类 -->
<motan:referer id="motanDemoReferer"
interface="com.weibo.motan.demo.service.MotanDemoService"
connectTimeout="300" requestTimeout="300" basicReferer="motantestClientBasicConfig"/>

</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
public class MotanDemoServiceImpl implements MotanDemoService {

public String hello(String name) {
System.out.println(name);
return "Hello " + name + "!";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

<!-- 注册中心配置 使用不同注册中心需要依赖对应的jar包。如果不使用注册中心,可以把check属性改为false,忽略注册失败。-->
<!--<motan:registry regProtocol="local" name="registry" />-->
<motan:registry regProtocol="consul" name="registry" address="127.0.0.1:8500"/>
<!--<motan:registry regProtocol="zookeeper" name="registry" address="127.0.0.1:2181"/>-->
<!--<motan:registry regProtocol="consul" name="registry" address="127.0.0.1:8500"/>-->
<motan:registry regProtocol="zookeeper" name="registry" address="127.0.0.1:2181"/>

<!-- 协议配置。为防止多个业务配置冲突,推荐使用id表示具体协议。-->
<motan:protocol id="demoMotan" default="true" name="motan"
Expand Down
Loading

0 comments on commit b3b0681

Please sign in to comment.