Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
byit-myth-job
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
liyuan
byit-myth-job
Commits
abf792c4
Commit
abf792c4
authored
Apr 02, 2020
by
huangfusuper
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Plugin插件端服务代码编写
parent
2e97d21c
Hide whitespace changes
Inline
Side-by-side
Showing
22 changed files
with
1073 additions
and
0 deletions
+1073
-0
pom.xml
byit-plugin-core/byit-plugin-rpc-common/pom.xml
+51
-0
Command.java
...ugin-rpc-common/src/main/java/com/byit/enums/Command.java
+45
-0
SerializerAlgorithm.java
...mon/src/main/java/com/byit/enums/SerializerAlgorithm.java
+54
-0
PackerSpliterHandler.java
.../src/main/java/com/byit/handler/PackerSpliterHandler.java
+43
-0
PacketDecodeHandler.java
...n/src/main/java/com/byit/handler/PacketDecodeHandler.java
+22
-0
PacketEncodeHandler.java
...n/src/main/java/com/byit/handler/PacketEncodeHandler.java
+18
-0
BasePacketModel.java
...common/src/main/java/com/byit/packet/BasePacketModel.java
+33
-0
PluginRpcRequestPacket.java
.../java/com/byit/packet/request/PluginRpcRequestPacket.java
+36
-0
PluginRpcResponsePacket.java
...ava/com/byit/packet/response/PluginRpcResponsePacket.java
+30
-0
PluginCallback.java
...c-common/src/main/java/com/byit/param/PluginCallback.java
+13
-0
DataSourceServiceRegistry.java
...ain/java/com/byit/registry/DataSourceServiceRegistry.java
+64
-0
PluginServiceRegistry.java
...rc/main/java/com/byit/registry/PluginServiceRegistry.java
+49
-0
ISerializer.java
...mon/src/main/java/com/byit/serialization/ISerializer.java
+30
-0
JsonSerializer.java
.../src/main/java/com/byit/serialization/JsonSerializer.java
+25
-0
IpUtil.java
...lugin-rpc-common/src/main/java/com/byit/utils/IpUtil.java
+195
-0
PacketCodecUtil.java
...-common/src/main/java/com/byit/utils/PacketCodecUtil.java
+87
-0
pom.xml
byit-plugin-core/myth-plugin-rpc-client/pom.xml
+16
-0
pom.xml
byit-plugin-core/myth-plugin-rpc-server/pom.xml
+32
-0
PluginServerFactory.java
...r/src/main/java/com/byit/factory/PluginServerFactory.java
+157
-0
PluginServer.java
...pc-server/src/main/java/com/byit/server/PluginServer.java
+50
-0
pom.xml
byit-plugin-core/pom.xml
+22
-0
pom.xml
pom.xml
+1
-0
No files found.
byit-plugin-core/byit-plugin-rpc-common/pom.xml
0 → 100644
View file @
abf792c4
<?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>
byit-plugin-rpc-common
</artifactId>
<dependencies>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
</dependency>
<dependency>
<groupId>
cn.hutool
</groupId>
<artifactId>
hutool-all
</artifactId>
</dependency>
<dependency>
<groupId>
io.netty
</groupId>
<artifactId>
netty-all
</artifactId>
</dependency>
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-lang3
</artifactId>
</dependency>
<dependency>
<groupId>
org.projectlombok
</groupId>
<artifactId>
lombok
</artifactId>
</dependency>
<dependency>
<groupId>
myth-job
</groupId>
<artifactId>
myth-register-client
</artifactId>
<version>
1.0-SNAPSHOT
</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
byit-plugin-core/byit-plugin-rpc-common/src/main/java/com/byit/enums/Command.java
0 → 100644
View file @
abf792c4
package
com
.
byit
.
enums
;
import
com.byit.packet.BasePacketModel
;
import
com.byit.packet.request.PluginRpcRequestPacket
;
import
com.byit.packet.response.PluginRpcResponsePacket
;
/**
* @author 指令枚举
*/
public
enum
Command
{
RUN_REMOTELY_JOB_NODE_REQUEST
(
Byte
.
parseByte
(
"0"
),
PluginRpcRequestPacket
.
class
,
"执行远程任务节点命令请求!"
)
,
RUN_REMOTELY_JOB_NODE_RESPONSE
(
Byte
.
parseByte
(
"1"
),
PluginRpcResponsePacket
.
class
,
"执行远程任务节点的结果响应!"
)
,
;
private
byte
commandCode
;
private
Class
<?
extends
BasePacketModel
>
packet
;
private
String
commandDescription
;
public
byte
getCommandCode
()
{
return
commandCode
;
}
public
Class
<?
extends
BasePacketModel
>
getPacket
()
{
return
packet
;
}
Command
(
byte
commandCode
,
Class
<?
extends
BasePacketModel
>
packet
,
String
commandDescription
)
{
this
.
commandCode
=
commandCode
;
this
.
packet
=
packet
;
this
.
commandDescription
=
commandDescription
;
}
public
static
Class
<?
extends
BasePacketModel
>
match
(
byte
code
){
for
(
Command
command
:
Command
.
values
())
{
if
(
command
.
commandCode
==
code
)
{
return
command
.
packet
;
}
}
return
null
;
}
}
byit-plugin-core/byit-plugin-rpc-common/src/main/java/com/byit/enums/SerializerAlgorithm.java
0 → 100644
View file @
abf792c4
package
com
.
byit
.
enums
;
import
com.byit.serialization.ISerializer
;
import
com.byit.serialization.JsonSerializer
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.stream.Collectors
;
/**
* 序列化算法标识
* @author huangfu
*/
public
enum
SerializerAlgorithm
{
JSON
(
Byte
.
parseByte
(
"1"
),
new
JsonSerializer
(),
"FastJson序列化方式"
)
;
private
byte
serializerAlgorithm
;
private
ISerializer
iSerializer
;
private
String
serializerMsg
;
SerializerAlgorithm
(
byte
serializerAlgorithm
,
ISerializer
iSerializer
,
String
serializerMsg
)
{
this
.
serializerAlgorithm
=
serializerAlgorithm
;
this
.
iSerializer
=
iSerializer
;
this
.
serializerMsg
=
serializerMsg
;
}
public
byte
getSerializerAlgorithm
()
{
return
serializerAlgorithm
;
}
public
ISerializer
getImSerializer
()
{
return
iSerializer
;
}
public
String
getSerializerMsg
()
{
return
serializerMsg
;
}
/**
* 根据序列胡算法匹配序列化对象
* @param code 序列化算法字节
* @return 序列化算法对象
*/
public
static
ISerializer
match
(
byte
code
){
for
(
SerializerAlgorithm
serializerAlgorithm
:
SerializerAlgorithm
.
values
())
{
if
(
serializerAlgorithm
.
serializerAlgorithm
==
code
)
{
return
serializerAlgorithm
.
iSerializer
;
}
}
return
null
;
}
}
\ No newline at end of file
byit-plugin-core/byit-plugin-rpc-common/src/main/java/com/byit/handler/PackerSpliterHandler.java
0 → 100644
View file @
abf792c4
package
com
.
byit
.
handler
;
import
com.byit.utils.PacketCodecUtil
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.handler.codec.LengthFieldBasedFrameDecoder
;
/**
* 数据长度分离器,其实这个是防止socket的粘包和拆包的
* 第二个功能是做一个魔数非指定值的过滤即垃圾请求过滤
* @author huangfu
*/
public
class
PackerSpliterHandler
extends
LengthFieldBasedFrameDecoder
{
/**
* 协议头的长度
*/
private
final
static
Integer
PROTOCOL_LENGTH
=
7
;
/**
* 标识数据体长度的字节位数
*/
private
final
static
Integer
DATA_LENGTH
=
4
;
public
PackerSpliterHandler
()
{
super
(
Integer
.
MAX_VALUE
,
PROTOCOL_LENGTH
,
DATA_LENGTH
);
}
/**
* 解析数据 做连接拒绝策略
* @param ctx 上下文对象
* @param in 字节缓冲
* @return 解码后的对象
* @throws Exception 异常信息
*/
@Override
protected
Object
decode
(
ChannelHandlerContext
ctx
,
ByteBuf
in
)
throws
Exception
{
//从指定位置获取一个数据
if
(
in
.
getInt
(
in
.
readerIndex
())
!=
PacketCodecUtil
.
MAGIC_NUMBER
){
ctx
.
channel
().
closeFuture
().
sync
();
return
null
;
}
return
super
.
decode
(
ctx
,
in
);
}
}
byit-plugin-core/byit-plugin-rpc-common/src/main/java/com/byit/handler/PacketDecodeHandler.java
0 → 100644
View file @
abf792c4
package
com
.
byit
.
handler
;
import
com.byit.packet.BasePacketModel
;
import
com.byit.utils.PacketCodecUtil
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.handler.codec.ByteToMessageDecoder
;
import
java.util.List
;
/**
* 解码器处理
* @author huangfu
*/
public
class
PacketDecodeHandler
extends
ByteToMessageDecoder
{
@Override
protected
void
decode
(
ChannelHandlerContext
ctx
,
ByteBuf
in
,
List
<
Object
>
out
)
throws
Exception
{
BasePacketModel
basePacketModel
=
PacketCodecUtil
.
decode
(
in
);
out
.
add
(
basePacketModel
);
}
}
byit-plugin-core/byit-plugin-rpc-common/src/main/java/com/byit/handler/PacketEncodeHandler.java
0 → 100644
View file @
abf792c4
package
com
.
byit
.
handler
;
import
com.byit.packet.BasePacketModel
;
import
com.byit.utils.PacketCodecUtil
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.handler.codec.MessageToByteEncoder
;
/**
* 数据包编码处理
* @author huangfu
*/
public
class
PacketEncodeHandler
extends
MessageToByteEncoder
<
BasePacketModel
>
{
@Override
protected
void
encode
(
ChannelHandlerContext
ctx
,
BasePacketModel
msg
,
ByteBuf
out
)
throws
Exception
{
PacketCodecUtil
.
encode
(
out
,
msg
);
}
}
byit-plugin-core/byit-plugin-rpc-common/src/main/java/com/byit/packet/BasePacketModel.java
0 → 100644
View file @
abf792c4
package
com
.
byit
.
packet
;
import
com.byit.enums.Command
;
import
com.byit.enums.SerializerAlgorithm
;
import
java.io.Serializable
;
/**
* 数据包基类
* @author huangfu
*/
public
abstract
class
BasePacketModel
implements
Serializable
{
/**
* 协议版本
*/
private
Byte
version
=
1
;
/**
* 指令
* @return 指令编码
*/
public
abstract
Command
getCommand
();
/**
* 返回序列化方式
* @return 序列化枚举
*/
public
abstract
SerializerAlgorithm
getSerializerAlgorithm
();
public
Byte
getVersion
()
{
return
version
;
}
}
byit-plugin-core/byit-plugin-rpc-common/src/main/java/com/byit/packet/request/PluginRpcRequestPacket.java
0 → 100644
View file @
abf792c4
package
com
.
byit
.
packet
.
request
;
import
com.byit.enums.Command
;
import
com.byit.enums.SerializerAlgorithm
;
import
com.byit.packet.BasePacketModel
;
import
lombok.Data
;
import
lombok.EqualsAndHashCode
;
import
java.util.Map
;
/**
* 插件服务端的请求数据包
* @author huangfu
*/
@EqualsAndHashCode
(
callSuper
=
true
)
@Data
public
class
PluginRpcRequestPacket
extends
BasePacketModel
{
/**
* 任务名称
*/
private
String
jobName
;
/**
* 任务参数
*/
private
Map
<
String
,
Object
>
param
;
@Override
public
Command
getCommand
()
{
return
Command
.
RUN_REMOTELY_JOB_NODE_REQUEST
;
}
@Override
public
SerializerAlgorithm
getSerializerAlgorithm
()
{
return
SerializerAlgorithm
.
JSON
;
}
}
byit-plugin-core/byit-plugin-rpc-common/src/main/java/com/byit/packet/response/PluginRpcResponsePacket.java
0 → 100644
View file @
abf792c4
package
com
.
byit
.
packet
.
response
;
import
com.byit.enums.Command
;
import
com.byit.enums.SerializerAlgorithm
;
import
com.byit.packet.BasePacketModel
;
import
lombok.Data
;
import
lombok.EqualsAndHashCode
;
/**
* 插件客户响应数据载体
* @author huangfu
*/
@Data
@EqualsAndHashCode
(
callSuper
=
true
)
public
class
PluginRpcResponsePacket
extends
BasePacketModel
{
private
String
code
;
private
String
msg
;
private
Object
result
;
private
boolean
status
=
false
;
@Override
public
Command
getCommand
()
{
return
Command
.
RUN_REMOTELY_JOB_NODE_RESPONSE
;
}
@Override
public
SerializerAlgorithm
getSerializerAlgorithm
()
{
return
SerializerAlgorithm
.
JSON
;
}
}
byit-plugin-core/byit-plugin-rpc-common/src/main/java/com/byit/param/PluginCallback.java
0 → 100644
View file @
abf792c4
package
com
.
byit
.
param
;
/**
* 插件端的回调接口
* @author huangfu
*/
public
interface
PluginCallback
{
/**
* 回调方法
* @throws Exception 异常
*/
void
run
()
throws
Exception
;
}
byit-plugin-core/byit-plugin-rpc-common/src/main/java/com/byit/registry/DataSourceServiceRegistry.java
0 → 100644
View file @
abf792c4
package
com
.
byit
.
registry
;
import
com.byit.registry.client.RegistryClient
;
import
com.byit.registry.client.model.RegistryDataParamVO
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.TreeSet
;
/**
* 数据库实现的注册中心
* @author huangfu
*/
public
class
DataSourceServiceRegistry
extends
PluginServiceRegistry
{
private
RegistryClient
registryClient
;
@Override
public
void
init
(
String
biz
,
String
env
,
String
registryUrl
)
{
registryClient
=
new
RegistryClient
(
registryUrl
,
null
,
biz
,
env
);
}
@Override
public
void
stop
()
{
if
(
registryClient
!=
null
)
{
registryClient
.
stop
();
}
}
@Override
public
boolean
registry
(
String
key
,
String
value
)
{
if
(
registryClient
==
null
)
{
throw
new
RuntimeException
(
"注册中心未初始化"
);
}
List
<
RegistryDataParamVO
>
registryDataList
=
new
ArrayList
<>();
registryDataList
.
add
(
new
RegistryDataParamVO
(
key
,
value
));
return
registryClient
.
registry
(
registryDataList
);
}
@Override
public
boolean
remove
(
List
<
RegistryDataParamVO
>
registryDataList
)
{
if
(
registryClient
!=
null
){
return
registryClient
.
remove
(
registryDataList
);
}
return
true
;
}
@Override
public
TreeSet
<
String
>
discovery
(
String
key
)
{
if
(
registryClient
!=
null
){
return
registryClient
.
discovery
(
key
);
}
return
null
;
}
public
RegistryClient
getRegistryClient
()
{
return
registryClient
;
}
public
void
setRegistryClient
(
RegistryClient
registryClient
)
{
this
.
registryClient
=
registryClient
;
}
}
byit-plugin-core/byit-plugin-rpc-common/src/main/java/com/byit/registry/PluginServiceRegistry.java
0 → 100644
View file @
abf792c4
package
com
.
byit
.
registry
;
import
com.byit.registry.client.model.RegistryDataParamVO
;
import
java.util.List
;
import
java.util.TreeSet
;
/**
* 插件端的注册策略
* @author huangfu
*/
public
abstract
class
PluginServiceRegistry
{
/**
* 开始注册的方法
* @param biz 业务key
* @param env 环境标识
* @param registryUrl 注册中心的地址
*/
public
abstract
void
init
(
String
biz
,
String
env
,
String
registryUrl
);
/**
* 停止注册
*/
public
abstract
void
stop
();
/**
* 注册地址
* @param key 业务标识
* @param value ip:port
* @return return
*/
public
abstract
boolean
registry
(
String
key
,
String
value
);
/**
* 删除这个服务
* @param registryDataList registryDataList
* @return 返
*/
public
abstract
boolean
remove
(
List
<
RegistryDataParamVO
>
registryDataList
);
/**
* 服务发现
* @param key 需要发现的key
* @return 返回服务器的地址
*/
public
abstract
TreeSet
<
String
>
discovery
(
String
key
);
}
byit-plugin-core/byit-plugin-rpc-common/src/main/java/com/byit/serialization/ISerializer.java
0 → 100644
View file @
abf792c4
package
com
.
byit
.
serialization
;
/**
* 序列化接口
* @author huangfu
*/
public
interface
ISerializer
{
/**
* 序列化算法
* @return 返回对应的序列化方式代表的字节
*/
byte
getSerializerAlgorithm
();
/**
* java 对象转换成二进制
* @param object
* @return 返回对象转的字节数组
*/
byte
[]
serialize
(
Object
object
);
/**
* 二进制转换成 java 对象
* @param clazz 转换的对象类型
* @param bytes 需要转换的数字
* @param <T> 返回的类型
* @return 返回字节转换的对象
*/
<
T
>
T
deserialize
(
Class
<
T
>
clazz
,
byte
[]
bytes
);
}
byit-plugin-core/byit-plugin-rpc-common/src/main/java/com/byit/serialization/JsonSerializer.java
0 → 100644
View file @
abf792c4
package
com
.
byit
.
serialization
;
import
com.alibaba.fastjson.JSON
;
import
com.byit.enums.SerializerAlgorithm
;
/**
* json方式的序列化数组
* @author huangfu
*/
public
class
JsonSerializer
implements
ISerializer
{
@Override
public
byte
getSerializerAlgorithm
()
{
return
SerializerAlgorithm
.
JSON
.
getSerializerAlgorithm
();
}
@Override
public
byte
[]
serialize
(
Object
object
)
{
return
JSON
.
toJSONBytes
(
object
);
}
@Override
public
<
T
>
T
deserialize
(
Class
<
T
>
clazz
,
byte
[]
bytes
)
{
return
JSON
.
parseObject
(
bytes
,
clazz
);
}
}
byit-plugin-core/byit-plugin-rpc-common/src/main/java/com/byit/utils/IpUtil.java
0 → 100644
View file @
abf792c4
package
com
.
byit
.
utils
;
import
java.io.IOException
;
import
java.net.Inet6Address
;
import
java.net.InetAddress
;
import
java.net.NetworkInterface
;
import
java.net.UnknownHostException
;
import
java.util.Enumeration
;
import
java.util.regex.Pattern
;
/**
* ip tool
*
*/
public
class
IpUtil
{
private
static
final
String
ANYHOST
=
"0.0.0.0"
;
private
static
final
String
LOCALHOST
=
"127.0.0.1"
;
private
static
final
Pattern
IP_PATTERN
=
Pattern
.
compile
(
"\\d{1,3}(\\.\\d{1,3}){3,5}$"
);
private
static
volatile
InetAddress
LOCAL_ADDRESS
=
null
;
// ---------------------- valid ----------------------
/**
* valid Inet4Address
*
* @param address
* @return
*/
private
static
boolean
isValidAddress
(
InetAddress
address
)
{
if
(
address
==
null
||
address
.
isLoopbackAddress
())
{
return
false
;
}
String
name
=
address
.
getHostAddress
();
return
(
name
!=
null
&&
!
ANYHOST
.
equals
(
name
)
&&
!
LOCALHOST
.
equals
(
name
)
&&
IP_PATTERN
.
matcher
(
name
).
matches
());
}
/**
* valid Inet6Address, if an ipv6 address is reachable.
*
* @param address
* @return
*/
private
static
boolean
isValidV6Address
(
Inet6Address
address
)
{
boolean
preferIpv6
=
Boolean
.
getBoolean
(
"java.net.preferIPv6Addresses"
);
if
(!
preferIpv6
)
{
return
false
;
}
try
{
return
address
.
isReachable
(
100
);
}
catch
(
IOException
e
)
{
// ignore
}
return
false
;
}
/**
* normalize the ipv6 Address, convert scope name to scope id.
*
* e.g.
* convert
* fe80:0:0:0:894:aeec:f37d:23e1%en0
* to
* fe80:0:0:0:894:aeec:f37d:23e1%5
*
* The %5 after ipv6 address is called scope id.
* see java doc of {@link Inet6Address} for more details.
*
* @param address the input address
* @return the normalized address, with scope id converted to int
*/
private
static
InetAddress
normalizeV6Address
(
Inet6Address
address
)
{
String
addr
=
address
.
getHostAddress
();
int
i
=
addr
.
lastIndexOf
(
'%'
);
if
(
i
>
0
)
{
try
{
return
InetAddress
.
getByName
(
addr
.
substring
(
0
,
i
)
+
'%'
+
address
.
getScopeId
());
}
catch
(
UnknownHostException
e
)
{
e
.
printStackTrace
();
}
}
return
address
;
}
// ---------------------- find ip ----------------------
private
static
InetAddress
getLocalAddress0
()
{
InetAddress
localAddress
=
null
;
try
{
localAddress
=
InetAddress
.
getLocalHost
();
if
(
localAddress
instanceof
Inet6Address
)
{
Inet6Address
address
=
(
Inet6Address
)
localAddress
;
if
(
isValidV6Address
(
address
)){
return
normalizeV6Address
(
address
);
}
}
else
if
(
isValidAddress
(
localAddress
))
{
return
localAddress
;
}
}
catch
(
Throwable
e
)
{
e
.
printStackTrace
();
}
try
{
Enumeration
<
NetworkInterface
>
interfaces
=
NetworkInterface
.
getNetworkInterfaces
();
if
(
null
==
interfaces
)
{
return
localAddress
;
}
while
(
interfaces
.
hasMoreElements
())
{
try
{
NetworkInterface
network
=
interfaces
.
nextElement
();
Enumeration
<
InetAddress
>
addresses
=
network
.
getInetAddresses
();
while
(
addresses
.
hasMoreElements
())
{
try
{
InetAddress
address
=
addresses
.
nextElement
();
if
(
address
instanceof
Inet6Address
)
{
Inet6Address
v6Address
=
(
Inet6Address
)
address
;
if
(
isValidV6Address
(
v6Address
)){
return
normalizeV6Address
(
v6Address
);
}
}
else
if
(
isValidAddress
(
address
))
{
return
address
;
}
}
catch
(
Throwable
e
)
{
e
.
printStackTrace
();
}
}
}
catch
(
Throwable
e
)
{
e
.
printStackTrace
();
}
}
}
catch
(
Throwable
e
)
{
e
.
printStackTrace
();
}
return
localAddress
;
}
// ---------------------- tool ----------------------
/**
* Find first valid IP from local network card
*
* @return first valid local IP
*/
public
static
InetAddress
getLocalAddress
()
{
if
(
LOCAL_ADDRESS
!=
null
)
{
return
LOCAL_ADDRESS
;
}
InetAddress
localAddress
=
getLocalAddress0
();
LOCAL_ADDRESS
=
localAddress
;
return
localAddress
;
}
/**
* get ip address
*
* @return String
*/
public
static
String
getIp
(){
return
getLocalAddress
().
getHostAddress
();
}
/**
* get ip:port
*
* @param port
* @return String
*/
public
static
String
getIpPort
(
int
port
){
String
ip
=
getIp
();
return
getIpPort
(
ip
,
port
);
}
public
static
String
getIpPort
(
String
ip
,
int
port
){
if
(
ip
==
null
)
{
return
null
;
}
return
ip
.
concat
(
":"
).
concat
(
String
.
valueOf
(
port
));
}
public
static
Object
[]
parseIpPort
(
String
address
){
String
[]
array
=
address
.
split
(
":"
);
String
host
=
array
[
0
];
int
port
=
Integer
.
parseInt
(
array
[
1
]);
return
new
Object
[]{
host
,
port
};
}
}
byit-plugin-core/byit-plugin-rpc-common/src/main/java/com/byit/utils/PacketCodecUtil.java
0 → 100644
View file @
abf792c4
package
com
.
byit
.
utils
;
import
com.byit.enums.Command
;
import
com.byit.enums.SerializerAlgorithm
;
import
com.byit.packet.BasePacketModel
;
import
com.byit.serialization.ISerializer
;
import
io.netty.buffer.ByteBuf
;
/**
* 说明:
* 编码字符集会对对象进行编解码,其中包括,其中自定义协议规则如下:
* 4字节的魔数: 此值是为了过滤无差别垃圾连接
* 1字节的版本号: 此值暂时无用,只是为了区分后期的某些更改扩展字段预留
* 1字节的序列化算法标识:此值是为了标识该数据承载类所用的序列化方式
* 1字节的指令码: 此值是为了标识该链接对应的数据载体
* 4字节的(1整形): 此值是为了标识数据包的长度
* 无限长度的字节数组: 此值是数据体部分,即对象转换成字节后的数据
*
* 注意:
* 后期所有的编解码操作均由此类进行编解码操作,不通过该工具类进行编解码的,服务
* 在解析数据包时可能会出现严重错误!
*
*
* 协议 封包解包工具
* @author huangfu
*/
public
class
PacketCodecUtil
{
/**
* 定义魔数 4字节长度的整形数据
*/
public
static
final
int
MAGIC_NUMBER
=
0x12345678
;
/**
* 编码
* @param byteBuf 数据缓冲
* @param packet 数据包
*/
public
static
void
encode
(
ByteBuf
byteBuf
,
BasePacketModel
packet
){
//获取改数据包的序列化格式
ISerializer
imSerializer
=
packet
.
getSerializerAlgorithm
().
getImSerializer
();
byte
[]
serializeObjBuf
=
imSerializer
.
serialize
(
packet
);
// 3. 实际编码过程
//写入魔数 按照规定4字节
byteBuf
.
writeInt
(
MAGIC_NUMBER
);
//写入版本号 按照规定 一字节
byteBuf
.
writeByte
(
packet
.
getVersion
());
//写入序列化算法 按照规定一字节
byteBuf
.
writeByte
(
imSerializer
.
getSerializerAlgorithm
());
//写入指令 按照规定1字节
byteBuf
.
writeByte
(
packet
.
getCommand
().
getCommandCode
());
//写入字节长度,按照规定四字节
byteBuf
.
writeInt
(
serializeObjBuf
.
length
);
//写入最终数据
byteBuf
.
writeBytes
(
serializeObjBuf
);
}
/**
* ByteBuf在进行读取时会进行顺序读取,所以他的读取数据应该与写入顺序一致
* @param byteBuf 数据载体缓冲区
* @return 解码后的对象
*/
public
static
BasePacketModel
decode
(
ByteBuf
byteBuf
){
//跳过魔数 后续校验
byteBuf
.
skipBytes
(
4
);
//跳过 版本号
byteBuf
.
skipBytes
(
1
);
//获取编解码算法
byte
codecIdentification
=
byteBuf
.
readByte
();
//获取指令
byte
command
=
byteBuf
.
readByte
();
//获取数据包长度
int
dataLength
=
byteBuf
.
readInt
();
//数据字节
byte
[]
bytes
=
new
byte
[
dataLength
];
byteBuf
.
readBytes
(
bytes
);
//获取命令编码
Class
<?
extends
BasePacketModel
>
packet
=
Command
.
match
(
command
);
ISerializer
serializer
=
SerializerAlgorithm
.
match
(
codecIdentification
);
if
(
packet
!=
null
&&
serializer
!=
null
)
{
return
serializer
.
deserialize
(
packet
,
bytes
);
}
return
null
;
}
}
\ No newline at end of file
byit-plugin-core/myth-plugin-rpc-client/pom.xml
0 → 100644
View file @
abf792c4
<?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>
myth-plugin-rpc-client
</artifactId>
</project>
\ No newline at end of file
byit-plugin-core/myth-plugin-rpc-server/pom.xml
0 → 100644
View file @
abf792c4
<?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>
myth-plugin-rpc-server
</artifactId>
<dependencies>
<dependency>
<groupId>
myth-job
</groupId>
<artifactId>
byit-plugin-rpc-common
</artifactId>
<version>
1.0-SNAPSHOT
</version>
</dependency>
<dependency>
<groupId>
org.dom4j
</groupId>
<artifactId>
dom4j
</artifactId>
<version>
2.1.1
</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
byit-plugin-core/myth-plugin-rpc-server/src/main/java/com/byit/factory/PluginServerFactory.java
0 → 100644
View file @
abf792c4
package
com
.
byit
.
factory
;
import
cn.hutool.core.net.NetUtil
;
import
com.byit.param.PluginCallback
;
import
com.byit.registry.PluginServiceRegistry
;
import
com.byit.server.PluginServer
;
import
com.byit.utils.IpUtil
;
import
org.apache.commons.lang3.StringUtils
;
/**
* 插件服务端的工厂对象
* @author huangfu
*/
public
abstract
class
PluginServerFactory
{
private
final
static
String
SERVER_NAME
=
"plugin-rpc-server"
;
private
int
corePoolSize
;
private
int
maxPoolSize
;
private
String
registryUrl
;
private
String
env
;
private
String
ip
;
private
int
port
;
private
PluginCallback
runServerAfterCallback
;
private
String
serverName
;
private
Class
<?
extends
PluginServiceRegistry
>
serviceRegistryClass
;
private
Class
<?
extends
PluginServer
>
pluginServerClass
;
public
void
init
(
int
corePoolSize
,
int
maxPoolSize
,
String
registryUrl
,
String
env
,
int
port
,
PluginCallback
runServerAfterCallback
,
String
serverName
,
Class
<?
extends
PluginServiceRegistry
>
serviceRegistryClass
,
Class
<?
extends
PluginServer
>
pluginServerClass
){
if
(!(
corePoolSize
>
0
&&
maxPoolSize
>
0
&&
maxPoolSize
>=
corePoolSize
)){
this
.
corePoolSize
=
60
;
this
.
maxPoolSize
=
300
;
}
else
{
this
.
corePoolSize
=
corePoolSize
;
this
.
maxPoolSize
=
maxPoolSize
;
}
this
.
registryUrl
=
registryUrl
;
this
.
env
=
env
;
this
.
ip
=
IpUtil
.
getIp
();
if
(
port
<=
0
){
this
.
port
=
7060
;
}
else
{
this
.
port
=
port
;
}
if
(!
NetUtil
.
isUsableLocalPort
(
this
.
port
))
{
throw
new
RuntimeException
(
port
+
"端口被占用"
);
}
this
.
runServerAfterCallback
=
runServerAfterCallback
;
if
(
StringUtils
.
isBlank
(
serverName
)){
this
.
serverName
=
SERVER_NAME
;
}
else
{
this
.
serverName
=
serverName
;
}
this
.
serviceRegistryClass
=
serviceRegistryClass
;
this
.
pluginServerClass
=
pluginServerClass
;
if
(
this
.
serviceRegistryClass
==
null
){
throw
new
RuntimeException
(
"注册中心类型不能为空!"
);
}
if
(
this
.
pluginServerClass
==
null
){
throw
new
RuntimeException
(
"插件端使用的服务类型不能为空!"
);
}
}
public
void
start
()
throws
Exception
{
PluginServer
pluginServer
=
pluginServerClass
.
newInstance
();
}
public
int
getCorePoolSize
()
{
return
corePoolSize
;
}
public
void
setCorePoolSize
(
int
corePoolSize
)
{
this
.
corePoolSize
=
corePoolSize
;
}
public
int
getMaxPoolSize
()
{
return
maxPoolSize
;
}
public
void
setMaxPoolSize
(
int
maxPoolSize
)
{
this
.
maxPoolSize
=
maxPoolSize
;
}
public
String
getIp
()
{
return
ip
;
}
public
void
setIp
(
String
ip
)
{
this
.
ip
=
ip
;
}
public
int
getPort
()
{
return
port
;
}
public
void
setPort
(
int
port
)
{
this
.
port
=
port
;
}
public
PluginCallback
getRunServerAfterCallback
()
{
return
runServerAfterCallback
;
}
public
void
setRunServerAfterCallback
(
PluginCallback
runServerAfterCallback
)
{
this
.
runServerAfterCallback
=
runServerAfterCallback
;
}
public
String
getServerName
()
{
return
serverName
;
}
public
void
setServerName
(
String
serverName
)
{
this
.
serverName
=
serverName
;
}
public
Class
<?
extends
PluginServiceRegistry
>
getServiceRegistryClass
()
{
return
serviceRegistryClass
;
}
public
void
setServiceRegistryClass
(
Class
<?
extends
PluginServiceRegistry
>
serviceRegistryClass
)
{
this
.
serviceRegistryClass
=
serviceRegistryClass
;
}
public
Class
<?
extends
PluginServer
>
getPluginServerClass
()
{
return
pluginServerClass
;
}
public
void
setPluginServerClass
(
Class
<?
extends
PluginServer
>
pluginServerClass
)
{
this
.
pluginServerClass
=
pluginServerClass
;
}
public
String
getRegistryUrl
()
{
return
registryUrl
;
}
public
void
setRegistryUrl
(
String
registryUrl
)
{
this
.
registryUrl
=
registryUrl
;
}
public
String
getEnv
()
{
return
env
;
}
public
void
setEnv
(
String
env
)
{
this
.
env
=
env
;
}
}
byit-plugin-core/myth-plugin-rpc-server/src/main/java/com/byit/server/PluginServer.java
0 → 100644
View file @
abf792c4
package
com
.
byit
.
server
;
import
com.byit.param.PluginCallback
;
/**
* 插件端的服务器的基类
* @author huangfu
*/
public
abstract
class
PluginServer
{
private
PluginCallback
startPluginCallback
;
private
PluginCallback
stopPluginCallback
;
public
PluginCallback
getStartPluginCallback
()
{
return
startPluginCallback
;
}
public
void
setStartPluginCallback
(
PluginCallback
startPluginCallback
)
{
this
.
startPluginCallback
=
startPluginCallback
;
}
public
PluginCallback
getStopPluginCallback
()
{
return
stopPluginCallback
;
}
public
void
setStopPluginCallback
(
PluginCallback
stopPluginCallback
)
{
this
.
stopPluginCallback
=
stopPluginCallback
;
}
public
void
onStart
(){
if
(
startPluginCallback
!=
null
){
try
{
startPluginCallback
.
run
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
public
void
onStop
(){
if
(
stopPluginCallback
!=
null
){
if
(
stopPluginCallback
!=
null
){
try
{
stopPluginCallback
.
run
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
}
}
byit-plugin-core/pom.xml
0 → 100644
View file @
abf792c4
<?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-myth-job
</artifactId>
<groupId>
myth-job
</groupId>
<version>
1.0-SNAPSHOT
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<artifactId>
byit-plugin-core
</artifactId>
<packaging>
pom
</packaging>
<modules>
<module>
myth-plugin-rpc-server
</module>
<module>
myth-plugin-rpc-client
</module>
<module>
byit-plugin-rpc-common
</module>
</modules>
</project>
\ No newline at end of file
pom.xml
View file @
abf792c4
...
...
@@ -16,6 +16,7 @@
<module>
byit-myth-rpc
</module>
<module>
byit-mybatis-plugin
</module>
<module>
byit-myth-gateway
</module>
<module>
byit-plugin-core
</module>
</modules>
<parent>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment