Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zookeeper graceful shutdown #17

Merged
merged 7 commits into from
Apr 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/wiki/en_quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ UI backend [http://localhost:8500/ui](http://localhost:8500/ui)
4. After the server starts, you SHOULD call hearbeat switcher explicitly in order to start heartbeat for Consul.

```java
MotanSwitcherUtil.setSwitcher(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true)
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true)
```

5. Go to [UI backend](http://localhost:8500/ui). Verify whether the service is normal.
Expand Down Expand Up @@ -265,7 +265,7 @@ Install and start ZooKeeper:
4. After the server starts, you SHOULD call hearbeat switcher explicitly in order to start heartbeat for Zookeeper.

```java
MotanSwitcherUtil.setSwitcher(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true)
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true)
```

5. Start client, call service.
Expand Down
10 changes: 8 additions & 2 deletions docs/wiki/zh_quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ ui后台 [http://localhost:8500/ui](http://localhost:8500/ui)
4. server程序启动后,需要显式调用心跳开关,注册到consul。

```java
MotanSwitcherUtil.setSwitcher(ConsulConstants.NAMING_PROCESS_HEARTBEAT_SWITCHER, true)
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true)
```

5. 进入[ui后台](http://localhost:8500/ui)查看服务是否正常提供调用
Expand Down Expand Up @@ -261,7 +261,13 @@ ui后台 [http://localhost:8500/ui](http://localhost:8500/ui)
<motan:service interface="quickstart.FooService" ref="serviceImpl" registry="my_zookeeper" export="8002" />
```

4. 启动client,调用服务
4. server程序启动后,需要显式调用心跳开关,注册到zookeeper。

```java
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true)
```

5. 启动client,调用服务


[maven]:https://maven.apache.org
Expand Down
6 changes: 2 additions & 4 deletions docs/wiki/zh_userguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,16 +352,14 @@ motan:referer中的basicService属性用来标识引用哪个motan:basicReferer

# 运维及监控
## 优雅的停止服务
Motan支持在Consul集群环境下优雅的关闭节点,当需要关闭或重启节点时,可以先将待上线节点从集群中摘除,避免直接关闭影响正常请求。
Motan支持在Consul、ZooKeeper集群环境下优雅的关闭节点,当需要关闭或重启节点时,可以先将待上线节点从集群中摘除,避免直接关闭影响正常请求。

待关闭节点需要调用以下代码,建议通过servlet或业务的管理模块进行该调用。

```java
MotanSwitcherUtil.setSwitcher(ConsulConstants.NAMING_PROCESS_HEARTBEAT_SWITCHER, false)
MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true)
```

> 注意:Zookeeper模块此功能正在开发。

## 管理后台
管理后台主要包括RPC服务查询、流量切换、Motan指令设置等功能,需使用ZooKeeper作为注册中心

Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ public class ConsulConstants {
public static int DEFAULT_LOOKUP_INTERVAL = 30000;

/**
* 心跳开关。与configservice一致
* consul心跳检测开关
*/
public static final String NAMING_PROCESS_HEARTBEAT_SWITCHER = "feature.configserver.heartbeat";
@Deprecated
public static final String CONSUL_PROCESS_HEARTBEAT_SWITCHER = "feature.consul.heartbeat";

/**
* consul block 查询时 block的最长时间,单位,分钟
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* consul 心跳管理类。 rpc服务把需要设置passing状态的serviceid注册到此类,
* 此类会定时对注册的serviceid设置passing状态(实际是对servcieid对应对checkid设置passing状态),
* 从而完成servivce的心跳。 心跳开关名为feature.configserver.heartbeat,
* 从而完成servivce的心跳。
* 开关开启后会进行心跳,开关关闭则停止心跳。
*
* @author zhanglei
Expand All @@ -34,12 +34,6 @@ public class ConsulHeartbeatManager {
// 开关检查次数。
private int switcherCheckTimes = 0;

static {
// 心跳开关默认为关闭状态。rpc服务启动正常后,通过设置开关状态为开启,打开心跳,对外提供rpc服务
MotanSwitcherUtil.initSwitcher(
ConsulConstants.NAMING_PROCESS_HEARTBEAT_SWITCHER, false);
}

public ConsulHeartbeatManager(MotanConsulClient client) {
this.client = client;
heartbeatExecutor = Executors.newSingleThreadScheduledExecutor();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.weibo.api.motan.registry.zookeeper;

enum ZkNodeType {
AVAILABLE_SERVER,
UNAVAILABLE_SERVER,
CLIENT
}
Loading