Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
X
xxl-job
Project
Project
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
刘晓滨
xxl-job
Commits
1ba55fd9
Commit
1ba55fd9
authored
Nov 04, 2021
by
陈红帅
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf(admin):读取zookeeper中的配置信息
parent
98036098
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
201 additions
and
4 deletions
+201
-4
pom.xml
xxl-job-admin/pom.xml
+7
-0
XxlJobAdminApplication.java
...c/main/java/com/xxl/job/admin/XxlJobAdminApplication.java
+39
-1
ZookeeperClientUtil.java
...main/java/com/xxl/job/admin/util/ZookeeperClientUtil.java
+149
-0
application.properties
xxl-job-admin/src/main/resources/application.properties
+6
-3
No files found.
xxl-job-admin/pom.xml
View file @
1ba55fd9
...
@@ -115,6 +115,13 @@
...
@@ -115,6 +115,13 @@
<version>
${project.parent.version}
</version>
<version>
${project.parent.version}
</version>
</dependency>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>
org.apache.zookeeper
</groupId>
<artifactId>
zookeeper
</artifactId>
<version>
3.4.6
</version>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
...
...
xxl-job-admin/src/main/java/com/xxl/job/admin/XxlJobAdminApplication.java
View file @
1ba55fd9
package
com
.
xxl
.
job
.
admin
;
package
com
.
xxl
.
job
.
admin
;
import
com.xxl.job.admin.util.ZookeeperClientUtil
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.util.StringUtils
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.util.Properties
;
/**
/**
* @author xuxueli 2018-10-28 00:38:13
* @author xuxueli 2018-10-28 00:38:13
...
@@ -9,8 +15,39 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
...
@@ -9,8 +15,39 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@SpringBootApplication
public
class
XxlJobAdminApplication
{
public
class
XxlJobAdminApplication
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
){
init
(
args
);
SpringApplication
.
run
(
XxlJobAdminApplication
.
class
,
args
);
SpringApplication
.
run
(
XxlJobAdminApplication
.
class
,
args
);
}
}
private
static
void
init
(
String
[]
args
){
// 促使化zookeeper客户端工具类
if
(
args
==
null
||
args
.
length
==
0
){
throw
new
RuntimeException
(
"请传入zookeeper的地址"
);
}
ZookeeperClientUtil
.
init
(
args
[
0
],
"/weixt/conf"
);
// 创建zookeeper客户端
ZookeeperClientUtil
configurationClient
=
new
ZookeeperClientUtil
();
try
{
String
resource
=
XxlJobAdminApplication
.
class
.
getResource
(
"/application.properties"
).
getPath
();
Properties
properties
=
new
Properties
();
properties
.
load
(
new
FileInputStream
(
resource
));
makeProperties
(
properties
,
configurationClient
,
"spring.datasource.url"
);
makeProperties
(
properties
,
configurationClient
,
"spring.datasource.username"
);
makeProperties
(
properties
,
configurationClient
,
"spring.datasource.password"
);
FileOutputStream
fileOutputStream
=
new
FileOutputStream
(
resource
);
properties
.
store
(
fileOutputStream
,
""
);
fileOutputStream
.
close
();
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
}
private
static
void
makeProperties
(
Properties
properties
,
ZookeeperClientUtil
configurationClient
,
String
key
){
String
value
=
configurationClient
.
getData
(
configurationClient
.
mainPath
+
"/"
+
key
);
if
(
StringUtils
.
isEmpty
(
value
)){
throw
new
RuntimeException
(
"zookeeper中没有该属性的值:"
+
key
);
}
properties
.
setProperty
(
key
,
value
);
}
}
}
\ No newline at end of file
xxl-job-admin/src/main/java/com/xxl/job/admin/util/ZookeeperClientUtil.java
0 → 100644
View file @
1ba55fd9
package
com
.
xxl
.
job
.
admin
.
util
;
import
org.apache.zookeeper.CreateMode
;
import
org.apache.zookeeper.KeeperException
;
import
org.apache.zookeeper.ZooDefs.Ids
;
import
org.apache.zookeeper.ZooDefs.Perms
;
import
org.apache.zookeeper.ZooKeeper
;
import
org.apache.zookeeper.data.ACL
;
import
org.apache.zookeeper.data.Stat
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.StringUtils
;
import
java.io.IOException
;
import
java.util.Collections
;
import
java.util.List
;
//@Component
public
class
ZookeeperClientUtil
{
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
ZookeeperClientUtil
.
class
);
private
ZooKeeper
zk
;
// zookeeper地址
private
static
String
servers
;
// 链接超时时间
private
int
sessionTimeout
=
60000
;
public
static
String
mainPath
;
public
static
void
init
(
String
serversAddress
,
String
mainPathStr
)
{
servers
=
serversAddress
;
mainPath
=
mainPathStr
;
}
public
ZooKeeper
getAliveZk
()
{
ZooKeeper
aliveZk
=
zk
;
if
(
aliveZk
!=
null
&&
aliveZk
.
getState
().
isAlive
())
{
return
aliveZk
;
}
else
{
zkReconnect
();
return
zk
;
}
}
public
synchronized
void
zkReconnect
()
{
close
();
try
{
connect
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
public
synchronized
void
close
()
{
if
(
zk
!=
null
)
{
try
{
zk
.
close
();
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
zk
=
null
;
}
}
public
void
mkPaths
(
String
path
)
throws
KeeperException
,
InterruptedException
{
String
[]
subs
=
path
.
split
(
"\\/"
);
if
(
subs
.
length
<
2
)
{
return
;
}
String
curPath
=
""
;
for
(
int
i
=
1
;
i
<
subs
.
length
;
i
++)
{
curPath
=
curPath
+
"/"
+
subs
[
i
];
Stat
stat
=
getAliveZk
().
exists
(
curPath
,
false
);
if
(
stat
==
null
)
{
getAliveZk
().
create
(
path
,
null
,
Ids
.
OPEN_ACL_UNSAFE
,
CreateMode
.
PERSISTENT
);
}
}
}
private
synchronized
void
connect
()
throws
IOException
{
if
(
zk
==
null
&&
!
StringUtils
.
isEmpty
(
servers
))
zk
=
new
ZooKeeper
(
servers
,
sessionTimeout
,
null
);
}
public
String
getData
(
String
path
)
{
String
result
=
null
;
try
{
byte
[]
data
=
getAliveZk
().
getData
(
path
,
Boolean
.
TRUE
,
null
);
if
(
null
!=
data
)
{
result
=
new
String
(
data
,
"UTF-8"
);
}
}
catch
(
KeeperException
e
)
{
logger
.
error
(
"获取节点发送错误,path="
+
path
+
" errmsg="
+
e
.
getMessage
());
}
catch
(
InterruptedException
e
)
{
logger
.
error
(
"获取节点发送错误,path="
+
path
+
" errmsg="
+
e
.
getMessage
());
}
catch
(
Exception
e
)
{
logger
.
error
(
"获取子节点发送错误,mainPath="
+
mainPath
+
" errmsg="
+
e
.
getMessage
());
}
return
result
;
}
public
void
setData
(
String
path
,
String
dataStr
)
{
try
{
Stat
stat
=
getAliveZk
().
exists
(
path
,
false
);
if
(
stat
==
null
)
{
getAliveZk
().
create
(
path
,
null
,
Collections
.
singletonList
(
new
ACL
(
Perms
.
ALL
,
Ids
.
ANYONE_ID_UNSAFE
)),
CreateMode
.
PERSISTENT
);
}
getAliveZk
().
setData
(
path
,
dataStr
.
getBytes
(),
-
1
);
}
catch
(
KeeperException
e
)
{
logger
.
error
(
"获取节点发送错误,path="
+
path
,
e
);
}
catch
(
InterruptedException
e
)
{
logger
.
error
(
"获取节点发送错误,path="
+
path
,
e
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"获取子节点发送错误,mainPath="
+
mainPath
,
e
);
}
}
public
List
<
String
>
getChildren
()
{
List
<
String
>
data
=
null
;
try
{
data
=
getAliveZk
().
getChildren
(
mainPath
,
Boolean
.
FALSE
);
}
catch
(
KeeperException
e
)
{
logger
.
error
(
"获取子节点发送错误,mainPath="
+
mainPath
,
e
);
}
catch
(
InterruptedException
e
)
{
logger
.
error
(
"获取子节点发送错误,mainPath="
+
mainPath
,
e
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"获取子节点发送错误,mainPath="
+
mainPath
,
e
);
}
return
data
;
}
public
void
setSessionTimeout
(
int
sessionTimeout
)
{
this
.
sessionTimeout
=
sessionTimeout
;
}
// public String getMainPath() {
// return mainPath;
// }
// public void setMainPath(String mainPath) {
// this.mainPath = mainPath;
// }
// public static void setServers(String servers) {
// this.servers = servers;
// }
}
xxl-job-admin/src/main/resources/application.properties
View file @
1ba55fd9
...
@@ -17,9 +17,10 @@ spring.freemarker.settings.number_format=0.##########
...
@@ -17,9 +17,10 @@ spring.freemarker.settings.number_format=0.##########
mybatis.mapper-locations
=
classpath:/mybatis-mapper/*Mapper.xml
mybatis.mapper-locations
=
classpath:/mybatis-mapper/*Mapper.xml
### xxl-job, datasource
### xxl-job, datasource
spring.datasource.url
=
jdbc:mysql://135.202.16.39:3306/xxl-job?useUnicode=true&characterEncoding=UTF-8
#spring.datasource.url=jdbc:mysql://135.202.16.39:3306/xxl-job?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username
=
root
#spring.datasource.url=jdbc:mysql://10.10.10.192:3306/xxl-job-new?useUnicode=true&characterEncoding=UTF-8
spring.datasource.password
=
Space1234
#spring.datasource.username=root
#spring.datasource.password=Space1234
spring.datasource.driver-class-name
=
com.mysql.jdbc.Driver
spring.datasource.driver-class-name
=
com.mysql.jdbc.Driver
spring.datasource.type
=
org.apache.tomcat.jdbc.pool.DataSource
spring.datasource.type
=
org.apache.tomcat.jdbc.pool.DataSource
...
@@ -44,3 +45,5 @@ xxl.job.accessToken=
...
@@ -44,3 +45,5 @@ xxl.job.accessToken=
### xxl-job, i18n (default empty as chinese, "en" as english)
### xxl-job, i18n (default empty as chinese, "en" as english)
xxl.job.i18n
=
xxl.job.i18n
=
###
abc.zookeeper.mainpath
=
/weixt/conf
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