Skip to content

Commit 20b3535

Browse files
committed
Merge pull request #69 from qq254963746/develop
在 LTS-Admin中增加主机名
2 parents a2392a1 + 6e85518 commit 20b3535

File tree

9 files changed

+47
-19
lines changed

9 files changed

+47
-19
lines changed

lts-admin/src/main/java/com/lts/web/support/memorydb/NodeMemoryDB.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public class NodeMemoryDB extends MemoryDB {
3030
" port, " +
3131
" nodeGroup, " +
3232
" createTime, " +
33-
" threads)" +
34-
" VALUES (?,?,?,?,?,?,?,?,?)";
33+
" threads," +
34+
" hostName)" +
35+
" VALUES (?,?,?,?,?,?,?,?,?,?)";
3536

3637
private String deleteSQL = "DELETE FROM lts_node where identity = ?";
3738

@@ -50,6 +51,7 @@ private void createTable() {
5051
" nodeGroup varchar(64) ," +
5152
" createTime bigint ," +
5253
" threads int ," +
54+
" hostName varchar(64) ," +
5355
" PRIMARY KEY (identity))";
5456

5557
try {
@@ -78,7 +80,8 @@ public void addNode(List<Node> nodes) {
7880
node.getPort(),
7981
node.getGroup(),
8082
node.getCreateTime(),
81-
node.getThreads()
83+
node.getThreads(),
84+
node.getHostName()
8285
);
8386
} catch (Exception e) {
8487
LOGGER.error("Insert {} error!", node, e);
@@ -139,6 +142,7 @@ public List<Node> handle(ResultSet rs) throws SQLException {
139142
node.setCreateTime(rs.getLong("createTime"));
140143
node.setThreads(rs.getInt("threads"));
141144
node.setAvailable(rs.getInt("available") == 1);
145+
node.setHostName(rs.getString("hostName"));
142146
nodes.add(node);
143147
}
144148
return nodes;

lts-admin/src/main/webapp/main/node-manager.jsp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@
167167
}
168168
},
169169
{
170-
title: 'IP', dataIndex: 'ip', width: 140, renderer: function (v, obj) {
170+
title: '机器', dataIndex: 'ip', width: 140, renderer: function (v, obj) {
171171
if (obj['nodeType'] == 'JOB_TRACKER') {
172-
return obj['ip'] + ":" + obj['port'];
172+
return obj['hostName'] + "<br/>(" + obj['ip'] + ":" + obj['port'] + ")";
173173
}
174-
return obj['ip'];
174+
return obj['hostName'] + "<br/>(" + obj['ip'] + ")";
175175
}
176176
},
177177
{

lts-core/src/main/java/com/lts/core/cluster/Node.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class Node {
1818
private NodeType nodeType;
1919
private String ip;
2020
private Integer port;
21+
private String hostName;
2122
private String group;
2223
private Long createTime = SystemClock.now();
2324
// 线程个数
@@ -30,6 +31,14 @@ public class Node {
3031

3132
private String fullString;
3233

34+
public String getHostName() {
35+
return hostName;
36+
}
37+
38+
public void setHostName(String hostName) {
39+
this.hostName = hostName;
40+
}
41+
3342
public boolean isAvailable() {
3443
return available;
3544
}

lts-core/src/main/java/com/lts/core/commons/utils/NetUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ public static String getLocalHost() {
127127
return address == null ? LOCALHOST : address.getHostAddress();
128128
}
129129

130+
public static String getLocalHostName(){
131+
InetAddress address = getLocalAddress();
132+
if(address == null){
133+
return "localhost";
134+
}
135+
return address.getHostName();
136+
}
137+
130138
/**
131139
* 遍历本地网卡,返回第一个合理的IP。
132140
*

lts-core/src/main/java/com/lts/core/factory/NodeFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public static <T extends Node> T create(Class<T> clazz, Config config) {
1414
try {
1515
T node = clazz.newInstance();
1616
node.setIp(NetUtils.getLocalHost());
17+
node.setHostName(NetUtils.getLocalHostName());
1718
node.setGroup(config.getNodeGroup());
1819
node.setThreads(config.getWorkThreads());
1920
node.setPort(config.getListenPort());

lts-core/src/main/java/com/lts/core/registry/NodeRegistryUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public static Node parse(String fullPath) {
5858
node.setCreateTime(Long.valueOf(value));
5959
} else if ("isAvailable".equals(key)) {
6060
node.setAvailable(Boolean.valueOf(value));
61+
} else if("hostName".equals(key)){
62+
node.setHostName(value);
6163
}
6264
}
6365
return node;
@@ -93,7 +95,9 @@ public static String getFullPath(Node node) {
9395
.append("&createTime=")
9496
.append(node.getCreateTime())
9597
.append("&isAvailable=")
96-
.append(node.isAvailable());
98+
.append(node.isAvailable())
99+
.append("&hostName=")
100+
.append(node.getHostName());
97101

98102
return path.toString();
99103
}

lts-example/src/main/java/com/lts/example/benchmark/JobTrackerMain.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.lts.example.benchmark;
22

33
import com.lts.example.support.MasterChangeListenerImpl;
4+
import com.lts.example.support.MemoryStatus;
45
import com.lts.jobtracker.JobTracker;
56
import com.lts.jobtracker.support.policy.OldDataDeletePolicy;
67

@@ -48,13 +49,14 @@ public static void main(String[] args) {
4849
jobTracker.addConfig("job.queue", "mongo");
4950
// mongo 配置
5051
jobTracker.addConfig("mongo.addresses", "127.0.0.1:27017"); // 多个地址用逗号分割
51-
jobTracker.addConfig("mongo.database", "lts2");
52+
jobTracker.addConfig("mongo.database", "lts3");
5253
// 这个是对于 返回给客户端 任务的 老数据删除策略
5354
jobTracker.setOldDataHandler(new OldDataDeletePolicy());
5455
// 设置 zk 客户端用哪个, 可选 zkclient(默认), curator
5556
jobTracker.addConfig("zk.client", "zkclient");
5657
// 启动节点
5758
jobTracker.start();
59+
MemoryStatus.print();
5860
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
5961
@Override
6062
public void run() {

lts-example/src/main/java/com/lts/example/support/TestJobRunner.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public class TestJobRunner implements JobRunner {
2020
@Override
2121
public Result run(Job job) throws Throwable {
2222
try {
23-
Thread.sleep(1000L);
24-
25-
if (job.getRetryTimes() > 5) {
26-
return new Result(Action.EXECUTE_FAILED, "重试次数超过5次了,放过你吧!");
27-
}
28-
29-
if (SystemClock.now() % 2 == 1) {
30-
return new Result(Action.EXECUTE_LATER, "稍后执行");
31-
}
23+
// Thread.sleep(1000L);
24+
//
25+
// if (job.getRetryTimes() > 5) {
26+
// return new Result(Action.EXECUTE_FAILED, "重试次数超过5次了,放过你吧!");
27+
// }
28+
//
29+
// if (SystemClock.now() % 2 == 1) {
30+
// return new Result(Action.EXECUTE_LATER, "稍后执行");
31+
// }
3232

3333
// TODO 业务逻辑
3434
LOGGER.info("我要执行:" + job);

lts-example/src/main/resources/log4j.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
log4j.rootLogger=WARN,stdout
2+
log4j.rootLogger=INFO,stdout
33

4-
log4j.appender.stdout.Threshold=WARN
4+
log4j.appender.stdout.Threshold=INFO
55
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
66
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
77
log4j.appender.stdout.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n

0 commit comments

Comments
 (0)