Skip to content

Commit

Permalink
fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenChongze authored and ChenChongze committed Feb 14, 2017
2 parents 800d5c8 + a2f69b9 commit 5982bdc
Show file tree
Hide file tree
Showing 14 changed files with 778 additions and 712 deletions.
6 changes: 4 additions & 2 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ pigeon.provider.applimit.enable=true
如果使用了点评内部的lion配置中心,相比本地配置管理上更加方便,在lion管理端进行配置的统一管理,无需在每台机器上的properties文件里进行配置

lion配置需要按前面依赖里提到的引入以下依赖:

```xml
<dependency>
<groupId>com.dianping</groupId>
<artifactId>pigeon-config-lion</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

```
如果要设置某个应用级的pigeon配置,需要在lion里增加相应的pigeon配置,如pigeon内部有一个全局默认配置pigeon.provider.applimit.enable,值为false

如果某个应用xxx-service(这个应用名就是app.properties里的app.name)想修改这个默认配置,那么可以在lion里增加一个key:xxx-service.pigeon.provider.applimit.enable,设置为true
Expand All @@ -191,6 +191,8 @@ com.xxx....XXXConfigManager

## 快速入门

本文档相关代码示例也可以参考[pigeon-demo](https://github.com/dianping/pigeon-demo)项目

### 定义服务

定义服务接口: (该接口需单独打包,在服务提供方和调用方共享)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dianping.pigeon.remoting.invoker.config.InvokerConfig;
import org.apache.commons.lang.StringUtils;
import com.dianping.pigeon.log.Logger;
import org.springframework.aop.support.AopUtils;

import com.dianping.pigeon.config.ConfigManager;
import com.dianping.pigeon.config.ConfigManagerLoader;
Expand All @@ -30,10 +29,12 @@
import com.dianping.pigeon.console.status.checker.GlobalStatusChecker;
import com.dianping.pigeon.console.status.checker.ProviderStatusChecker;
import com.dianping.pigeon.console.status.checker.StatusChecker;
import com.dianping.pigeon.log.Logger;
import com.dianping.pigeon.log.LoggerLoader;
import com.dianping.pigeon.registry.RegistryManager;
import com.dianping.pigeon.remoting.ServiceFactory;
import com.dianping.pigeon.remoting.common.util.ServiceConfigUtils;
import com.dianping.pigeon.remoting.invoker.config.InvokerConfig;
import com.dianping.pigeon.remoting.provider.ProviderBootStrap;
import com.dianping.pigeon.remoting.provider.Server;
import com.dianping.pigeon.remoting.provider.config.ProviderConfig;
Expand All @@ -46,7 +47,6 @@
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.aop.support.AopUtils;

/**
* @author sean.wang
Expand Down Expand Up @@ -114,7 +114,7 @@ public Map<String, ProviderConfig<?>> getServiceProviders() {
return ServiceFactory.getAllServiceProviders();
}

public Map<InvokerConfig<?>, Object> getInvokerConfigs(){
public Map<InvokerConfig<?>, Object> getInvokerConfigs() {
return ServiceFactory.getAllServiceInvokers();
}

Expand Down Expand Up @@ -174,8 +174,10 @@ protected boolean initServicePage(HttpServletRequest request, HttpServletRespons
page.setAppName(configManager.getAppName());
page.setStartTime(ProviderBootStrap.getStartTime() + "");
page.setValidate("" + isValidate);
page.setGovernorUrl(configManager.getStringValue("pigeon.governor.address")
+ "/services/" + configManager.getAppName());
String governorAddr = configManager.getStringValue("pigeon.governor.address");
if (StringUtils.isNotBlank(governorAddr)) {
page.setGovernorUrl(governorAddr + "/services/" + configManager.getAppName());
}
this.model = page;
return true;
}
Expand Down Expand Up @@ -220,7 +222,8 @@ public String getContentType() {
return "text/html; charset=UTF-8";
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(getContentType());
response.setStatus(HttpServletResponse.SC_OK);

Expand All @@ -232,8 +235,8 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
doGet(req, resp);
}

protected void generateView(HttpServletRequest request, HttpServletResponse response) throws IOException,
ServletException {
protected void generateView(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
Template temp = cfg.getTemplate(getView());
boolean result = initServicePage(request, response);
if (result) {
Expand Down
9 changes: 3 additions & 6 deletions pigeon-console/src/main/resources/Service.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,12 @@
</SCRIPT>
</head>
<body style="font-size:62.5%;padding-top:10px;padding-left:10px;">
<div class="row">
<div class="span12">
admin portal: <a href="${governorUrl}" target="_blank">${governorUrl}</a>
</div>
</div>
<div class="row">
<div class="span8" style="overflow:hidden">
<div>
<#if governorUrl != "">
<p>admin portal: <a href="${governorUrl}" target="_blank">${governorUrl}</a></p>
</#if>
<p>pigeon services registered at port ${port}</p>
<p>version: ${version}</p>
<p>env: ${environment}</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
import java.util.concurrent.Executors;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.curator.CuratorZookeeperClient;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.framework.state.ConnectionStateListener;
import org.apache.curator.retry.RetryNTimes;
import com.dianping.pigeon.log.Logger;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.data.Stat;

import com.dianping.pigeon.config.ConfigChangeListener;
import com.dianping.pigeon.config.ConfigManager;
import com.dianping.pigeon.config.ConfigManagerLoader;
import com.dianping.pigeon.log.Logger;
import com.dianping.pigeon.log.LoggerLoader;
import com.dianping.pigeon.monitor.Monitor;
import com.dianping.pigeon.monitor.MonitorLoader;
Expand All @@ -36,11 +37,11 @@ public class CuratorClient {

private CuratorFramework client;

private int retries = configManager.getIntValue("pigeon.registry.curator.retries", Integer.MAX_VALUE);
private volatile int retries = configManager.getIntValue("pigeon.registry.curator.retries", Integer.MAX_VALUE);

private int retryInterval = configManager.getIntValue("pigeon.registry.curator.retryinterval", 3000);
private volatile int retryInterval = configManager.getIntValue("pigeon.registry.curator.retryinterval", 3000);

private int retryLimit = configManager.getIntValue("pigeon.registry.curator.retrylimit", 500);
private volatile int retryLimit = configManager.getIntValue("pigeon.registry.curator.retrylimit", 50);

private int sessionTimeout = configManager.getIntValue("pigeon.registry.curator.sessiontimeout", 30 * 1000);

Expand All @@ -54,19 +55,35 @@ public class CuratorClient {

private static Monitor monitor = MonitorLoader.getMonitor();

private String address;
private static final String KEY_REGISTRY_ADDRESS = "pigeon.registry.address";

private volatile String address;

private final String EVENT_NAME = "Pigeon.registry";

public CuratorClient(String zkAddress) throws Exception {
newCuratorClient(zkAddress);
}

public CuratorClient() throws Exception {
String zkAddress = configManager.getStringValue(KEY_REGISTRY_ADDRESS);
newCuratorClient(zkAddress);
}

private void newCuratorClient(String zkAddress) throws Exception {
if (StringUtils.isBlank(zkAddress)) {
throw new IllegalArgumentException("zookeeper address is required");
}
logger.info("start to initialize zookeeper client:" + zkAddress);
this.address = zkAddress;
newCuratorClient();
curatorStateListenerThreadPool.execute(new CuratorStateListener());
configManager.registerConfigChangeListener(new InnerConfigChangeListener());
logger.info("succeed to initialize zookeeper client:" + zkAddress);
}

private boolean newCuratorClient() throws InterruptedException {
logger.info("begin to create zookeeper client");
logger.info("begin to create zookeeper client:" + address);
// CuratorFramework client = CuratorFrameworkFactory.newClient(address,
// sessionTimeout, connectionTimeout,
// new MyRetryPolicy(retries, retryInterval));
Expand All @@ -89,11 +106,12 @@ public void stateChanged(CuratorFramework client, ConnectionState newState) {
CuratorFramework oldClient = this.client;
this.client = client;
close(oldClient);
logger.info("succeed to create zookeeper client, connected:" + isConnected);

if (isConnected) {
logger.info("succeed to connect to zookeeper:" + address);
monitor.logEvent(EVENT_NAME, "zookeeper:rebuild_success", "");
} else {
logger.warn("unable to connect to zookeeper:" + address);
monitor.logEvent(EVENT_NAME, "zookeeper:rebuild_failure", "");
}

Expand All @@ -104,6 +122,19 @@ public CuratorFramework getClient() {
return client;
}

public boolean isConnected() {
final CuratorFramework cf = getClient();
if (cf != null) {
try {
return cf.getZookeeperClient().getZooKeeper().getState().isConnected()
&& cf.getZookeeperClient().isConnected();
} catch (Exception e) {
return false;
}
}
return false;
}

private class CuratorStateListener implements Runnable {

private final Logger logger = LoggerLoader.getLogger(CuratorStateListener.class);
Expand All @@ -114,14 +145,13 @@ public void run() {
boolean isSuccess = true;
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(sleepTime);
Thread.sleep(sleepTime * (1 + RandomUtils.nextInt(20)));
final CuratorFramework cf = getClient();
if (cf != null) {
int retryCount = ((MyRetryPolicy) cf.getZookeeperClient().getRetryPolicy()).getRetryCount();
boolean isConnected = false;
try {
isConnected = cf.getZookeeperClient().getZooKeeper().getState().isConnected()
&& cf.getZookeeperClient().isConnected();
isConnected = isConnected();
} catch (Exception e) {
logger.info("error with zookeeper client's connection:" + e.toString());
}
Expand All @@ -135,7 +165,8 @@ public void run() {
} else {
failCount++;
if (retryCount > 0) {
logger.info("zookeeper client's retries:" + retryCount);
logger.info("zookeeper client's retries:" + retryCount + ", fails:" + failCount
+ ", limit:" + retryLimit);
}
}
if (failCount > retryLimit) {
Expand All @@ -152,13 +183,14 @@ public void run() {
}
}

private boolean rebuildCuratorClient() throws InterruptedException {
boolean isSuccess = newCuratorClient();
if (isSuccess) {
RegistryEventListener.connectionReconnected();
}
return isSuccess;
}

private boolean rebuildCuratorClient() throws InterruptedException {
boolean isSuccess = newCuratorClient();
if (isSuccess) {
RegistryEventListener.connectionReconnected();
}
return isSuccess;
}

private static class MyRetryPolicy extends RetryNTimes {
Expand All @@ -173,7 +205,7 @@ public MyRetryPolicy(int n, int sleepMsBetweenRetries) {
@Override
protected int getSleepTimeMs(int retryCount, long elapsedTimeMs) {
this.retryCount = retryCount;
return sleepMsBetweenRetries;
return sleepMsBetweenRetries * (1 + RandomUtils.nextInt(20));
}

public int getRetryCount() {
Expand Down Expand Up @@ -385,6 +417,15 @@ public void onKeyUpdated(String key, String value) {
retryLimit = Integer.valueOf(value);
} catch (RuntimeException e) {
}
} else if (key.endsWith(KEY_REGISTRY_ADDRESS)) {
address = value;
logger.info("registry address changed:" + address);
try {
Thread.sleep(RandomUtils.nextInt(180) * 1000);
rebuildCuratorClient();
} catch (Exception e) {
logger.warn("rebuild curator client failed:", e);
}
}
}

Expand All @@ -400,7 +441,7 @@ public void onKeyRemoved(String key) {

public String getStatistics() {
CuratorZookeeperClient client = getClient().getZookeeperClient();
return new StringBuilder().append("connected:").append(client.isConnected()).append(", retries:")
return new StringBuilder().append("address:").append(client.getCurrentConnectionString()).append(", connected:").append(isConnected()).append(", retries:")
.append(((MyRetryPolicy) client.getRetryPolicy()).getRetryCount()).toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,10 @@ public void init() {
synchronized (this) {
if (!inited) {
try {
String zkAddress = configManager.getStringValue(Constants.KEY_REGISTRY_ADDRESS);
if (StringUtils.isBlank(zkAddress)) {
throw new IllegalArgumentException("zookeeper address is required");
client = new CuratorClient();
if (!client.isConnected()) {
throw new IllegalStateException("unable to connect to zookeeper");
}
logger.info("start to initialize zookeeper client:" + zkAddress);
client = new CuratorClient(zkAddress);
logger.info("succeed to initialize zookeeper client:" + zkAddress);
inited = true;
} catch (Exception ex) {
logger.error("failed to initialize zookeeper client", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.dianping.pigeon.registry.Registry;
import com.dianping.pigeon.registry.exception.RegistryException;
import com.dianping.pigeon.registry.listener.RegistryEventListener;
import com.dianping.pigeon.registry.util.Constants;

public class CuratorRegistryTest {

Expand All @@ -28,7 +27,7 @@ public class CuratorRegistryTest {
@BeforeClass
public static void startTestServer() throws Exception {
server = new TestingServer();
ConfigManagerLoader.getConfigManager().setLocalStringValue(Constants.KEY_REGISTRY_ADDRESS,
ConfigManagerLoader.getConfigManager().setLocalStringValue("pigeon.registry.address",
server.getConnectString());
/*ConfigManagerLoader.getConfigManager().setLocalStringValue(Constants.KEY_REGISTRY_ADDRESS,
"10.66.15.109:2181");*/
Expand Down
Loading

0 comments on commit 5982bdc

Please sign in to comment.