Skip to content
Snippets Groups Projects
Commit 0e9188be authored by xujingfeng's avatar xujingfeng
Browse files

change the dependency of zookeeper and internal dubbo

parents 7b5a18f5 4e91b558
No related branches found
No related tags found
No related merge requests found
Showing
with 434 additions and 72 deletions
......@@ -322,4 +322,5 @@ Session.vim
tags
# Persistent undo
[._]*.un~
.flattened-pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>adaptive-loadbalance</artifactId>
<groupId>com.aliware.tianchi</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>bootstrap</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>com.aliware.tianchi</groupId>
<artifactId>workspace-gateway</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.aliware.tianchi</groupId>
<artifactId>workspace-provider</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.aliware.tianchi</groupId>
<artifactId>service-provider</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.aliware.tianchi;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author guohaoice@gmail.com
*/
public class ProviderApp {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:dubbo-provider.xml"});
context.start();
System.in.read(); // press any key to exit
}
}
......@@ -15,7 +15,6 @@
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>tianchi.1.0.0</version>
</dependency>
</dependencies>
......
......@@ -22,4 +22,11 @@
</modules>
<dependencies>
<dependency>
<groupId>com.aliware.tianchi</groupId>
<artifactId>internal-gateway</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -20,7 +20,43 @@
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.aliware.tianchi.MyConsumer</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
......@@ -7,13 +7,14 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
* @author guohaoice@gmail.com
*/
public class MyConsumer {
public static void main(String[] args) throws IOException {
public static void main(String[] args) throws IOException, InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:dubbo-consumer.xml"});
context.start();
HashInterface bean = context.getBean(HashInterface.class);
for (int i = 0; i < 10; i++) {
while(true){
Thread.sleep(1000);
System.out.println(bean.hash("hahaha"));
}
System.in.read(); // press any key to exit
// System.in.read(); // press any key to exit
}
}
......@@ -20,7 +20,44 @@
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.aliware.tianchi.DemoProviderMain</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
......@@ -3,14 +3,19 @@ package com.aliware.tianchi;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author guohaoice@gmail.com
*/
public class GatewayApp {
/** @author guohaoice@gmail.com */
public class DemoProviderMain {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-provider.xml"});
context.start();
// if(args.length!=3){
// throw new IllegalArgumentException("Bad argument number");
// }
// String salt=args[0];
// System.out.println(salt);
new EmbeddedZooKeeper(2181, false).start();
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"classpath:dubbo-provider.xml"});
context.start();
System.in.read(); // press any key to exit
}
}
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.aliware.tianchi;
import java.io.File;
import java.lang.reflect.Method;
import java.util.Properties;
import java.util.UUID;
import org.apache.zookeeper.server.ServerConfig;
import org.apache.zookeeper.server.ZooKeeperServerMain;
import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.SmartLifecycle;
import org.springframework.util.ErrorHandler;
import org.springframework.util.SocketUtils;
/**
* from: https://github.com/spring-projects/spring-xd/blob/v1.3.1.RELEASE/spring-xd-dirt/src/main/java/org/springframework/xd/dirt/zookeeper/ZooKeeperUtils.java
*
* Helper class to start an embedded instance of standalone (non clustered) ZooKeeper.
*
* NOTE: at least an external standalone server (if not an ensemble) are recommended, even for
// * {@link org.springframework.xd.dirt.server.singlenode.SingleNodeApplication}
*
* @author Patrick Peralta
* @author Mark Fisher
* @author David Turanski
*/
public class EmbeddedZooKeeper implements SmartLifecycle {
/**
* Logger.
*/
// private static final Logger logger = LoggerFactory.getLogger(EmbeddedZooKeeper.class);
/**
* ZooKeeper client port. This will be determined dynamically upon startup.
*/
private final int clientPort;
/**
* Whether to auto-start. Default is true.
*/
private boolean autoStartup = true;
/**
* Lifecycle phase. Default is 0.
*/
private int phase = 0;
/**
* Thread for running the ZooKeeper server.
*/
private volatile Thread zkServerThread;
/**
* ZooKeeper server.
*/
private volatile ZooKeeperServerMain zkServer;
/**
* {@link ErrorHandler} to be invoked if an Exception is thrown from the ZooKeeper server thread.
*/
private ErrorHandler errorHandler;
private boolean daemon = true;
/**
* Construct an EmbeddedZooKeeper with a random port.
*/
public EmbeddedZooKeeper() {
clientPort = SocketUtils.findAvailableTcpPort();
}
/**
* Construct an EmbeddedZooKeeper with the provided port.
*
* @param clientPort port for ZooKeeper server to bind to
*/
public EmbeddedZooKeeper(int clientPort, boolean daemon) {
this.clientPort = clientPort;
this.daemon = daemon;
}
/**
* Returns the port that clients should use to connect to this embedded server.
*
* @return dynamically determined client port
*/
public int getClientPort() {
return this.clientPort;
}
/**
* Specify whether to start automatically. Default is true.
*
* @param autoStartup whether to start automatically
*/
public void setAutoStartup(boolean autoStartup) {
this.autoStartup = autoStartup;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isAutoStartup() {
return this.autoStartup;
}
/**
* Specify the lifecycle phase for the embedded server.
*
* @param phase the lifecycle phase
*/
public void setPhase(int phase) {
this.phase = phase;
}
/**
* {@inheritDoc}
*/
@Override
public int getPhase() {
return this.phase;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isRunning() {
return (zkServerThread != null);
}
/**
* Start the ZooKeeper server in a background thread.
* <p>
* Register an error handler via {@link #setErrorHandler} in order to handle
* any exceptions thrown during startup or execution.
*/
@Override
public synchronized void start() {
if (zkServerThread == null) {
zkServerThread = new Thread(new ServerRunnable(), "ZooKeeper Server Starter");
zkServerThread.setDaemon(daemon);
zkServerThread.start();
}
}
/**
* Shutdown the ZooKeeper server.
*/
@Override
public synchronized void stop() {
if (zkServerThread != null) {
// The shutdown method is protected...thus this hack to invoke it.
// This will log an exception on shutdown; see
// https://issues.apache.org/jira/browse/ZOOKEEPER-1873 for details.
try {
Method shutdown = ZooKeeperServerMain.class.getDeclaredMethod("shutdown");
shutdown.setAccessible(true);
shutdown.invoke(zkServer);
}
catch (Exception e) {
throw new RuntimeException(e);
}
// It is expected that the thread will exit after
// the server is shutdown; this will block until
// the shutdown is complete.
try {
zkServerThread.join(5000);
zkServerThread = null;
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
// logger.warn("Interrupted while waiting for embedded ZooKeeper to exit");
// abandoning zk thread
zkServerThread = null;
}
}
}
/**
* Stop the server if running and invoke the callback when complete.
*/
@Override
public void stop(Runnable callback) {
stop();
callback.run();
}
/**
* Provide an {@link ErrorHandler} to be invoked if an Exception is thrown from the ZooKeeper server thread. If none
* is provided, only error-level logging will occur.
*
* @param errorHandler the {@link ErrorHandler} to be invoked
*/
public void setErrorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}
/**
* Runnable implementation that starts the ZooKeeper server.
*/
private class ServerRunnable implements Runnable {
@Override
public void run() {
try {
Properties properties = new Properties();
File file = new File(System.getProperty("java.io.tmpdir")
+ File.separator + UUID.randomUUID());
file.deleteOnExit();
properties.setProperty("dataDir", file.getAbsolutePath());
properties.setProperty("clientPort", String.valueOf(clientPort));
QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig();
quorumPeerConfig.parseProperties(properties);
zkServer = new ZooKeeperServerMain();
ServerConfig configuration = new ServerConfig();
configuration.readFrom(quorumPeerConfig);
zkServer.runFromConfig(configuration);
}
catch (Exception e) {
if (errorHandler != null) {
errorHandler.handleError(e);
}
else {
// logger.error("Exception running embedded ZooKeeper", e);
}
}
}
}
}
\ No newline at end of file
package com.aliware.tianchi;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.dubbo.common.utils.NamedThreadFactory;
/**
* Hash service impl
* Facade
*
* @author guohaoice@gmail.com
*/
public class HashServiceImpl implements HashInterface {
private long averageRTT;
private int maxConcurrency;
private String salt;
private final String salt;
private final AtomicBoolean init = new AtomicBoolean(false);
private final List<ThrashConfig> configs;
private volatile ThrashConfig config;
private ScheduledExecutorService scheduler =
new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("Benchmark-refresh-permit"));
public HashServiceImpl(String salt) {
this(salt,Collections.emptyList());
}
public HashServiceImpl(long averageRTT, int maxConcurrency, String salt) {
this.averageRTT = averageRTT;
this.maxConcurrency = maxConcurrency;
public HashServiceImpl(String salt, List<ThrashConfig> configs) {
this.salt = salt;
this.config = ThrashConfig.INIT_CONFIG;
this.configs = Collections.unmodifiableList(configs);
}
@Override
public int hash(String input) {
long baseRtt = nextRTT();
if (!init.get()) {
if (init.compareAndSet(false, true)) {
int startTime = 0;
for (ThrashConfig thrashConfig : configs) {
scheduler.schedule(
() -> refresh(thrashConfig), startTime + config.durationInMs, TimeUnit.MILLISECONDS);
startTime += config.durationInMs;
}
}
}
Semaphore permit = config.permit;
try {
permit.acquire();
long baseRtt = nextRTT();
Thread.sleep(baseRtt);
return (input + salt).hashCode();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
permit.release();
}
return (input + salt).hashCode();
throw new IllegalStateException("Unexpected exception");
}
private void refresh(ThrashConfig thrashConfig) {
this.config = thrashConfig;
}
private long nextRTT() {
......@@ -36,7 +69,7 @@ public class HashServiceImpl implements HashInterface {
double cdf = 0;
while (u >= cdf) {
x++;
cdf = 1 - Math.exp(-1.0D * 1 / averageRTT * x);
cdf = 1 - Math.exp(-1.0D * 1 / config.averageRTTInMs * x);
}
return x;
}
......
package com.aliware.tianchi;
import java.util.concurrent.Semaphore;
/**
* @author guohaoice@gmail.com
*/
public class ThrashConfig {
public static final ThrashConfig INIT_CONFIG=new ThrashConfig(4000,Integer.MAX_VALUE,10);
final long durationInMs;
final int maxConcurrency;
final int averageRTTInMs;
final Semaphore permit;
public ThrashConfig(long durationInMs, int maxConcurrency, int averageRTTInMs) {
this.durationInMs = durationInMs;
this.maxConcurrency = maxConcurrency;
this.averageRTTInMs = averageRTTInMs;
this.permit=new Semaphore(maxConcurrency);
}
}
......@@ -7,14 +7,14 @@
<!-- provider's application name, used for tracing dependency relationship -->
<dubbo:application name="demo-provider"/>
<!-- use multicast registry center to export service -->
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- use dubbo protocol to export service on port 20880 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- service implementation, as same as regular local bean -->
<bean id="demoService" class="com.aliware.tianchi.HashServiceImpl">
<constructor-arg index="0" value="10"/>
<constructor-arg index="1" value="10000000"/>
<constructor-arg index="2" value="salt-value"/>
<!-- <constructor-arg index="0" value=""/>-->
<!-- <constructor-arg index="1" value="10000000"/>-->
<constructor-arg index="0" value="salt-value"/>
</bean>
<!-- declare the service interface to be exported -->
<dubbo:service interface="com.aliware.tianchi.HashInterface" ref="demoService"/>
......
......@@ -22,6 +22,11 @@
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!--internal start-->
<dependency>
<groupId>com.aliware.tianchi</groupId>
......@@ -79,7 +84,6 @@
<modules>
<module>internal-gateway</module>
<module>internal-service</module>
<module>bootstrap</module>
<module>workspace</module>
</modules>
</project>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment