Commit 5deaf41d by huangfusuper

rpc服务端 服务工厂数据初始化方式改变为配置文件赋值

parent 6774216f
......@@ -17,7 +17,6 @@ import java.util.Map;
@AllArgsConstructor
@ToString
public class ServiceConfigModel {
private Boolean autoScan;
private ServerConfigurationModel serverConfigurationModel;
private Map<String,String> classNames;
}
......@@ -2,21 +2,16 @@ package com.byit.factory;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.net.NetUtil;
import com.byit.model.ServerConfigurationModel;
import com.byit.model.ServiceConfigModel;
import com.byit.registry.DataSourceServiceRegistry;
import com.byit.registry.PluginServiceRegistry;
import com.byit.registry.client.model.RegistryDataParamVO;
import com.byit.server.NettyPluginServer;
import com.byit.server.PluginServer;
import com.byit.utils.IpUtil;
import com.byit.utils.XmlParseUtil;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* 插件服务端的工厂对象
......@@ -27,6 +22,10 @@ public abstract class PluginServerFactory {
private final static String DEFAULT_ENV_NAME="plugin-netty";
private final static Class<? extends PluginServiceRegistry> DEFAULT_SERVICE_REGISTRY_CLASS = DataSourceServiceRegistry.class;
private final static Class<? extends PluginServer> DEFAULT_NETTY_PLUGIN_SERVER_CLASS = NettyPluginServer.class;
/**
* 任务缓存处理器
*/
private Map<String, Object> serverPoll = new ConcurrentHashMap<>(8);
private int corePoolSize;
private int maxPoolSize;
private String registryUrl;
......@@ -44,38 +43,16 @@ public abstract class PluginServerFactory {
private Class<? extends PluginServer> pluginServerClass;
private PluginServer pluginServer;
private PluginServiceRegistry pluginServiceRegistry;
private Set<String> serverKeys;
private Set<String> serverKeys = new HashSet<>(8);
private List<RegistryDataParamVO> registryDataParamVOs = new ArrayList<>(2);
public PluginServerFactory() {
}
public PluginServerFactory(String configLocation) {
ServiceConfigModel serviceConfigModel = XmlParseUtil.parse(configLocation);
ServerConfigurationModel serverConfigurationModel = serviceConfigModel.getServerConfigurationModel();
String pluginServiceRegistryStr = serverConfigurationModel.getPluginServiceRegistry();
String pluginServerClassStr = serverConfigurationModel.getPluginServerClass();
Class<? extends PluginServiceRegistry> pluginServiceRegistryClass = null;
Class<? extends PluginServer> pluginServerClass = null;
try {
pluginServiceRegistryClass = (Class<? extends PluginServiceRegistry>) Class.forName(pluginServiceRegistryStr);
pluginServerClass = (Class<? extends PluginServer>) Class.forName(pluginServerClassStr);
} catch (Exception e) {
e.printStackTrace();
}
Map<String, String> classNames = serviceConfigModel.getClassNames();
Set<String> keySet = classNames.keySet();
init(serverConfigurationModel.getCoreSize(),serverConfigurationModel.getMaxSize(),
serverConfigurationModel.getRegistryUrl(),serverConfigurationModel.getServerEnv(),
serverConfigurationModel.getServerBiz(),pluginServiceRegistryClass,pluginServerClass,keySet);
}
public void init(int corePoolSize, int maxPoolSize, String registryUrl, String env, String biz,
public void init(int corePoolSize, int maxPoolSize,Integer port, String registryUrl, String env, String biz,
Class<? extends PluginServiceRegistry> serviceRegistryClass,
Class<? extends PluginServer> pluginServerClass, Set<String> serverKeys){
Class<? extends PluginServer> pluginServerClass){
if(!(corePoolSize>0 && maxPoolSize>0 && maxPoolSize>=corePoolSize)){
this.corePoolSize = 60;
......@@ -129,10 +106,17 @@ public abstract class PluginServerFactory {
if(this.pluginServerClass == null){
throw new RuntimeException("插件端使用的服务类型不能为空!");
}
}
this.serverKeys = serverKeys;
public void addService(String key,Object serverBean){
serverKeys.add(key);
serverPoll.put(key,serverBean);
}
/**
* 启动方法
* @throws Exception
*/
public void start() throws Exception {
pluginServer = pluginServerClass.newInstance();
pluginServiceRegistry = serviceRegistryClass.newInstance();
......
package com.byit.factory;
import com.byit.executor.handler.interfaces.IJobHandler;
import com.byit.model.ServerConfigurationModel;
import com.byit.model.ServiceConfigModel;
import com.byit.registry.PluginServiceRegistry;
import com.byit.server.PluginServer;
import com.byit.utils.XmlParseUtil;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
......@@ -12,15 +17,68 @@ import java.util.Set;
*/
public class RpcMainPluginServerFactory extends PluginServerFactory {
public RpcMainPluginServerFactory(int corePoolSize, int maxPoolSize, String registryUrl, String env, String biz, Set<String> serverKeys, Class<? extends PluginServiceRegistry> serviceRegistryClass, Class<? extends PluginServer> pluginServerClass) {
init(corePoolSize,maxPoolSize,registryUrl,env,biz,serviceRegistryClass,pluginServerClass,serverKeys);
/**
* 全属性传递注入
* @param corePoolSize 核心线程池大小
* @param maxPoolSize 最大线程池大小
* @param port 端口号
* @param registryUrl 注册中心地址
* @param env 环境标识
* @param biz 工程名称
* @param serverKeys 服务key
* @param serviceRegistryClass 注册中心类
* @param pluginServerClass 插件服务类
*/
public RpcMainPluginServerFactory(int corePoolSize, int maxPoolSize,Integer port, String registryUrl, String env, String biz, Set<String> serverKeys, Class<? extends PluginServiceRegistry> serviceRegistryClass, Class<? extends PluginServer> pluginServerClass) {
init(corePoolSize,maxPoolSize,port,registryUrl,env,biz,serviceRegistryClass,pluginServerClass);
}
/**
* 配置文件注入
* @param configLocation
*/
public RpcMainPluginServerFactory(String configLocation) {
super(configLocation);
try {
ServiceConfigModel serviceConfigModel = XmlParseUtil.parse(configLocation);
ServerConfigurationModel serverConfigurationModel = serviceConfigModel.getServerConfigurationModel();
String pluginServiceRegistryStr = serverConfigurationModel.getPluginServiceRegistry();
String pluginServerClassStr = serverConfigurationModel.getPluginServerClass();
Class<? extends PluginServiceRegistry> pluginServiceRegistryClass = (Class<? extends PluginServiceRegistry>) Class.forName(pluginServiceRegistryStr);
Class<? extends PluginServer> pluginServerClass = (Class<? extends PluginServer>) Class.forName(pluginServerClassStr);
Map<String, String> classNames = serviceConfigModel.getClassNames();
super.init(serverConfigurationModel.getCoreSize(),serverConfigurationModel.getMaxSize(),
serverConfigurationModel.getPort(), serverConfigurationModel.getRegistryUrl(),
serverConfigurationModel.getServerEnv(), serverConfigurationModel.getServerBiz(),
pluginServiceRegistryClass,pluginServerClass);
classNames.forEach((key,value) ->{
try {
Object o = Class.forName(value).newInstance();
super.addService(key,o);
} catch (Exception e) {
e.printStackTrace();
}
});
super.start();
}catch (Exception e){
e.printStackTrace();
}
try {
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println(XmlParseUtil.parse("/plugin.xml"));
PluginServerFactory rpcMainPluginServerFactory = new RpcMainPluginServerFactory("/plugin.xml");
System.out.println(rpcMainPluginServerFactory);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment