Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
刘石明 committed Oct 26, 2016
2 parents b9c90dd + 3919221 commit 4d87f67
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 68 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@
/spring-boot-starter-saluki/.classpath
/spring-boot-starter-saluki/.gradle/
/.gradle/
/build/
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
allprojects {
apply plugin: 'maven'
group = 'com.quancheng'

version = '1.0.1-SNAPSHOT'
}

Expand Down
9 changes: 9 additions & 0 deletions build/ivy.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<ivy-module version="2.0" xmlns:m="http://ant.apache.org/ivy/maven">
<info organisation="com.quancheng" module="saluki" revision="1.0.1-SNAPSHOT" status="integration" default="true" publication="20161026165948"/>
<configurations>
<conf name="archives" visibility="public"/>
<conf name="default" visibility="public"/>
</configurations>
<publications/>
</ivy-module>
1 change: 0 additions & 1 deletion saluki-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ dependencies {
compile group: 'com.google.guava', name: 'guava', version:'18.0'
compile group: 'org.slf4j', name: 'slf4j-api', version:'1.7.21'
compile group: 'org.apache.commons', name: 'commons-lang3', version:'3.4'
compile group: 'javassist',name: 'javassist',version:'3.12.1.GA'
compile project(':saluki-serializer')
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class SalukiConstants {
public static final int DEFAULT_TIMEOUT = 60;
public static final boolean DEFAULT_GENERIC = false;
public static final String GRPC_IN_LOCAL_PROCESS = "LocalProcess";

public static final String REMOTE_ADDRESS = "RmoteAddress";
public static final String REGISTRY_PORT = "registry_port";

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public SalukiServer getServer(Map<SalukiURL, Object> providerUrls, int port) thr
new HeaderServerInterceptor());
remoteServer.addService(serviceDefinition);
injvmServer.addService(serviceDefinition);
String registryPort = System.getProperty(SalukiConstants.REGISTRY_PORT,
Integer.valueOf(providerUrl.getPort()).toString());
providerUrl = providerUrl.setPort(Integer.valueOf(registryPort).intValue());
registry.register(providerUrl);
}
return new SalukiServer(injvmServer.build().start(), remoteServer.build().start());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@

import com.google.common.collect.Lists;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.ClassFile;
import javassist.bytecode.ConstPool;
import javassist.bytecode.annotation.IntegerMemberValue;

public final class ReflectUtil {

private ReflectUtil(){
Expand All @@ -36,53 +27,6 @@ public static Object classInstance(Class cls) {
return null;
}

public static Class<?> addHastrategyAnnotation(String className, int retries) throws Exception {
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.getCtClass(className);
CtMethod[] methods = cc.getDeclaredMethods();
for (CtMethod method : methods) {
String methodName_ = method.getName();
Class[] parameterTypes = CtClass2Class(method.getParameterTypes());
if (!neglectMethod(methodName_, parameterTypes)) {
addHastrategyAnnotation(cc, method, retries);
}
}
Class<?> targetClass = cc.toClass();
return targetClass;
}

public static Class<?> addHastrategyAnnotation(String className, String[] methodNames,
int retries) throws Exception {
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.getCtClass(className);
for (String methodName : methodNames) {
CtMethod method = cc.getDeclaredMethod(methodName);
addHastrategyAnnotation(cc, method, retries);
}
Class<?> targetClass = cc.toClass();
return targetClass;
}

private static Class<?>[] CtClass2Class(CtClass[] parameterTypes) throws CannotCompileException {
Class<?>[] clzzs = new Class<?>[parameterTypes.length];
for (int i = 0; i < parameterTypes.length; i++) {
CtClass ctClzz = parameterTypes[i];
clzzs[i] = ctClzz.toClass();
}
return clzzs;
}

private static void addHastrategyAnnotation(CtClass cc, CtMethod method, int retries) throws Exception {
ClassFile ccFile = cc.getClassFile();
ConstPool constpool = ccFile.getConstPool();
AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
javassist.bytecode.annotation.Annotation annot = new javassist.bytecode.annotation.Annotation("com.quancheng.saluki.core.grpc.client.ha.Hastrategy",
constpool);
annot.addMemberValue("retries", new IntegerMemberValue(ccFile.getConstPool(), Integer.valueOf(retries)));
attr.addAnnotation(annot);
method.getMethodInfo().addAttribute(attr);
}

public static Class<?> getTypedReq(Method method) {
Class[] params = method.getParameterTypes();
return params[0];
Expand Down
2 changes: 1 addition & 1 deletion saluki-registry/saluki-registry-consul/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description = 'saluki-registry-consul'

dependencies {
compile group: 'org.slf4j', name: 'slf4j-api', version:'1.7.21'
compile group: 'com.ecwid.consul', name: 'consul-api', version:'1.1.11'
compile group: 'com.ecwid.consul', name: 'consul-api', version:'1.2.1'
compile group: 'com.google.guava', name: 'guava', version:'18.0'
compile group: 'org.apache.commons', name: 'commons-lang3', version:'3.4'
compile project(':saluki-core')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import java.util.UUID;

import com.ecwid.consul.v1.agent.model.NewService;
import com.google.common.collect.Lists;
import com.quancheng.saluki.core.common.SalukiURL;

public final class SalukiConsulService {

Expand Down Expand Up @@ -41,6 +39,7 @@ public NewService getNewService() {
consulService.setTags(unmodifiableList(new ArrayList<>(this.tags)));
NewService.Check check = new NewService.Check();
check.setTtl(this.interval + "s");
check.setDeregisterCriticalServiceAfter("1m");
consulService.setCheck(check);
return consulService;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ public class SalukiProperties {

private String serverHost;

private int serverPort = 6565;
private int serverStartPort;

private int serverRegistryPort;

private String consulIp;

Expand All @@ -21,12 +23,20 @@ public class SalukiProperties {

private String servcieVersion;

public int getServerPort() {
return serverPort;
public int getServerStartPort() {
return serverStartPort;
}

public void setServerStartPort(int serverStartPort) {
this.serverStartPort = serverStartPort;
}

public int getServerRegistryPort() {
return serverRegistryPort;
}

public void setServerPort(int serverPort) {
this.serverPort = serverPort;
public void setServerRegistryPort(int serverRegistryPort) {
this.serverRegistryPort = serverRegistryPort;
}

public String getConsulIp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.base.Preconditions;
import com.quancheng.boot.saluki.starter.SalukiService;
import com.quancheng.boot.saluki.starter.autoconfigure.SalukiProperties;
import com.quancheng.saluki.core.common.SalukiConstants;
import com.quancheng.saluki.core.config.ServiceConfig;

@Order(value = 0)
Expand All @@ -38,6 +39,7 @@ public class SalukiServerRunner implements CommandLineRunner, DisposableBean {
@Override
public void run(String... args) throws Exception {
log.info("Starting gRPC Server ...");
resetRegistryPort();
ServiceConfig serviceConfig = newServiceConfig();
for (Object obj : getTypedBeansWithAnnotation(SalukiService.class)) {
SalukiService gRpcServiceAnn = obj.getClass().getAnnotation(SalukiService.class);
Expand Down Expand Up @@ -77,6 +79,13 @@ private String getVersion(SalukiService service) {
}
}

private void resetRegistryPort() {
int serverRegistryPort = grpcProperties.getServerRegistryPort();
if (serverRegistryPort != 0) {
System.setProperty(SalukiConstants.REGISTRY_PORT, Integer.valueOf(serverRegistryPort).toString());
}
}

private ServiceConfig newServiceConfig() {
ServiceConfig serviceConfig = new ServiceConfig();
serviceConfig.setRegistryName("consul");
Expand All @@ -86,9 +95,9 @@ private ServiceConfig newServiceConfig() {
int port = grpcProperties.getConsulPort();
Preconditions.checkState(port != 0, "RegistryPort can not be zero", port);
serviceConfig.setRegistryPort(grpcProperties.getConsulPort());
int serverPort = grpcProperties.getServerPort();
int serverPort = grpcProperties.getServerStartPort();
Preconditions.checkState(serverPort != 0, "ServerPort can not be null", serverPort);
serviceConfig.setPort(grpcProperties.getServerPort());
serviceConfig.setPort(serverPort);
serviceConfig.setHost(grpcProperties.getServerHost());
return serviceConfig;
}
Expand Down

0 comments on commit 4d87f67

Please sign in to comment.