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
5524c8b3
Commit
5524c8b3
authored
Apr 03, 2020
by
huangfusuper
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Plugin插件服务端 服务工厂数据属性完善,支持多任务对一个1ip方式注册
parent
dfacf668
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
133 additions
and
32 deletions
+133
-32
PluginServerFactory.java
...r/src/main/java/com/byit/factory/PluginServerFactory.java
+133
-32
No files found.
byit-plugin-core/myth-plugin-rpc-server/src/main/java/com/byit/factory/PluginServerFactory.java
View file @
5524c8b3
package
com
.
byit
.
factory
;
package
com
.
byit
.
factory
;
import
cn.hutool.core.collection.CollectionUtil
;
import
cn.hutool.core.net.NetUtil
;
import
cn.hutool.core.net.NetUtil
;
import
com.byit.param.PluginCallback
;
import
com.byit.model.ServerConfigurationModel
;
import
com.byit.model.ServiceConfigModel
;
import
com.byit.registry.DataSourceServiceRegistry
;
import
com.byit.registry.PluginServiceRegistry
;
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.server.PluginServer
;
import
com.byit.utils.IpUtil
;
import
com.byit.utils.IpUtil
;
import
com.byit.utils.XmlParseUtil
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
/**
/**
* 插件服务端的工厂对象
* 插件服务端的工厂对象
* @author huangfu
* @author huangfu
*/
*/
public
abstract
class
PluginServerFactory
{
public
abstract
class
PluginServerFactory
{
private
final
static
String
SERVER_NAME
=
"plugin-rpc-server"
;
private
final
static
String
DEFAULT_BIZ_NAME
=
"plugin-rpc-server"
;
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
int
corePoolSize
;
private
int
corePoolSize
;
private
int
maxPoolSize
;
private
int
maxPoolSize
;
private
String
registryUrl
;
private
String
registryUrl
;
/**
* 环境标识
*/
private
String
env
;
private
String
env
;
/**
* 工程名称,相同的额工程里的模块才会相互通讯
*/
private
String
biz
;
private
String
ip
;
private
String
ip
;
private
int
port
;
private
int
port
;
private
PluginCallback
runServerAfterCallback
;
private
String
serverName
;
private
Class
<?
extends
PluginServiceRegistry
>
serviceRegistryClass
;
private
Class
<?
extends
PluginServiceRegistry
>
serviceRegistryClass
;
private
Class
<?
extends
PluginServer
>
pluginServerClass
;
private
Class
<?
extends
PluginServer
>
pluginServerClass
;
private
PluginServer
pluginServer
;
private
PluginServiceRegistry
pluginServiceRegistry
;
private
Set
<
String
>
serverKeys
;
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
();
}
public
void
init
(
int
corePoolSize
,
int
maxPoolSize
,
String
registryUrl
,
String
env
,
int
port
,
Map
<
String
,
String
>
classNames
=
serviceConfigModel
.
getClassNames
();
PluginCallback
runServerAfterCallback
,
String
serverName
,
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
,
Class
<?
extends
PluginServiceRegistry
>
serviceRegistryClass
,
Class
<?
extends
PluginServiceRegistry
>
serviceRegistryClass
,
Class
<?
extends
PluginServer
>
pluginServerClass
){
Class
<?
extends
PluginServer
>
pluginServerClass
,
Set
<
String
>
serverKeys
){
if
(!(
corePoolSize
>
0
&&
maxPoolSize
>
0
&&
maxPoolSize
>=
corePoolSize
)){
if
(!(
corePoolSize
>
0
&&
maxPoolSize
>
0
&&
maxPoolSize
>=
corePoolSize
)){
this
.
corePoolSize
=
60
;
this
.
corePoolSize
=
60
;
...
@@ -37,7 +85,12 @@ public abstract class PluginServerFactory {
...
@@ -37,7 +85,12 @@ public abstract class PluginServerFactory {
this
.
maxPoolSize
=
maxPoolSize
;
this
.
maxPoolSize
=
maxPoolSize
;
}
}
this
.
registryUrl
=
registryUrl
;
this
.
registryUrl
=
registryUrl
;
this
.
env
=
env
;
if
(
StringUtils
.
isNotBlank
(
env
)){
this
.
env
=
env
;
}
else
{
this
.
env
=
DEFAULT_ENV_NAME
;
}
this
.
ip
=
IpUtil
.
getIp
();
this
.
ip
=
IpUtil
.
getIp
();
if
(
port
<=
0
){
if
(
port
<=
0
){
...
@@ -49,16 +102,25 @@ public abstract class PluginServerFactory {
...
@@ -49,16 +102,25 @@ public abstract class PluginServerFactory {
throw
new
RuntimeException
(
port
+
"端口被占用"
);
throw
new
RuntimeException
(
port
+
"端口被占用"
);
}
}
this
.
runServerAfterCallback
=
runServerAfterCallback
;
if
(
StringUtils
.
isBlank
(
biz
)){
this
.
biz
=
DEFAULT_BIZ_NAME
;
}
else
{
this
.
biz
=
biz
;
}
if
(
StringUtils
.
isBlank
(
serverName
))
{
if
(
serviceRegistryClass
!=
null
)
{
this
.
serv
erName
=
SERVER_NAME
;
this
.
serv
iceRegistryClass
=
serviceRegistryClass
;
}
else
{
}
else
{
this
.
serv
erName
=
serverName
;
this
.
serv
iceRegistryClass
=
DEFAULT_SERVICE_REGISTRY_CLASS
;
}
}
this
.
serviceRegistryClass
=
serviceRegistryClass
;
if
(
pluginServerClass
!=
null
)
{
this
.
pluginServerClass
=
pluginServerClass
;
this
.
pluginServerClass
=
pluginServerClass
;
}
else
{
this
.
pluginServerClass
=
DEFAULT_NETTY_PLUGIN_SERVER_CLASS
;
}
if
(
this
.
serviceRegistryClass
==
null
){
if
(
this
.
serviceRegistryClass
==
null
){
throw
new
RuntimeException
(
"注册中心类型不能为空!"
);
throw
new
RuntimeException
(
"注册中心类型不能为空!"
);
...
@@ -67,11 +129,33 @@ public abstract class PluginServerFactory {
...
@@ -67,11 +129,33 @@ public abstract class PluginServerFactory {
if
(
this
.
pluginServerClass
==
null
){
if
(
this
.
pluginServerClass
==
null
){
throw
new
RuntimeException
(
"插件端使用的服务类型不能为空!"
);
throw
new
RuntimeException
(
"插件端使用的服务类型不能为空!"
);
}
}
this
.
serverKeys
=
serverKeys
;
}
}
public
void
start
()
throws
Exception
{
public
void
start
()
throws
Exception
{
PluginServer
pluginServer
=
pluginServerClass
.
newInstance
();
pluginServer
=
pluginServerClass
.
newInstance
();
pluginServiceRegistry
=
serviceRegistryClass
.
newInstance
();
//放置启动回调 启动成功后会调用注册服务的方法
pluginServer
.
setStartPluginCallback
(()->{
//数据初始化
pluginServiceRegistry
.
init
(
biz
,
env
,
registryUrl
);
//服务注册
serverKeys
.
forEach
(
serverKey
->{
registryDataParamVOs
.
add
(
new
RegistryDataParamVO
(
serverKey
,
ip
+
":"
+
port
));
});
pluginServiceRegistry
.
registry
(
registryDataParamVOs
);
});
//设置停止回调
pluginServer
.
setStopPluginCallback
(()
->{
if
(
CollectionUtil
.
isNotEmpty
(
registryDataParamVOs
)){
pluginServiceRegistry
.
remove
(
registryDataParamVOs
);
}
pluginServiceRegistry
.
stop
();
pluginServiceRegistry
=
null
;
});
//启动服务器
pluginServer
.
start
(
this
);
}
}
...
@@ -107,22 +191,6 @@ public abstract class PluginServerFactory {
...
@@ -107,22 +191,6 @@ public abstract class PluginServerFactory {
this
.
port
=
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
()
{
public
Class
<?
extends
PluginServiceRegistry
>
getServiceRegistryClass
()
{
return
serviceRegistryClass
;
return
serviceRegistryClass
;
}
}
...
@@ -154,4 +222,37 @@ public abstract class PluginServerFactory {
...
@@ -154,4 +222,37 @@ public abstract class PluginServerFactory {
public
void
setEnv
(
String
env
)
{
public
void
setEnv
(
String
env
)
{
this
.
env
=
env
;
this
.
env
=
env
;
}
}
public
PluginServer
getPluginServer
()
{
return
pluginServer
;
}
public
void
setPluginServer
(
PluginServer
pluginServer
)
{
this
.
pluginServer
=
pluginServer
;
}
public
PluginServiceRegistry
getPluginServiceRegistry
()
{
return
pluginServiceRegistry
;
}
public
void
setPluginServiceRegistry
(
PluginServiceRegistry
pluginServiceRegistry
)
{
this
.
pluginServiceRegistry
=
pluginServiceRegistry
;
}
public
String
getBiz
()
{
return
biz
;
}
public
void
setBiz
(
String
biz
)
{
this
.
biz
=
biz
;
}
public
Set
<
String
>
getServerKeys
()
{
return
serverKeys
;
}
public
void
setServerKeys
(
Set
<
String
>
serverKeys
)
{
this
.
serverKeys
=
serverKeys
;
}
}
}
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