Commit 0e92d305 by huangfusuper

rpc插件端再封装

parent 31c58003
...@@ -45,7 +45,7 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<RpcResponse> ...@@ -45,7 +45,7 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<RpcResponse>
logger.debug(">>>>>>>>>>> myth-rpc netty client close an idle channel.");*/ logger.debug(">>>>>>>>>>> myth-rpc netty client close an idle channel.");*/
nettyConnectClient.send(Beat.BEAT_PING); // beat N, close if fail(may throw error) nettyConnectClient.send(Beat.BEAT_PING); // beat N, close if fail(may throw error)
logger.debug(">>>>>>>>>>> myth-rpc netty client send beat-ping."); logger.info(">>>>>>>>>>> myth-rpc netty client send beat-ping."+ctx.channel().id().asShortText());
} else { } else {
super.userEventTriggered(ctx, evt); super.userEventTriggered(ctx, evt);
......
...@@ -71,7 +71,7 @@ public class NettyServerHandler extends SimpleChannelInboundHandler<RpcRequest> ...@@ -71,7 +71,7 @@ public class NettyServerHandler extends SimpleChannelInboundHandler<RpcRequest>
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent){ if (evt instanceof IdleStateEvent){
ctx.channel().close(); // beat 3N, close if idle ctx.channel().close(); // beat 3N, close if idle
logger.debug(">>>>>>>>>>> myth-rpc provider netty server close an idle channel."); logger.info(">>>>>>>>>>> myth-rpc provider netty server close an idle channel."+ctx.channel().id());
} else { } else {
super.userEventTriggered(ctx, evt); super.userEventTriggered(ctx, evt);
} }
......
package com.byit.param;
import com.byit.packet.request.PluginRpcRequestPacket;
/**
* 心跳定义
* @author huangfu
*/
public final class PluginBeat {
public static final int BEAT_INTERVAL = 30;
public static final String BEAT_ID = "BEAT_PING_PONG";
public static PluginRpcRequestPacket PLUGIN_RPC_REQUEST_PACKET;
static {
PLUGIN_RPC_REQUEST_PACKET = new PluginRpcRequestPacket();
PLUGIN_RPC_REQUEST_PACKET.setRequestId(BEAT_ID);
}
}
\ No newline at end of file
...@@ -3,8 +3,10 @@ package com.byit.client.handler; ...@@ -3,8 +3,10 @@ package com.byit.client.handler;
import com.byit.future.PluginFutureResponse; import com.byit.future.PluginFutureResponse;
import com.byit.init.PluginClientInitialization; import com.byit.init.PluginClientInitialization;
import com.byit.packet.response.PluginRpcResponsePacket; import com.byit.packet.response.PluginRpcResponsePacket;
import com.byit.param.PluginBeat;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;
/** /**
* Netty客户端业务处理类 * Netty客户端业务处理类
...@@ -24,4 +26,16 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<PluginRpcRes ...@@ -24,4 +26,16 @@ public class NettyClientHandler extends SimpleChannelInboundHandler<PluginRpcRes
PluginFutureResponse pluginFutureResponseMap = pluginClientInitialization.getPluginClientFactory().getPluginFutureResponseMap(msg.getRequestId()); PluginFutureResponse pluginFutureResponseMap = pluginClientInitialization.getPluginClientFactory().getPluginFutureResponseMap(msg.getRequestId());
pluginFutureResponseMap.setRpcResponsePacket(msg); pluginFutureResponseMap.setRpcResponsePacket(msg);
} }
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
//判断事件是否是心跳事件
if( evt instanceof IdleStateEvent){
pluginConnectClient.send(PluginBeat.PLUGIN_RPC_REQUEST_PACKET);
System.out.println("------客户端发送心跳请求-----"+ctx.channel().id().asShortText());
}else{
super.userEventTriggered(ctx, evt);
}
}
} }
...@@ -5,12 +5,17 @@ import com.byit.handler.PacketDecodeHandler; ...@@ -5,12 +5,17 @@ import com.byit.handler.PacketDecodeHandler;
import com.byit.handler.PacketEncodeHandler; import com.byit.handler.PacketEncodeHandler;
import com.byit.init.PluginClientInitialization; import com.byit.init.PluginClientInitialization;
import com.byit.packet.request.PluginRpcRequestPacket; import com.byit.packet.request.PluginRpcRequestPacket;
import com.byit.param.PluginBeat;
import com.byit.rpc.remoting.net.params.Beat;
import com.byit.utils.IpUtil; import com.byit.utils.IpUtil;
import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*; import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import java.util.concurrent.TimeUnit;
/** /**
* netty客户端 * netty客户端
...@@ -39,6 +44,8 @@ public class NettyPluginConnectionClient extends PluginConnectClient { ...@@ -39,6 +44,8 @@ public class NettyPluginConnectionClient extends PluginConnectClient {
@Override @Override
protected void initChannel(SocketChannel ch) throws Exception { protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline(); ChannelPipeline pipeline = ch.pipeline();
//心跳介入 30s向服务器发送一次心跳 维持连接
pipeline.addLast("idleStateHandler",new IdleStateHandler(0,0, PluginBeat.BEAT_INTERVAL, TimeUnit.SECONDS));
pipeline.addLast("packerSpliterHandler",new PackerSpliterHandler()); pipeline.addLast("packerSpliterHandler",new PackerSpliterHandler());
pipeline.addLast("packetDecodeHandler",new PacketDecodeHandler()); pipeline.addLast("packetDecodeHandler",new PacketDecodeHandler());
pipeline.addLast("nettyClientHandler",new NettyClientHandler(pluginClientInitialization,thisClient)); pipeline.addLast("nettyClientHandler",new NettyClientHandler(pluginClientInitialization,thisClient));
...@@ -48,6 +55,7 @@ public class NettyPluginConnectionClient extends PluginConnectClient { ...@@ -48,6 +55,7 @@ public class NettyPluginConnectionClient extends PluginConnectClient {
this.channel = bootstrap.connect(ip, port).sync().channel(); this.channel = bootstrap.connect(ip, port).sync().channel();
// valid // valid
if (!isValidate()) { if (!isValidate()) {
System.out.println("------关闭链接");
close(); close();
return; return;
} }
...@@ -57,6 +65,7 @@ public class NettyPluginConnectionClient extends PluginConnectClient { ...@@ -57,6 +65,7 @@ public class NettyPluginConnectionClient extends PluginConnectClient {
@Override @Override
public void close() { public void close() {
if(this.channel !=null && isValidate()){ if(this.channel !=null && isValidate()){
System.out.println("------关闭链接"+channel.id().asShortText());
this.channel.close(); this.channel.close();
} }
......
...@@ -3,11 +3,6 @@ package com.byit.client.handler; ...@@ -3,11 +3,6 @@ package com.byit.client.handler;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import com.byit.init.PluginClientInitialization; import com.byit.init.PluginClientInitialization;
import com.byit.packet.request.PluginRpcRequestPacket; import com.byit.packet.request.PluginRpcRequestPacket;
import com.byit.rpc.remoting.invoker.RpcInvokerFactory;
import com.byit.rpc.remoting.invoker.reference.RpcReferenceBean;
import com.byit.rpc.remoting.net.common.ConnectClient;
import com.byit.rpc.remoting.net.params.RpcRequest;
import com.byit.rpc.serialize.Serializer;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
...@@ -71,17 +66,19 @@ public abstract class PluginConnectClient { ...@@ -71,17 +66,19 @@ public abstract class PluginConnectClient {
private static PluginConnectClient getPool(String address, Class<? extends PluginConnectClient> connectClientImpl, private static PluginConnectClient getPool(String address, Class<? extends PluginConnectClient> connectClientImpl,
final PluginClientInitialization pluginClientInitialization) throws Exception { final PluginClientInitialization pluginClientInitialization) throws Exception {
//初始化连接池 为了避免重复初始化 需要判断 //初始化连接池 为了避免重复初始化 需要判断
if (connectClientMap == null) { if (connectClientMap == null) {
synchronized (PluginConnectClient.class){ synchronized (PluginConnectClient.class){
if (connectClientMap == null) { if (connectClientMap == null) {
System.out.println("--------------------------");
connectClientMap = new ConcurrentHashMap<>(8); connectClientMap = new ConcurrentHashMap<>(8);
pluginClientInitialization.getPluginClientFactory().addStopCallback(() ->{ pluginClientInitialization.getPluginClientFactory().addStopCallback(() ->{
if (CollectionUtil.isNotEmpty(connectClientMap)) { if (CollectionUtil.isNotEmpty(connectClientMap)) {
connectClientMap.forEach((key,value) ->{ connectClientMap.forEach((key,value) ->{
value.close(); value.close();
}); });
connectClientMap.clear(); //connectClientMap.clear();
} }
}); });
} }
...@@ -107,13 +104,21 @@ public abstract class PluginConnectClient { ...@@ -107,13 +104,21 @@ public abstract class PluginConnectClient {
} }
if(pluginConnectClient != null){ if(pluginConnectClient != null){
pluginConnectClient.close();
connectClientMap.remove(address); connectClientMap.remove(address);
} }
//实例化连接对象
PluginConnectClient newPluginConnectClient = connectClientImpl.newInstance(); PluginConnectClient newPluginConnectClient = connectClientImpl.newInstance();
newPluginConnectClient.init(address,pluginClientInitialization); try{
//实例化连接对象
newPluginConnectClient.init(address,pluginClientInitialization);
connectClientMap.put(address,newPluginConnectClient);
}catch (Exception e){
newPluginConnectClient.close();
throw e;
}
//重新在连接池里设置 //重新在连接池里设置
connectClientLockMap.put(address,newPluginConnectClient);
return newPluginConnectClient; return newPluginConnectClient;
} }
} }
......
...@@ -26,31 +26,23 @@ public class PluginSpringClientFactory extends InstantiationAwareBeanPostProcess ...@@ -26,31 +26,23 @@ public class PluginSpringClientFactory extends InstantiationAwareBeanPostProcess
private PluginClientFactory pluginClientFactory; private PluginClientFactory pluginClientFactory;
private Class<? extends PluginServiceRegistry> pluginServiceRegistryClass; private Class<? extends PluginServiceRegistry> pluginServiceRegistryClass;
private Class<? extends PluginClient> pluginClientClass; private Class<? extends PluginClient> pluginClientClass;
@Value("${myth.register.url}")
private String registryUrl; private String registryUrl;
@Value("${myth.plugin.env}")
private String env; private String env;
@Value("${myth.plugin.biz}")
private String biz; private String biz;
public PluginSpringClientFactory( Class<? extends PluginServiceRegistry> pluginServiceRegistryClass, Class<? extends PluginClient> pluginClientClass) { public PluginSpringClientFactory(String registryUrl, String env, String biz) {
this.pluginServiceRegistryClass = pluginServiceRegistryClass; this(null,null);
this.pluginClientClass = pluginClientClass; this.registryUrl = registryUrl;
this.env = env;
if (this.pluginServiceRegistryClass == null) { this.biz = biz;
this.pluginServiceRegistryClass = DataSourceServiceRegistry.class;
}
if (this.pluginClientClass == null) {
this.pluginClientClass = NettyPluginClient.class;
}
} }
public PluginSpringClientFactory(Class<? extends PluginServiceRegistry> pluginServiceRegistryClass, public PluginSpringClientFactory(Class<? extends PluginServiceRegistry> pluginServiceRegistryClass,
String registryUrl, String env, String biz, Class<? extends PluginClient> pluginClientClass) { ; Class<? extends PluginClient> pluginClientClass) {
this.pluginServiceRegistryClass = pluginServiceRegistryClass; this.pluginServiceRegistryClass = pluginServiceRegistryClass;
this.registryUrl = registryUrl;
this.env = env;
this.biz = biz;
this.pluginClientClass = pluginClientClass; this.pluginClientClass = pluginClientClass;
if (this.pluginServiceRegistryClass == null) { if (this.pluginServiceRegistryClass == null) {
...@@ -61,6 +53,7 @@ public class PluginSpringClientFactory extends InstantiationAwareBeanPostProcess ...@@ -61,6 +53,7 @@ public class PluginSpringClientFactory extends InstantiationAwareBeanPostProcess
} }
} }
/** /**
* 属性设置之后调用 * 属性设置之后调用
* @throws Exception * @throws Exception
......
...@@ -19,19 +19,18 @@ import java.util.Map; ...@@ -19,19 +19,18 @@ import java.util.Map;
* @author huangfu * @author huangfu
*/ */
public class RpcSpringPluginServerFactory extends PluginServerFactory implements ApplicationContextAware, InitializingBean, DisposableBean { public class RpcSpringPluginServerFactory extends PluginServerFactory implements ApplicationContextAware, InitializingBean, DisposableBean {
@Value("${myth.register.url}")
private String address; private String address;
@Value("${myth.plugin.biz}")
private String biz; private String biz;
@Value("${myth.plugin.env}")
private String env; private String env;
@Value("${myth.plugin.port}")
private int port; private int port;
public RpcSpringPluginServerFactory() { public RpcSpringPluginServerFactory(String address, String biz, String env, int port) {
this.address = address;
this.biz = biz;
this.env = env;
this.port = port;
} }
@Override @Override
public void destroy() throws Exception { public void destroy() throws Exception {
super.stop(); super.stop();
......
...@@ -4,6 +4,7 @@ import com.byit.factory.PluginServerFactory; ...@@ -4,6 +4,7 @@ import com.byit.factory.PluginServerFactory;
import com.byit.handler.PackerSpliterHandler; import com.byit.handler.PackerSpliterHandler;
import com.byit.handler.PacketDecodeHandler; import com.byit.handler.PacketDecodeHandler;
import com.byit.handler.PacketEncodeHandler; import com.byit.handler.PacketEncodeHandler;
import com.byit.param.PluginBeat;
import com.byit.server.PluginServer; import com.byit.server.PluginServer;
import com.byit.server.netty.handler.NettyPluginServerHandler; import com.byit.server.netty.handler.NettyPluginServerHandler;
import com.byit.utils.ThreadPoolUtil; import com.byit.utils.ThreadPoolUtil;
...@@ -12,8 +13,10 @@ import io.netty.channel.*; ...@@ -12,8 +13,10 @@ import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/** /**
* main方法启动 * main方法启动
...@@ -38,6 +41,7 @@ public class MainNettyPluginServer extends PluginServer { ...@@ -38,6 +41,7 @@ public class MainNettyPluginServer extends PluginServer {
@Override @Override
protected void initChannel(SocketChannel ch) { protected void initChannel(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline(); ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("idleStateHandler",new IdleStateHandler(0,0, PluginBeat.BEAT_INTERVAL * 3, TimeUnit.SECONDS));
pipeline.addLast("packerSpliterHandler",new PackerSpliterHandler()); pipeline.addLast("packerSpliterHandler",new PackerSpliterHandler());
pipeline.addLast("packetDecodeHandler",new PacketDecodeHandler()); pipeline.addLast("packetDecodeHandler",new PacketDecodeHandler());
pipeline.addLast("nettyPluginServerHandler",new NettyPluginServerHandler(pluginServerFactory,threadPoolExecutor)); pipeline.addLast("nettyPluginServerHandler",new NettyPluginServerHandler(pluginServerFactory,threadPoolExecutor));
......
...@@ -4,6 +4,7 @@ import com.byit.factory.PluginServerFactory; ...@@ -4,6 +4,7 @@ import com.byit.factory.PluginServerFactory;
import com.byit.handler.PackerSpliterHandler; import com.byit.handler.PackerSpliterHandler;
import com.byit.handler.PacketDecodeHandler; import com.byit.handler.PacketDecodeHandler;
import com.byit.handler.PacketEncodeHandler; import com.byit.handler.PacketEncodeHandler;
import com.byit.param.PluginBeat;
import com.byit.server.PluginServer; import com.byit.server.PluginServer;
import com.byit.server.netty.handler.NettyPluginServerHandler; import com.byit.server.netty.handler.NettyPluginServerHandler;
import com.byit.utils.ThreadPoolUtil; import com.byit.utils.ThreadPoolUtil;
...@@ -12,8 +13,10 @@ import io.netty.channel.*; ...@@ -12,8 +13,10 @@ import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/** /**
* Netty服务启动 * Netty服务启动
...@@ -40,6 +43,7 @@ public class NettyPluginServer extends PluginServer { ...@@ -40,6 +43,7 @@ public class NettyPluginServer extends PluginServer {
@Override @Override
protected void initChannel(SocketChannel ch) { protected void initChannel(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline(); ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("idleStateHandler",new IdleStateHandler(0,0, PluginBeat.BEAT_INTERVAL * 3, TimeUnit.SECONDS));
pipeline.addLast("packerSpliterHandler",new PackerSpliterHandler()); pipeline.addLast("packerSpliterHandler",new PackerSpliterHandler());
pipeline.addLast("packetDecodeHandler",new PacketDecodeHandler()); pipeline.addLast("packetDecodeHandler",new PacketDecodeHandler());
pipeline.addLast("nettyPluginServerHandler",new NettyPluginServerHandler(pluginServerFactory,threadPoolExecutor)); pipeline.addLast("nettyPluginServerHandler",new NettyPluginServerHandler(pluginServerFactory,threadPoolExecutor));
......
...@@ -6,10 +6,12 @@ import com.byit.enums.ResponseTyEnum; ...@@ -6,10 +6,12 @@ import com.byit.enums.ResponseTyEnum;
import com.byit.factory.PluginServerFactory; import com.byit.factory.PluginServerFactory;
import com.byit.packet.request.PluginRpcRequestPacket; import com.byit.packet.request.PluginRpcRequestPacket;
import com.byit.packet.response.PluginRpcResponsePacket; import com.byit.packet.response.PluginRpcResponsePacket;
import com.byit.param.PluginBeat;
import com.byit.param.ResultCallback; import com.byit.param.ResultCallback;
import com.byit.task.handler.interfaces.IJobHandler; import com.byit.task.handler.interfaces.IJobHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
...@@ -36,6 +38,10 @@ public class NettyPluginServerHandler extends SimpleChannelInboundHandler<Plugin ...@@ -36,6 +38,10 @@ public class NettyPluginServerHandler extends SimpleChannelInboundHandler<Plugin
*/ */
@Override @Override
protected void channelRead0(ChannelHandlerContext ctx, PluginRpcRequestPacket msg) throws Exception { protected void channelRead0(ChannelHandlerContext ctx, PluginRpcRequestPacket msg) throws Exception {
if(PluginBeat.BEAT_ID.equals(msg.getRequestId())){
System.out.println("------接收到客户端的心跳连接-------"+ctx.channel().id().asShortText());
return;
}
PluginRpcResponsePacket transferPluginRpcResponse = new PluginRpcResponsePacket(); PluginRpcResponsePacket transferPluginRpcResponse = new PluginRpcResponsePacket();
threadPoolExecutor.execute(()->{ threadPoolExecutor.execute(()->{
PluginRpcResponsePacket rpcResponsePacket = new PluginRpcResponsePacket(); PluginRpcResponsePacket rpcResponsePacket = new PluginRpcResponsePacket();
...@@ -87,5 +93,13 @@ public class NettyPluginServerHandler extends SimpleChannelInboundHandler<Plugin ...@@ -87,5 +93,13 @@ public class NettyPluginServerHandler extends SimpleChannelInboundHandler<Plugin
ctx.close(); ctx.close();
} }
//TODO 心跳检测功能暂时不添加 @Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent){
ctx.channel().close();
System.out.println("-------心跳超时,关闭链接-------");
} else {
super.userEventTriggered(ctx, evt);
}
}
} }
<?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>byit-plugin-core</artifactId>
<groupId>myth-job</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>plugin-spring-boot-starter</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>myth-job</groupId>
<artifactId>myth-plugin-rpc-client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>myth-job</groupId>
<artifactId>myth-plugin-rpc-server</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.byit.annotations;
import com.byit.client.PluginClient;
import com.byit.marks.PluginClientMark;
import com.byit.marks.PluginServerMark;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* 插件服务端启动
* @author huangfu
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import(PluginClientMark.class)
public @interface EnablePluginClient {
}
package com.byit.annotations;
import com.byit.marks.PluginServerMark;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* 插件服务端启动
* @author huangfu
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import(PluginServerMark.class)
public @interface EnablePluginService {
}
package com.byit.configuration;
import com.byit.factory.PluginSpringClientFactory;
import com.byit.factory.RpcSpringPluginServerFactory;
import com.byit.marks.PluginClientMark;
import com.byit.marks.PluginServerMark;
import com.byit.model.MythConfigModel;
import com.byit.model.PluginConfigurationModel;
import com.byit.model.RegisteredConfigurationModel;
import com.byit.server.netty.NettyPluginServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 插件配置类
* @author huangfu
*/
@Configuration
@EnableConfigurationProperties(MythConfigModel.class)
public class PluginAutoConfigure {
private final MythConfigModel mythConfigModel;
public PluginAutoConfigure(MythConfigModel mythConfigModel) {
this.mythConfigModel = mythConfigModel;
}
@Bean
@ConditionalOnBean(PluginServerMark.class)
public RpcSpringPluginServerFactory pluginServerFactory(){
PluginConfigurationModel plugin = mythConfigModel.getPlugin();
RegisteredConfigurationModel register = mythConfigModel.getRegister();
RpcSpringPluginServerFactory rpcSpringPluginServerFactory =
new RpcSpringPluginServerFactory(register.getUrl(),plugin.getBiz(),plugin.getEnv(),plugin.getPort());
rpcSpringPluginServerFactory.setPluginServerClass(NettyPluginServer.class);
return rpcSpringPluginServerFactory;
}
@Bean
@ConditionalOnBean(PluginClientMark.class)
public PluginSpringClientFactory pluginSpringClientFactory(){
PluginConfigurationModel plugin = mythConfigModel.getPlugin();
RegisteredConfigurationModel register = mythConfigModel.getRegister();
return new PluginSpringClientFactory(register.getUrl(),plugin.getEnv(),plugin.getBiz());
}
}
package com.byit.marks;
/**
* 客户端标记类
* @author huangfu
*/
public class PluginClientMark {
}
package com.byit.marks;
/**
* 插件服务启动的标记类
* @author huangfu
*/
public class PluginServerMark {
}
package com.byit.model;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 所需配置信息的配置项
* @author huangfu
*/
@ConfigurationProperties("myth")
public class MythConfigModel {
private PluginConfigurationModel plugin;
private RegisteredConfigurationModel register;
public PluginConfigurationModel getPlugin() {
return plugin;
}
public void setPlugin(PluginConfigurationModel plugin) {
this.plugin = plugin;
}
public RegisteredConfigurationModel getRegister() {
return register;
}
public void setRegister(RegisteredConfigurationModel register) {
this.register = register;
}
}
package com.byit.model;
/**
* 插件端配置实体
* @author huangfu
*/
public class PluginConfigurationModel {
private String env;
private String biz;
private Integer port;
public String getEnv() {
return env;
}
public void setEnv(String env) {
this.env = env;
}
public String getBiz() {
return biz;
}
public void setBiz(String biz) {
this.biz = biz;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
}
package com.byit.model;
/**
* 对于注册中心的配置解析
* @author huangfu
*/
public class RegisteredConfigurationModel {
private String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.byit.configuration.PluginAutoConfigure
\ No newline at end of file
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
<module>myth-plugin-rpc-server</module> <module>myth-plugin-rpc-server</module>
<module>myth-plugin-rpc-client</module> <module>myth-plugin-rpc-client</module>
<module>byit-plugin-rpc-common</module> <module>byit-plugin-rpc-common</module>
<module>plugin-spring-boot-starter</module>
</modules> </modules>
......
...@@ -16,7 +16,7 @@ public class AddComplexPy1 { ...@@ -16,7 +16,7 @@ public class AddComplexPy1 {
PluginPackage pluginPackage = new PluginPackage(); PluginPackage pluginPackage = new PluginPackage();
pluginPackage.setWorkspaceName("test"); pluginPackage.setWorkspaceName("test");
pluginPackage.setFlow(createFlow()); pluginPackage.setFlow(createFlow());
JobUtils.setRequestUrl("http://10.0.120.208:8081/myth-job-admin"); JobUtils.setRequestUrl("http://127.0.0.1:8081/myth-job-admin");
JobUtils.setTOKEN("test"); JobUtils.setTOKEN("test");
JobUtils.publish(pluginPackage); JobUtils.publish(pluginPackage);
} }
...@@ -35,7 +35,7 @@ public class AddComplexPy1 { ...@@ -35,7 +35,7 @@ public class AddComplexPy1 {
.scheduleFollow("1") .scheduleFollow("1")
.build(); .build();
pluginFlow.setName("复杂工作流"); pluginFlow.setName("复杂工作流11");
pluginFlow.setDesc("测试多脚本复杂工作流创建"); pluginFlow.setDesc("测试多脚本复杂工作流创建");
pluginFlow.setConfig(build); pluginFlow.setConfig(build);
pluginFlow.setPrincipal("皇甫科星"); pluginFlow.setPrincipal("皇甫科星");
......
package com.byit.conf; package com.byit.conf;
import com.byit.factory.PluginSpringClientFactory; import com.byit.factory.PluginSpringClientFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
/**
* @author huangfu
*/
@Configuration @Configuration
public class ClientConf { public class ClientConf {
@Value("${myth.register.url}")
private String registryUrl;
@Value("${myth.plugin.env}")
private String env;
@Value("${myth.plugin.biz}")
private String biz;
@Bean @Bean
public PluginSpringClientFactory pluginSpringClientFactory(){ public PluginSpringClientFactory pluginSpringClientFactory(){
PluginSpringClientFactory pluginSpringClientFactory = new PluginSpringClientFactory(null,null); PluginSpringClientFactory pluginSpringClientFactory =
new PluginSpringClientFactory(registryUrl,env,biz);
return pluginSpringClientFactory; return pluginSpringClientFactory;
} }
} }
...@@ -2,6 +2,7 @@ package com.byit.conf; ...@@ -2,6 +2,7 @@ package com.byit.conf;
import com.byit.factory.RpcSpringPluginServerFactory; import com.byit.factory.RpcSpringPluginServerFactory;
import com.byit.server.netty.NettyPluginServer; import com.byit.server.netty.NettyPluginServer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -10,10 +11,20 @@ import org.springframework.context.annotation.Configuration; ...@@ -10,10 +11,20 @@ import org.springframework.context.annotation.Configuration;
*/ */
@Configuration @Configuration
public class PluginConf { public class PluginConf {
@Value("${myth.register.url}")
private String address;
@Value("${myth.plugin.biz}")
private String biz;
@Value("${myth.plugin.env}")
private String env;
@Value("${myth.plugin.port}")
private int port;
@Bean @Bean
public RpcSpringPluginServerFactory rpcSpringPluginServerFactory(){ public RpcSpringPluginServerFactory rpcSpringPluginServerFactory(){
RpcSpringPluginServerFactory rpcSpringPluginServerFactory = new RpcSpringPluginServerFactory(); RpcSpringPluginServerFactory rpcSpringPluginServerFactory = new RpcSpringPluginServerFactory(
address,biz,env,port
);
rpcSpringPluginServerFactory.setPluginServerClass(NettyPluginServer.class); rpcSpringPluginServerFactory.setPluginServerClass(NettyPluginServer.class);
return rpcSpringPluginServerFactory; return rpcSpringPluginServerFactory;
} }
......
<?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>demo-client</artifactId>
<groupId>myth-job</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>plugin-test-spring-boot</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>myth-job</groupId>
<artifactId>plugin-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.byit;
import com.byit.annotations.EnablePluginService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author huangfu
*/
@SpringBootApplication
//@EnablePluginService
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class,args);
}
}
package com.byit.server;
import com.byit.dto.web.ReturnResult;
import com.byit.task.annotations.TaskHandler;
import com.byit.task.handler.BaseJobHandler;
import org.springframework.stereotype.Component;
/**
* @author huangfu
*/
@Component
@TaskHandler(taskName = "sentEmailServer")
public class SentEmailServer extends BaseJobHandler {
@Override
public ReturnResult<String> execute(String param) throws Exception {
return ReturnResult.SUCCESS;
}
}
server:
port: 8970
myth:
plugin:
env: plugin_test
biz: byit-myth-job
port: 8971
register:
url: http://localhost:8080/myth-register
\ No newline at end of file
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
<module>byit-demo-service</module> <module>byit-demo-service</module>
<module>byit-plugin-server</module> <module>byit-plugin-server</module>
<module>byit-plugin-client</module> <module>byit-plugin-client</module>
<module>plugin-test-spring-boot</module>
</modules> </modules>
......
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