Commit 8124cc8b authored by zengtianlai3's avatar zengtianlai3

简单的负载均衡随机分配策略

parent 91ebb822
package iot.sixiang.license.auth;
import iot.sixiang.license.entity.Server;
import iot.sixiang.license.service.ServerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.*;
@Component
@Slf4j
public class AuthManager {
@Autowired
private ServerService serverService;
private Map<String, Server> allServers = null;
public AuthManager() {
allServers = new HashMap<String, Server>();
}
@PostConstruct
public void init() {
List<Server> servers = serverService.getServerList(0, 20);
for (Server server : servers) {
String serverIp = server.getServerIp();
allServers.put(serverIp, server);
}
}
public Server getBalanceServer(){
int count = allServers.size();
if(count==0){
return null;
}else{
Random random = new Random();
int index = random.nextInt(count);
List<Server> servers = new ArrayList<>(allServers.values());
return servers.get(index);
}
}
}
...@@ -27,10 +27,6 @@ public class DeviceManager { ...@@ -27,10 +27,6 @@ public class DeviceManager {
@Autowired @Autowired
private DeviceServerHandler handler; private DeviceServerHandler handler;
@Getter
@Setter
SessionContext sessionContext = null;
public DeviceManager() { public DeviceManager() {
sessionContexts = new HashMap<String, SessionContext>(); sessionContexts = new HashMap<String, SessionContext>();
......
package iot.sixiang.license.event; package iot.sixiang.license.event;
import iot.sixiang.license.auth.AuthManager;
import iot.sixiang.license.entity.Server;
import iot.sixiang.license.forward.ForwardManager; import iot.sixiang.license.forward.ForwardManager;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -15,18 +17,29 @@ public class CreateForwarClientEventHandler { ...@@ -15,18 +17,29 @@ public class CreateForwarClientEventHandler {
ForwardManager forwardManager; ForwardManager forwardManager;
@Autowired @Autowired
EventPublisher eventPublisher; EventPublisher eventPublisher;
@Autowired
AuthManager authManager;
public CreateForwarClientEventHandler() { public CreateForwarClientEventHandler() {
} }
// @Async("asyncExecutor")
@EventListener @EventListener
public void handlerEvent(CreateForwarClientEvent event) { public void handlerEvent(CreateForwarClientEvent event) {
String appId = event.getAppId(); String appId = event.getAppId();
forwardManager.startTcpClient(appId);
Server balanceServer = authManager.getBalanceServer();
if (balanceServer != null) {
String serverIp = balanceServer.getServerIp();
Integer port = balanceServer.getPort();
forwardManager.startTcpClient(serverIp, port, appId);
} else {
log.error("balanceServer is null");
}
} }
......
...@@ -20,8 +20,6 @@ public class ForwardManager { ...@@ -20,8 +20,6 @@ public class ForwardManager {
@Autowired @Autowired
public ForwardClient client; public ForwardClient client;
String serviceIP = "121.46.25.14";
int port = 18889;
private Map<String, SessionContext> sessionContexts = null; private Map<String, SessionContext> sessionContexts = null;
...@@ -30,7 +28,7 @@ public class ForwardManager { ...@@ -30,7 +28,7 @@ public class ForwardManager {
} }
public void startTcpClient(String appId) { public void startTcpClient(String serviceIP,int port,String appId) {
client.startTcp(serviceIP, port,appId); client.startTcp(serviceIP, port,appId);
} }
......
server.port=8868 server.port=8868
logging.level.root=debug logging.level.root=debug
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/firstwork?serverTimezone=GMT%2B8 spring.datasource.url=jdbc:mysql://localhost:3306/iot_license?serverTimezone=GMT%2B8
spring.datasource.username=root spring.datasource.username=root
spring.datasource.password=123456 spring.datasource.password=123456
mybatis-plus.mapper-locations=classpath:/mapper/**.xml mybatis-plus.mapper-locations=classpath:/mapper/**.xml
......
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