diff --git a/pom.xml b/pom.xml
index ef3ee6a..857ebb8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -23,7 +23,6 @@
4.0.0
1.2.3
1.0-SNAPSHOT
- 5.2.7.RELEASE
@@ -90,13 +89,6 @@
${xrpc.versin}
-
- org.springframework
- spring-context
- ${spring.version}
-
-
-
@@ -114,4 +106,21 @@
+
+
+
+
+ jdk-1.8
+
+ true
+ 1.8
+
+
+ 1.8
+ 1.8
+ 1.8
+
+
+
+
\ No newline at end of file
diff --git a/xrpc-core/pom.xml b/xrpc-core/pom.xml
index fbbe391..8d3f553 100644
--- a/xrpc-core/pom.xml
+++ b/xrpc-core/pom.xml
@@ -54,6 +54,14 @@
org.springframework
spring-context
+ 5.2.7.RELEASE
+ provided
+
+
+ org.springframework.boot
+ spring-boot-autoconfigure
+ 2.6.15
+ provided
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/common/AppConst.java b/xrpc-core/src/main/java/com/xchb/xrpc/common/AppConst.java
new file mode 100644
index 0000000..9decd52
--- /dev/null
+++ b/xrpc-core/src/main/java/com/xchb/xrpc/common/AppConst.java
@@ -0,0 +1,18 @@
+package com.xchb.xrpc.common;
+
+public class AppConst {
+
+
+ public static final Integer RANDOM_MIN_EXPORT_PORT = 11111;
+
+ /**
+ * 对外暴露的端口
+ */
+ public static Integer exportPort;
+
+ /**
+ * 注册中心地址
+ */
+ public static String zkAddr = "localhost:2181";
+
+}
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/common/RpcResponseBak.java b/xrpc-core/src/main/java/com/xchb/xrpc/common/RpcResponseBak.java
index c4aaadb..d35fb8e 100644
--- a/xrpc-core/src/main/java/com/xchb/xrpc/common/RpcResponseBak.java
+++ b/xrpc-core/src/main/java/com/xchb/xrpc/common/RpcResponseBak.java
@@ -1,6 +1,5 @@
package com.xchb.xrpc.common;
-import com.xchb.xrpc.common.proto.RpcResponseProto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/common/ServerParam.java b/xrpc-core/src/main/java/com/xchb/xrpc/common/ServerParam.java
index 961aaa3..e03a5fc 100644
--- a/xrpc-core/src/main/java/com/xchb/xrpc/common/ServerParam.java
+++ b/xrpc-core/src/main/java/com/xchb/xrpc/common/ServerParam.java
@@ -1,8 +1,6 @@
package com.xchb.xrpc.common;
-import com.xchb.xrpc.config.XrpcConfigProperties;
import com.xchb.xrpc.util.IpUtils;
-import com.xchb.xrpc.util.SingleFactory;
import lombok.Builder;
import lombok.Data;
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/config/XrpcConfigProperties.java b/xrpc-core/src/main/java/com/xchb/xrpc/config/XrpcConfigProperties.java
deleted file mode 100644
index ecd0828..0000000
--- a/xrpc-core/src/main/java/com/xchb/xrpc/config/XrpcConfigProperties.java
+++ /dev/null
@@ -1,32 +0,0 @@
-package com.xchb.xrpc.config;
-
-import lombok.Data;
-import lombok.NonNull;
-
-/**
- * @author XDD
- * @project xrpc
- * @date 2021/7/26
- * @description Good Good Study,Day Day Up.
- */
-@Data
-public class XrpcConfigProperties {
-
-
- /**
- * 是否是服务提供方
- */
- private boolean isServer = false;
-
- /**
- * 服务提供者对外暴露的端口
- */
- private Integer exportPort = 8770;
-
- /**
- * 注册中心地址
- */
- private String zkAddr = "localhost:2181";
-
-
-}
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/extension/ExtensionLoader.java b/xrpc-core/src/main/java/com/xchb/xrpc/extension/ExtensionLoader.java
index 7f7c5cc..50f5902 100644
--- a/xrpc-core/src/main/java/com/xchb/xrpc/extension/ExtensionLoader.java
+++ b/xrpc-core/src/main/java/com/xchb/xrpc/extension/ExtensionLoader.java
@@ -1,7 +1,7 @@
package com.xchb.xrpc.extension;
-import lombok.extern.slf4j.Slf4j;
import com.xchb.xrpc.register.ServerRegister;
+import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.IOException;
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/proxy/ClientProxy.java b/xrpc-core/src/main/java/com/xchb/xrpc/proxy/ClientProxy.java
index ea968eb..3da2691 100644
--- a/xrpc-core/src/main/java/com/xchb/xrpc/proxy/ClientProxy.java
+++ b/xrpc-core/src/main/java/com/xchb/xrpc/proxy/ClientProxy.java
@@ -1,17 +1,14 @@
package com.xchb.xrpc.proxy;
import cn.hutool.core.util.IdUtil;
-import com.alibaba.fastjson.JSONObject;
import com.xchb.xrpc.common.ServerParam;
import com.xchb.xrpc.common.proto.RpcRequestProto;
import com.xchb.xrpc.common.proto.RpcResponseProto;
-import com.xchb.xrpc.config.XrpcConfigProperties;
import com.xchb.xrpc.exceptions.ServerNotFindExeception;
import com.xchb.xrpc.extension.ExtensionLoader;
import com.xchb.xrpc.loadbalance.LoadBalance;
import com.xchb.xrpc.register.ServerRegister;
import com.xchb.xrpc.transport.client.ClientBooter;
-import com.xchb.xrpc.util.AppConst;
import com.xchb.xrpc.util.ProtoBufUtils;
import com.xchb.xrpc.util.SingleFactory;
import com.xchb.xrpc.util.UnProcessRequest;
@@ -22,9 +19,9 @@
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
-import java.util.*;
+import java.util.LinkedHashMap;
+import java.util.List;
import java.util.concurrent.CompletableFuture;
-import java.util.stream.Collectors;
/**
* @author XDD
@@ -41,8 +38,6 @@ public class ClientProxy implements InvocationHandler {
private ServerParam serverParam;
- private XrpcConfigProperties xrpcConfigProperties = SingleFactory.getInstance(XrpcConfigProperties.class);
-
public ClientProxy(ServerParam serverParam){
local = ExtensionLoader.get(ServerRegister.class, "zk");
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/proxy/ClientProxyFactory.java b/xrpc-core/src/main/java/com/xchb/xrpc/proxy/ClientProxyFactory.java
index b3f32a6..73f26eb 100644
--- a/xrpc-core/src/main/java/com/xchb/xrpc/proxy/ClientProxyFactory.java
+++ b/xrpc-core/src/main/java/com/xchb/xrpc/proxy/ClientProxyFactory.java
@@ -1,20 +1,9 @@
package com.xchb.xrpc.proxy;
-import cn.hutool.core.util.IdUtil;
-import com.xchb.xrpc.transport.client.ClientBooter;
-import com.xchb.xrpc.util.AppConst;
-import com.xchb.xrpc.util.SingleFactory;
-import com.xchb.xrpc.util.UnProcessRequest;
-import io.netty.channel.Channel;
-import io.netty.channel.ChannelFutureListener;
-import lombok.extern.slf4j.Slf4j;
import com.xchb.xrpc.common.ServerParam;
+import lombok.extern.slf4j.Slf4j;
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
-import java.net.InetSocketAddress;
-import java.util.concurrent.CompletableFuture;
/**
* @author XDD
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/register/ServerRegister.java b/xrpc-core/src/main/java/com/xchb/xrpc/register/ServerRegister.java
index 17dde2a..af18327 100644
--- a/xrpc-core/src/main/java/com/xchb/xrpc/register/ServerRegister.java
+++ b/xrpc-core/src/main/java/com/xchb/xrpc/register/ServerRegister.java
@@ -1,7 +1,7 @@
package com.xchb.xrpc.register;
-import com.xchb.xrpc.extension.SPI;
import com.xchb.xrpc.common.ServerParam;
+import com.xchb.xrpc.extension.SPI;
import java.util.List;
@@ -21,6 +21,18 @@ public interface ServerRegister {
*/
boolean register() ;
+ /**
+ * 添加服务
+ * @param serverParam
+ */
+ void put(ServerParam serverParam);
+
+
+ /**
+ * 服务数量
+ * @return
+ */
+ Integer serverCount();
/**
@@ -29,6 +41,4 @@ public interface ServerRegister {
* @return
*/
List find(String serverName);
-
- void put(ServerParam serverParam);
}
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/register/annotation/RpcScan.java b/xrpc-core/src/main/java/com/xchb/xrpc/register/annotation/EnableXRpc.java
similarity index 93%
rename from xrpc-core/src/main/java/com/xchb/xrpc/register/annotation/RpcScan.java
rename to xrpc-core/src/main/java/com/xchb/xrpc/register/annotation/EnableXRpc.java
index 2e7fa47..706065d 100644
--- a/xrpc-core/src/main/java/com/xchb/xrpc/register/annotation/RpcScan.java
+++ b/xrpc-core/src/main/java/com/xchb/xrpc/register/annotation/EnableXRpc.java
@@ -1,7 +1,7 @@
package com.xchb.xrpc.register.annotation;
-import org.springframework.context.annotation.Import;
import com.xchb.xrpc.spring.CustomBeanRegistrar;
+import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@@ -16,7 +16,7 @@
@Target(ElementType.TYPE)
@Documented
@Import(CustomBeanRegistrar.class)
-public @interface RpcScan {
+public @interface EnableXRpc {
String[] basePackage();
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/register/impl/ZkServerRegister.java b/xrpc-core/src/main/java/com/xchb/xrpc/register/impl/ZkServerRegister.java
index 9ac6dc6..1245e43 100644
--- a/xrpc-core/src/main/java/com/xchb/xrpc/register/impl/ZkServerRegister.java
+++ b/xrpc-core/src/main/java/com/xchb/xrpc/register/impl/ZkServerRegister.java
@@ -1,15 +1,14 @@
package com.xchb.xrpc.register.impl;
-import com.xchb.xrpc.config.XrpcConfigProperties;
+import com.xchb.xrpc.common.AppConst;
+import com.xchb.xrpc.common.ServerParam;
import com.xchb.xrpc.register.ServerRegister;
import com.xchb.xrpc.util.SingleFactory;
-import com.xchb.xrpc.common.ServerParam;
import com.xchb.xrpc.util.ZkUtils;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.util.ArrayList;
-import java.util.Iterator;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
@@ -29,10 +28,9 @@ public class ZkServerRegister implements ServerRegister {
@Override
public boolean register() {
- Integer exportPort = SingleFactory.getInstance(XrpcConfigProperties.class).getExportPort();
while (!taskQueue.isEmpty()){
ServerParam serverParam = taskQueue.poll();
- serverParam.setPort(exportPort);
+ serverParam.setPort(AppConst.exportPort);
ObjectOutputStream oos = null;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
String serverName = serverParam.serverName();
@@ -78,5 +76,10 @@ public void put(ServerParam serverParam) {
taskQueue.offer(serverParam);
}
+ @Override
+ public Integer serverCount() {
+ return taskQueue.size();
+ }
+
}
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/spring/CustomBeanRegistrar.java b/xrpc-core/src/main/java/com/xchb/xrpc/spring/CustomBeanRegistrar.java
index 9571dd7..96f0155 100644
--- a/xrpc-core/src/main/java/com/xchb/xrpc/spring/CustomBeanRegistrar.java
+++ b/xrpc-core/src/main/java/com/xchb/xrpc/spring/CustomBeanRegistrar.java
@@ -1,20 +1,30 @@
package com.xchb.xrpc.spring;
import cn.hutool.core.util.ArrayUtil;
+import cn.hutool.core.util.StrUtil;
+import com.xchb.xrpc.common.AppConst;
+import com.xchb.xrpc.register.ServerRegister;
+import com.xchb.xrpc.register.annotation.EnableXRpc;
import com.xchb.xrpc.register.annotation.RpcReference;
-import com.xchb.xrpc.register.annotation.RpcScan;
import com.xchb.xrpc.register.annotation.RpcService;
+import com.xchb.xrpc.spring.properties.SpringAutoConfigProperties;
+import com.xchb.xrpc.transport.server.ServerBooter;
+import com.xchb.xrpc.util.SingleFactory;
import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
-import org.springframework.context.ResourceLoaderAware;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
-import org.springframework.core.io.ResourceLoader;
+import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;
-import java.util.*;
+import java.util.Arrays;
+import java.util.Map;
/**
* @author XDD
@@ -23,29 +33,20 @@
* @description 让自定义注解标识的类也交由spring管理.
*/
@Slf4j
-public class CustomBeanRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {
+public class CustomBeanRegistrar implements ImportBeanDefinitionRegistrar{
+
- private ResourceLoader resourceLoader;
//自定义注解中包扫描的注解属性名
private String PACKAGE_SCANNER_PARAM = "basePackage";
- //spring配置文件的包路径
- private String springConfigBasePackage = "com.xchb.xrpc.spring";
-
- @Override
- public void setResourceLoader(ResourceLoader resourceLoader) {
- this.resourceLoader = resourceLoader;
- }
-
-
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata,
BeanDefinitionRegistry registry,
BeanNameGenerator importBeanNameGenerator) {
//获取自定义注解中包含的扫描路径
Map rpcScanParams =
- annotationMetadata.getAnnotationAttributes(RpcScan.class.getCanonicalName());
+ annotationMetadata.getAnnotationAttributes(EnableXRpc.class.getCanonicalName());
String[] tmp = (String[]) rpcScanParams.get(PACKAGE_SCANNER_PARAM);
String[] basePackage = new String[tmp.length+1];
ArrayUtil.copy(tmp, basePackage, tmp.length);
@@ -58,7 +59,14 @@ public void registerBeanDefinitions(AnnotationMetadata annotationMetadata,
scanner.addIncludeFilter(new AnnotationTypeFilter(RpcService.class));
scanner.addIncludeFilter(new AnnotationTypeFilter(RpcReference.class));
scanner.scan(basePackage);
+
+
+ BeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(SpringBeanPostProcess.class).getBeanDefinition();
+ registry.registerBeanDefinition("springBeanPostProcess",beanDefinition);
+
}
+
+
}
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/spring/SpringBeanPostProcess.java b/xrpc-core/src/main/java/com/xchb/xrpc/spring/SpringBeanPostProcess.java
index fcc77d0..d49c575 100644
--- a/xrpc-core/src/main/java/com/xchb/xrpc/spring/SpringBeanPostProcess.java
+++ b/xrpc-core/src/main/java/com/xchb/xrpc/spring/SpringBeanPostProcess.java
@@ -12,7 +12,6 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
-import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
@@ -22,7 +21,6 @@
* @date 2021/1/18
* @description Good Good Study,Day Day Up.
*/
-@Component
@Slf4j
public class SpringBeanPostProcess implements BeanPostProcessor {
@@ -30,7 +28,6 @@ public class SpringBeanPostProcess implements BeanPostProcessor {
private ServerRegister serverRegister;
-
public SpringBeanPostProcess(){
super();
Class local = ExtensionLoader.get(ServerRegister.class, "zk");
@@ -38,9 +35,9 @@ public SpringBeanPostProcess(){
throw new BaseException(500,"注册服务不可用");
}
serverRegister = SingleFactory.getInstance(local);
+ log.info("服务注册器初始化完成:{}",ServerRegister.class.getSimpleName());
}
-
/**
* bean初始化
* @param bean
@@ -58,7 +55,6 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro
.buildServer(interfaceClass,bean.getClass(),rpcService.version(),rpcService.group());
//加入任务队列
serverRegister.put(serverParam);
- log.info("加入等待任务成功:{}",serverParam);
}
return bean;
}
@@ -95,4 +91,5 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
return bean;
}
+
}
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/spring/SpringContainerInit.java b/xrpc-core/src/main/java/com/xchb/xrpc/spring/SpringContainerInit.java
new file mode 100644
index 0000000..bda6c5b
--- /dev/null
+++ b/xrpc-core/src/main/java/com/xchb/xrpc/spring/SpringContainerInit.java
@@ -0,0 +1,47 @@
+package com.xchb.xrpc.spring;
+
+import cn.hutool.core.util.StrUtil;
+import com.xchb.xrpc.common.AppConst;
+import com.xchb.xrpc.register.ServerRegister;
+import com.xchb.xrpc.spring.properties.SpringAutoConfigProperties;
+import com.xchb.xrpc.transport.server.ServerBooter;
+import com.xchb.xrpc.util.SingleFactory;
+import com.xchb.xrpc.util.ZkUtils;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationListener;
+import org.springframework.context.event.ContextRefreshedEvent;
+import org.springframework.stereotype.Component;
+
+@Component
+public class SpringContainerInit implements ApplicationListener {
+
+ @Override
+ public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
+ ServerRegister serverRegister = SingleFactory.getInstance(ServerRegister.class);
+ ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
+ SpringAutoConfigProperties springAutoConfigProperties
+ = applicationContext.getBean(SpringAutoConfigProperties.class);
+
+ AppConst.exportPort = springAutoConfigProperties.getExportPort();
+ if(AppConst.exportPort==null) {
+ AppConst.exportPort = io.pisceshub.muchat.common.core.utils.MixUtils.findAvailablePort();
+ }
+ String zkAddr = springAutoConfigProperties.getZkAddr();
+ if(StrUtil.isNotBlank(zkAddr)){
+ AppConst.zkAddr = zkAddr;
+ }
+ ZkUtils zkUtils = new ZkUtils(AppConst.zkAddr);
+ SingleFactory.set(ZkUtils.class,zkUtils);
+ Integer serverCount = serverRegister.serverCount();
+ if(serverCount>0){
+ try {
+ new ServerBooter().start(AppConst.exportPort);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+
+ serverRegister.register();
+ }
+ }
+
+}
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/spring/XrpcStartBean.java b/xrpc-core/src/main/java/com/xchb/xrpc/spring/XrpcStartBean.java
deleted file mode 100644
index 068b3ca..0000000
--- a/xrpc-core/src/main/java/com/xchb/xrpc/spring/XrpcStartBean.java
+++ /dev/null
@@ -1,44 +0,0 @@
-package com.xchb.xrpc.spring;
-
-import com.xchb.xrpc.config.XrpcConfigProperties;
-import com.xchb.xrpc.exceptions.BaseException;
-import com.xchb.xrpc.extension.ExtensionLoader;
-import com.xchb.xrpc.register.ServerRegister;
-import com.xchb.xrpc.transport.server.ServerBooter;
-import com.xchb.xrpc.util.SingleFactory;
-import com.xchb.xrpc.util.ZkUtils;
-import org.springframework.context.ApplicationEvent;
-import org.springframework.context.ApplicationListener;
-import org.springframework.context.event.ContextStartedEvent;
-
-/**
- * @author XDD
- * @project xrpc
- * @date 2021/7/26
- * @description 服务提供者启动器.
- */
-public class XrpcStartBean {
-
-
- private ServerRegister serverRegister;
- public XrpcStartBean(){
- super();
- Class local = ExtensionLoader.get(ServerRegister.class, "zk");
- if(local==null){
- throw new BaseException(500,"注册服务不可用");
- }
- serverRegister = SingleFactory.getInstance(local);
- }
-
-
- public XrpcStartBean(XrpcConfigProperties configProperties) throws InterruptedException {
- this();
- SingleFactory.set(ZkUtils.class,new ZkUtils(configProperties.getZkAddr()));
- if(configProperties.isServer()){
- new ServerBooter().start(configProperties.getExportPort());
- SingleFactory.set(XrpcConfigProperties.class, configProperties);
- serverRegister.register();
- }
- }
-
-}
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/spring/properties/SpringAutoConfigProperties.java b/xrpc-core/src/main/java/com/xchb/xrpc/spring/properties/SpringAutoConfigProperties.java
new file mode 100644
index 0000000..34194a3
--- /dev/null
+++ b/xrpc-core/src/main/java/com/xchb/xrpc/spring/properties/SpringAutoConfigProperties.java
@@ -0,0 +1,26 @@
+package com.xchb.xrpc.spring.properties;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+
+@Data
+@Configuration
+@ConfigurationProperties(prefix = SpringAutoConfigProperties.PRE)
+public class SpringAutoConfigProperties {
+
+ public final static String PRE = "app";
+
+ /**
+ * 对外暴露的端口
+ */
+ private Integer exportPort;
+
+ /**
+ * 注册中心地址
+ */
+ private String zkAddr;
+
+
+}
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/transport/client/ClientBooter.java b/xrpc-core/src/main/java/com/xchb/xrpc/transport/client/ClientBooter.java
index a78dda9..308d217 100644
--- a/xrpc-core/src/main/java/com/xchb/xrpc/transport/client/ClientBooter.java
+++ b/xrpc-core/src/main/java/com/xchb/xrpc/transport/client/ClientBooter.java
@@ -1,6 +1,7 @@
package com.xchb.xrpc.transport.client;
import com.xchb.xrpc.common.proto.RpcResponseProto;
+import com.xchb.xrpc.transport.client.handler.ClientHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.Channel;
@@ -19,11 +20,6 @@
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;
-
-import com.xchb.xrpc.transport.client.handler.ClientHandler;
-import com.xchb.xrpc.transport.codec.RpcCustomDecode;
-import com.xchb.xrpc.transport.codec.RpcCustomEncode;
-
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/transport/server/ServerBooter.java b/xrpc-core/src/main/java/com/xchb/xrpc/transport/server/ServerBooter.java
index 99b95ca..334a956 100644
--- a/xrpc-core/src/main/java/com/xchb/xrpc/transport/server/ServerBooter.java
+++ b/xrpc-core/src/main/java/com/xchb/xrpc/transport/server/ServerBooter.java
@@ -1,7 +1,7 @@
package com.xchb.xrpc.transport.server;
import com.xchb.xrpc.common.proto.RpcRequestProto;
-import com.xchb.xrpc.util.AppConst;
+import com.xchb.xrpc.transport.server.handler.ServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.ChannelInitializer;
@@ -19,8 +19,6 @@
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;
-import com.xchb.xrpc.transport.server.handler.ServerHandler;
-
import java.util.concurrent.TimeUnit;
/**
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/transport/server/handler/ServerHandler.java b/xrpc-core/src/main/java/com/xchb/xrpc/transport/server/handler/ServerHandler.java
index b429403..892b4ae 100644
--- a/xrpc-core/src/main/java/com/xchb/xrpc/transport/server/handler/ServerHandler.java
+++ b/xrpc-core/src/main/java/com/xchb/xrpc/transport/server/handler/ServerHandler.java
@@ -3,20 +3,14 @@
import com.alibaba.fastjson.JSONObject;
import com.xchb.xrpc.common.proto.RpcRequestProto;
import com.xchb.xrpc.common.proto.RpcResponseProto;
-import com.xchb.xrpc.loadbalance.LoadBalance;
import com.xchb.xrpc.util.ProtoBufUtils;
import com.xchb.xrpc.util.SingleFactory;
-import io.netty.channel.*;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
-import com.xchb.xrpc.common.ServerParam;
-import com.xchb.xrpc.exceptions.ServerNotFindExeception;
-import com.xchb.xrpc.extension.ExtensionLoader;
-import com.xchb.xrpc.register.ServerRegister;
import java.lang.reflect.Method;
import java.util.Arrays;
-import java.util.Iterator;
-import java.util.List;
/**
* @author XDD
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/util/AppConst.java b/xrpc-core/src/main/java/com/xchb/xrpc/util/AppConst.java
deleted file mode 100644
index 407bdca..0000000
--- a/xrpc-core/src/main/java/com/xchb/xrpc/util/AppConst.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.xchb.xrpc.util;
-
-/**
- * @author XDD
- * @project xrpc
- * @date 2021/1/10
- * @description
- */
-public class AppConst {
-
-
-
-}
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/util/MixUtils.java b/xrpc-core/src/main/java/com/xchb/xrpc/util/MixUtils.java
new file mode 100644
index 0000000..29870ee
--- /dev/null
+++ b/xrpc-core/src/main/java/com/xchb/xrpc/util/MixUtils.java
@@ -0,0 +1,87 @@
+package io.pisceshub.muchat.common.core.utils;
+
+
+import com.xchb.xrpc.common.AppConst;
+
+import java.io.IOException;
+import java.net.*;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Random;
+import java.util.Set;
+
+/**
+ * @description:
+ * @author: xiaochangbai
+ * @date: 2023/6/12 16:44
+ */
+public class MixUtils {
+
+ private final static Set USE_PORT = new HashSet<>(10);
+
+ public static Integer findAvailablePort() {
+ for (int i = 0; i <= 1000; i++) {
+ Integer port = AppConst.RANDOM_MIN_EXPORT_PORT + i;
+ if (USE_PORT.contains(port)) {
+ continue;
+ }
+ try (ServerSocket serverSocket = new ServerSocket(port)) {
+ USE_PORT.add(port);
+ return port;
+ } catch (IOException e) {
+ }
+ }
+ throw new RuntimeException("没有可用的端口");
+ }
+
+ public static String getInet4Address() {
+ Enumeration nis;
+ String ip = null;
+ try {
+ nis = NetworkInterface.getNetworkInterfaces();
+ for (; nis.hasMoreElements(); ) {
+ NetworkInterface ni = nis.nextElement();
+ Enumeration ias = ni.getInetAddresses();
+ for (; ias.hasMoreElements(); ) {
+ InetAddress ia = ias.nextElement();
+ // ia instanceof Inet6Address && !ia.equals("")
+ if (ia instanceof Inet4Address && !ia.getHostAddress().equals("127.0.0.1")) {
+ ip = ia.getHostAddress();
+ }
+ }
+ }
+ } catch (SocketException e) {
+ e.printStackTrace();
+ }
+ return ip;
+ }
+
+ public static String random(String[] arrays) {
+ if (arrays == null || arrays.length < 1) {
+ return null;
+ }
+ int index = new Random().nextInt(arrays.length);
+ if (index < 0 || index >= arrays.length) {
+ index = 0;
+ }
+ return arrays[index];
+ }
+
+ public static byte[] LongToBytes(long values) {
+ byte[] buffer = new byte[8];
+ for (int i = 0; i < 8; i++) {
+ int offset = 64 - (i + 1) * 8;
+ buffer[i] = (byte) ((values >> offset) & 0xff);
+ }
+ return buffer;
+ }
+
+ public static long BytesToLong(byte[] buffer) {
+ long values = 0;
+ for (int i = 0; i < 8; i++) {
+ values <<= 8;
+ values |= (buffer[i] & 0xff);
+ }
+ return values;
+ }
+}
diff --git a/xrpc-core/src/main/java/com/xchb/xrpc/util/SingleFactory.java b/xrpc-core/src/main/java/com/xchb/xrpc/util/SingleFactory.java
index 79f110a..6fc515c 100644
--- a/xrpc-core/src/main/java/com/xchb/xrpc/util/SingleFactory.java
+++ b/xrpc-core/src/main/java/com/xchb/xrpc/util/SingleFactory.java
@@ -32,6 +32,10 @@ public static T getInstance(Class aClass) {
if (instance == null) {
instance = aClass.getDeclaredConstructor().newInstance();
MAPS.put(key, instance);
+ Class>[] interfaces = aClass.getInterfaces();
+ for(Class> iClass:interfaces){
+ MAPS.put(iClass.getName(),instance);
+ }
}
}
}
diff --git a/xrpc-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/xrpc-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json
new file mode 100644
index 0000000..cb4f9c2
--- /dev/null
+++ b/xrpc-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json
@@ -0,0 +1,14 @@
+{
+ "properties": [
+ {
+ "name": "app.exportPort",
+ "type": "java.lang.Integer",
+ "description": "export port"
+ },
+ {
+ "name": "app.zkAddr",
+ "type": "java.lang.String",
+ "description": "zookeeper address."
+ }
+ ]
+}
\ No newline at end of file
diff --git a/xrpc-demo/client-exmple/pom.xml b/xrpc-demo/client-exmple/pom.xml
index cd1754d..602fba6 100644
--- a/xrpc-demo/client-exmple/pom.xml
+++ b/xrpc-demo/client-exmple/pom.xml
@@ -13,6 +13,10 @@
+
+ org.springframework.boot
+ spring-boot-starter-web
+
com.xchb
server-api
diff --git a/xrpc-demo/client-exmple/src/main/java/com/xchb/xrpc/MainClientApplication.java b/xrpc-demo/client-exmple/src/main/java/com/xchb/xrpc/MainClientApplication.java
index 2007239..0e0f337 100644
--- a/xrpc-demo/client-exmple/src/main/java/com/xchb/xrpc/MainClientApplication.java
+++ b/xrpc-demo/client-exmple/src/main/java/com/xchb/xrpc/MainClientApplication.java
@@ -1,12 +1,8 @@
package com.xchb.xrpc;
-import com.xchb.xrpc.api.HelloController;
-import com.xchb.xrpc.api.UserController;
-import com.xchb.xrpc.api.UserService;
-import com.xchb.xrpc.entity.User;
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.annotation.AnnotationConfigApplicationContext;
-import com.xchb.xrpc.register.annotation.RpcScan;
+import com.xchb.xrpc.register.annotation.EnableXRpc;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
@@ -15,21 +11,13 @@
* @date 2021/1/15
* @description Good Good Study,Day Day Up.
*/
-@RpcScan(basePackage = "com.xchb.xrpc")
+@SpringBootApplication
+@EnableXRpc(basePackage = "com.xchb.xrpc")
public class MainClientApplication {
public static void main(String[] args) {
- ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainClientApplication.class);
- HelloController helloController = applicationContext.getBean(HelloController.class);
- System.out.println(helloController.hello("xchb-----------------------"));
- System.out.println(helloController.hello("xchb-----------------------"));
- System.out.println(helloController.hello("xchb-----------------------"));
- System.out.println(helloController.hello("xchb-----------------------"));
- System.out.println(helloController.hello2("xchb-----------------------"));
- UserController userController = applicationContext.getBean(UserController.class);
- userController.register("xcc","aa");
- User user = userController.login("xcc", "aa");
- System.out.println(user+"xchb-----------------------");
+ SpringApplication.run(MainClientApplication.class,args);
+
}
}
diff --git a/xrpc-demo/client-exmple/src/main/java/com/xchb/xrpc/api/UserController.java b/xrpc-demo/client-exmple/src/main/java/com/xchb/xrpc/api/UserController.java
index 5e33a97..6cea3f9 100644
--- a/xrpc-demo/client-exmple/src/main/java/com/xchb/xrpc/api/UserController.java
+++ b/xrpc-demo/client-exmple/src/main/java/com/xchb/xrpc/api/UserController.java
@@ -1,9 +1,9 @@
package com.xchb.xrpc.api;
import cn.hutool.core.util.IdUtil;
-import org.springframework.stereotype.Controller;
import com.xchb.xrpc.entity.User;
import com.xchb.xrpc.register.annotation.RpcReference;
+import org.springframework.stereotype.Controller;
/**
* @author XDD
diff --git a/xrpc-demo/client-exmple/src/main/java/com/xchb/xrpc/config/XrpClientConfig.java b/xrpc-demo/client-exmple/src/main/java/com/xchb/xrpc/config/XrpClientConfig.java
deleted file mode 100644
index 176f593..0000000
--- a/xrpc-demo/client-exmple/src/main/java/com/xchb/xrpc/config/XrpClientConfig.java
+++ /dev/null
@@ -1,24 +0,0 @@
-package com.xchb.xrpc.config;
-
-import com.xchb.xrpc.config.XrpcConfigProperties;
-import com.xchb.xrpc.spring.XrpcStartBean;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-/**
- * @author XDD
- * @project xrpc
- * @date 2021/7/26
- * @description Good Good Study,Day Day Up.
- */
-@Configuration
-public class XrpClientConfig {
-
- @Bean
- public XrpcStartBean xrpcStartBean() throws InterruptedException {
- XrpcConfigProperties configProperties = new XrpcConfigProperties();
- configProperties.setZkAddr("localhost:2181");
- return new XrpcStartBean(configProperties);
- }
-
-}
diff --git a/xrpc-demo/client-exmple/src/main/java/com/xchb/xrpc/api/HelloController.java b/xrpc-demo/client-exmple/src/main/java/com/xchb/xrpc/controller/TestController.java
similarity index 50%
rename from xrpc-demo/client-exmple/src/main/java/com/xchb/xrpc/api/HelloController.java
rename to xrpc-demo/client-exmple/src/main/java/com/xchb/xrpc/controller/TestController.java
index 6faaaa2..6f2d236 100644
--- a/xrpc-demo/client-exmple/src/main/java/com/xchb/xrpc/api/HelloController.java
+++ b/xrpc-demo/client-exmple/src/main/java/com/xchb/xrpc/controller/TestController.java
@@ -1,19 +1,15 @@
-package com.xchb.xrpc.api;
-
-
-import org.springframework.stereotype.Controller;
+package com.xchb.xrpc.controller;
+import com.xchb.xrpc.api.HelloService;
import com.xchb.xrpc.register.annotation.RpcReference;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+@RequestMapping("/test")
+@RestController
+public class TestController {
-/**
- * @author XDD
- * @project xrpc
- * @date 2021/1/18
- * @description Good Good Study,Day Day Up.
- */
-@Controller
-public class HelloController {
@RpcReference
private HelloService helloService;
@@ -21,10 +17,12 @@ public class HelloController {
@RpcReference(group = "1",version = "1.0")
private HelloService helloService2;
+ @GetMapping("/hello")
public String hello(String name) {
return helloService.hello(name);
}
+ @GetMapping("/hello2")
public String hello2( String name) {
return helloService2.hello(name);
}
diff --git a/xrpc-demo/client-exmple/src/main/resources/application.yml b/xrpc-demo/client-exmple/src/main/resources/application.yml
new file mode 100644
index 0000000..879550c
--- /dev/null
+++ b/xrpc-demo/client-exmple/src/main/resources/application.yml
@@ -0,0 +1,2 @@
+server:
+ port: 8988
\ No newline at end of file
diff --git a/xrpc-demo/client-exmple/src/test/java/T1.java b/xrpc-demo/client-exmple/src/test/java/T1.java
new file mode 100644
index 0000000..3ccb314
--- /dev/null
+++ b/xrpc-demo/client-exmple/src/test/java/T1.java
@@ -0,0 +1,4 @@
+public class T1 {
+
+
+}
diff --git a/xrpc-demo/pom.xml b/xrpc-demo/pom.xml
index 5b6a495..6716b71 100644
--- a/xrpc-demo/pom.xml
+++ b/xrpc-demo/pom.xml
@@ -17,6 +17,7 @@
server-api
client-exmple
+ pom
1.0-SNAPSHOT
@@ -24,6 +25,11 @@
+
+ org.springframework.boot
+ spring-boot-starter-web
+ 2.4.8
+
com.xchb
server-exmple
@@ -46,6 +52,12 @@
com.xchb
xrpc-core
${xrpc.versin}
+
+
+ org.springframework
+ spring-context
+
+
diff --git a/xrpc-demo/server-exmple/pom.xml b/xrpc-demo/server-exmple/pom.xml
index 9168a37..2cb0272 100644
--- a/xrpc-demo/server-exmple/pom.xml
+++ b/xrpc-demo/server-exmple/pom.xml
@@ -10,8 +10,13 @@
4.0.0
server-exmple
+ jar
+
+ org.springframework.boot
+ spring-boot-starter-web
+
com.xchb
server-api
diff --git a/xrpc-demo/server-exmple/src/main/java/com/xchb/xrpc/demo/MainServerApplication.java b/xrpc-demo/server-exmple/src/main/java/com/xchb/xrpc/demo/MainServerApplication.java
index fd5972d..99cd0bb 100644
--- a/xrpc-demo/server-exmple/src/main/java/com/xchb/xrpc/demo/MainServerApplication.java
+++ b/xrpc-demo/server-exmple/src/main/java/com/xchb/xrpc/demo/MainServerApplication.java
@@ -1,13 +1,9 @@
package com.xchb.xrpc.demo;
-import com.xchb.xrpc.util.AppConst;
+import com.xchb.xrpc.register.annotation.EnableXRpc;
import lombok.extern.slf4j.Slf4j;
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.annotation.AnnotationConfigApplicationContext;
-import com.xchb.xrpc.register.annotation.RpcScan;
-import com.xchb.xrpc.transport.server.ServerBooter;
-
-import java.io.IOException;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
@@ -17,15 +13,12 @@
* @description Good Good Study,Day Day Up.
*/
@Slf4j
-@RpcScan(basePackage = {"com.xchb.xrpc.demo"})
+@EnableXRpc(basePackage = {"com.xchb.xrpc.demo"})
+@SpringBootApplication
public class MainServerApplication {
- public static void main(String[] args) throws InterruptedException, IOException {
- //加载spring容器
- ApplicationContext applicationContext =
- new AnnotationConfigApplicationContext(MainServerApplication.class);
-
- System.in.read();
+ public static void main(String[] args) {
+ SpringApplication.run(MainServerApplication.class);
}
}
diff --git a/xrpc-demo/server-exmple/src/main/java/com/xchb/xrpc/demo/config/XrpServerConfig.java b/xrpc-demo/server-exmple/src/main/java/com/xchb/xrpc/demo/config/XrpServerConfig.java
deleted file mode 100644
index 8711c44..0000000
--- a/xrpc-demo/server-exmple/src/main/java/com/xchb/xrpc/demo/config/XrpServerConfig.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package com.xchb.xrpc.demo.config;
-
-import com.xchb.xrpc.config.XrpcConfigProperties;
-import com.xchb.xrpc.spring.XrpcStartBean;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-/**
- * @author XDD
- * @project xrpc
- * @date 2021/7/26
- * @description Good Good Study,Day Day Up.
- */
-@Configuration
-public class XrpServerConfig {
-
- @Bean
- public XrpcStartBean xrpcStartBean() throws InterruptedException {
- XrpcConfigProperties configProperties = new XrpcConfigProperties();
- configProperties.setExportPort(9091);
- configProperties.setZkAddr("localhost:2181");
- configProperties.setServer(true);
- return new XrpcStartBean(configProperties);
- }
-
-}
diff --git a/xrpc-demo/server-exmple/src/main/java/com/xchb/xrpc/demo/service/HelloServiceTwoImpl.java b/xrpc-demo/server-exmple/src/main/java/com/xchb/xrpc/demo/service/HelloServiceTwoImpl.java
deleted file mode 100644
index 9d55d08..0000000
--- a/xrpc-demo/server-exmple/src/main/java/com/xchb/xrpc/demo/service/HelloServiceTwoImpl.java
+++ /dev/null
@@ -1,20 +0,0 @@
-package com.xchb.xrpc.demo.service;
-
-import com.xchb.xrpc.api.HelloService;
-import com.xchb.xrpc.register.annotation.RpcService;
-
-/**
- * @author XDD
- * @project xrpc
- * @date 2021/1/17
- * @description Good Good Study,Day Day Up.
- */
-@RpcService
-public class HelloServiceTwoImpl implements HelloService {
-
- @Override
- public String hello(String name) {
- return this.getClass().getCanonicalName()+":"+name;
- }
-
-}
diff --git a/xrpc-demo/server-exmple/src/main/java/com/xchb/xrpc/demo/service/UserServiceImpl.java b/xrpc-demo/server-exmple/src/main/java/com/xchb/xrpc/demo/service/UserServiceImpl.java
index a180dc2..8309020 100644
--- a/xrpc-demo/server-exmple/src/main/java/com/xchb/xrpc/demo/service/UserServiceImpl.java
+++ b/xrpc-demo/server-exmple/src/main/java/com/xchb/xrpc/demo/service/UserServiceImpl.java
@@ -1,8 +1,8 @@
package com.xchb.xrpc.demo.service;
import cn.hutool.core.lang.Assert;
-import com.xchb.xrpc.entity.User;
import com.xchb.xrpc.api.UserService;
+import com.xchb.xrpc.entity.User;
import com.xchb.xrpc.register.annotation.RpcService;
import java.util.Map;
diff --git a/xrpc-demo/server-exmple/src/main/resources/application.yml b/xrpc-demo/server-exmple/src/main/resources/application.yml
new file mode 100644
index 0000000..5f17c70
--- /dev/null
+++ b/xrpc-demo/server-exmple/src/main/resources/application.yml
@@ -0,0 +1,5 @@
+server:
+ port: 8989
+
+app:
+ export-port: 9556
\ No newline at end of file