初始化

master
DongZeLiang 2024-04-18 10:58:25 +08:00
parent de22a25f44
commit f0a9228de6
13 changed files with 458 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package com.muyu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author DongZl
* @description:
* @Date 2024/4/12 5:11
*/
@SpringBootApplication
public class LoadCenterApplication {
public static void main (String[] args) {
SpringApplication.run(LoadCenterApplication.class, args);
}
}

View File

@ -0,0 +1,112 @@
package com.muyu.common.domain;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
*
*
* @author muyu
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {
/**
*
*/
public static final int SUCCESS = 200;
/**
*
*/
public static final int FAIL = 500;
/**
*
*/
public static final int WARN = 501;
private static final long serialVersionUID = 1L;
private int code;
private String msg;
private T data;
public static <T> Result<T> buildCode(int code, String msg, T data){
return restResult(data, code, msg);
}
public static <T> Result<T> success () {
return restResult(null, SUCCESS, null);
}
public static <T> Result<T> success (T data) {
return restResult(data, SUCCESS, null);
}
public static <T> Result<T> success (T data, String msg) {
return restResult(data, SUCCESS, msg);
}
public static <T> Result<T> error () {
return restResult(null, FAIL, null);
}
public static <T> Result<T> error (String msg) {
return restResult(null, FAIL, msg);
}
public static <T> Result<T> error (T data) {
return restResult(data, FAIL, null);
}
public static <T> Result<T> error (T data, String msg) {
return restResult(data, FAIL, msg);
}
public static <T> Result<T> error (int code, String msg) {
return restResult(null, code, msg);
}
public static <T> Result<T> warn () {
return restResult(null, WARN, null);
}
public static <T> Result<T> warn (String msg) {
return restResult(null, WARN, msg);
}
public static <T> Result<T> warn (T data) {
return restResult(data, WARN, null);
}
public static <T> Result<T> warn (T data, String msg) {
return restResult(data, WARN, msg);
}
public static <T> Result<T> warn (int code, String msg) {
return restResult(null, code, msg);
}
private static <T> Result<T> restResult (T data, int code, String msg) {
return Result.<T>builder()
.code(code)
.data(data)
.msg(msg)
.build();
}
public static <T> Boolean isError (Result<T> ret) {
return !isSuccess(ret);
}
public static <T> Boolean isSuccess (Result<T> ret) {
return Result.SUCCESS == ret.getCode();
}
}

View File

@ -0,0 +1,27 @@
package com.muyu.controller;
import com.muyu.common.domain.Result;
import com.muyu.service.GatewayLoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: DongZeLiang
* @date: 2024/4/18
* @Description:
* @Version: 1.0
*/
@RestController
@RequestMapping("/gateway")
public class GatewayController {
@Autowired
private GatewayLoadService gatewayLoadService;
@GetMapping("/load/node")
public Result<String> loadNode() {
return Result.success(gatewayLoadService.loadNode());
}
}

View File

@ -0,0 +1,50 @@
package com.muyu.gateway.cache;
import com.muyu.gateway.cache.abs.GatewayCacheAbs;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @Author: DongZeLiang
* @date: 2024/4/18
* @Description:
* @Version: 1.0
*/
@Component
public class GatewayLoadNodeCache extends GatewayCacheAbs<String> {
private final static String gatewayLoadNodeKey = "node";
@Override
public String getPre () { return "gateway:load:";}
/**
*
* @param nodeList
*/
public void put(List<String> nodeList){
redisService.deleteObject(encode(gatewayLoadNodeKey));
redisService.setCacheList(encode(gatewayLoadNodeKey), nodeList);
}
/**
*
* @return
*/
public List<String> get(){
return redisService.getCacheList(encode(gatewayLoadNodeKey));
}
/**
*
* @param index
* @return
*/
public String getBydIndex(Long index){
if (index == null || index > 100){
throw new RuntimeException("下标违法0-100");
}
return redisService.getCacheListValue(encode(gatewayLoadNodeKey), index);
}
}

View File

@ -0,0 +1,53 @@
package com.muyu.gateway.cache;
import com.muyu.common.redis.service.RedisService;
import com.muyu.gateway.cache.abs.GatewayCacheAbs;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @Author: DongZeLiang
* @date: 2024/4/18
* @Description:
* @Version: 1.0
*/
@Component
public class GatewayLoadSeriesCache extends GatewayCacheAbs<String> {
private final static String gatewayLoadSeriesKey = "series";
@Override
public String getPre () { return "gateway:load:";}
/**
* bean
*/
@PostConstruct
public void init(){
redisService.setCacheObject(encode(gatewayLoadSeriesKey), 0L);
}
/**
*
* @return
*/
public Long get(){
return redisService.getCacheObject(encode(gatewayLoadSeriesKey));
}
/**
*
* @return
*/
public Long incrementAndGet(){
return redisService.increment(encode(gatewayLoadSeriesKey), 1L);
}
/**
*
*/
public void reset(){
this.init();
}
}

View File

@ -0,0 +1,45 @@
package com.muyu.gateway.cache;
import com.muyu.gateway.cache.abs.GatewayCacheAbs;
import com.muyu.gateway.model.GatewayNodeInfo;
import org.springframework.stereotype.Component;
/**
* @Author: DongZeLiang
* @date: 2024/4/18
* @Description:
* @Version: 1.0
*/
@Component
public class GatewayNodeCache extends GatewayCacheAbs<String> {
@Override
public String getPre () {
return "gateway:node:info:";
}
/**
*
* @param gatewayNodeInfo
*/
public void put(GatewayNodeInfo gatewayNodeInfo){
redisService.setCacheObject(encode(gatewayNodeInfo.getNodeId()), gatewayNodeInfo);
}
/**
*
* @param nodeId ID
* @return
*/
public GatewayNodeInfo get(String nodeId){
return redisService.getCacheObject(encode(nodeId));
}
/**
*
* @param nodeId ID
*/
public void remove(String nodeId){
redisService.deleteObject(encode(nodeId));
}
}

View File

@ -0,0 +1,10 @@
package com.muyu.gateway.cache;
/**
* @Author: DongZeLiang
* @date: 2024/4/18
* @Description:
* @Version: 1.0
*/
public class GatewayNodeScoreCache {
}

View File

@ -0,0 +1,10 @@
package com.muyu.gateway.cache;
/**
* @Author: DongZeLiang
* @date: 2024/4/18
* @Description: VIN
* @Version: 1.0
*/
public class GatewayNodeSetVinCache {
}

View File

@ -0,0 +1,10 @@
package com.muyu.gateway.cache;
/**
* @Author: DongZeLiang
* @date: 2024/4/18
* @Description:
* @Version: 1.0
*/
public class GatewayVehicleLineNodeCache {
}

View File

@ -0,0 +1,22 @@
package com.muyu.gateway.cache.abs;
import com.muyu.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @Author: DongZeLiang
* @date: 2024/4/18
* @Description:
* @Version: 1.0
*/
public abstract class GatewayCacheAbs<K> {
@Autowired
public RedisService redisService;
public abstract String getPre();
public String encode(K key){
return getPre() + key;
}
}

View File

@ -0,0 +1,35 @@
package com.muyu.gateway.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Author: DongZeLiang
* @date: 2024/4/18
* @Description:
* @Version: 1.0
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class GatewayNodeInfo {
/**
* ID
*/
private String nodeId;
/**
* IP
*/
private String publicIdAddress;
/**
* IP
*/
private String privateIdAddress;
}

View File

@ -0,0 +1,16 @@
package com.muyu.service;
/**
* @Author: DongZeLiang
* @date: 2024/4/18
* @Description:
* @Version: 1.0
*/
public interface GatewayLoadService {
/**
*
* @return
*/
String loadNode ();
}

View File

@ -0,0 +1,52 @@
package com.muyu.service.impl;
import com.muyu.gateway.cache.GatewayLoadNodeCache;
import com.muyu.gateway.cache.GatewayLoadSeriesCache;
import com.muyu.gateway.cache.GatewayNodeCache;
import com.muyu.gateway.model.GatewayNodeInfo;
import com.muyu.service.GatewayLoadService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
/**
* @Author: DongZeLiang
* @date: 2024/4/18
* @Description:
* @Version: 1.0
*/
@Service
@AllArgsConstructor
public class GatewayLoadServiceImpl implements GatewayLoadService {
private final Long nodeLength = 100L;
/**
*
*/
private final GatewayLoadNodeCache gatewayLoadNodeCache;
/**
*
*/
private final GatewayLoadSeriesCache gatewayLoadSeriesCache;
/**
*
*/
private final GatewayNodeCache gatewayNodeCache;
/**
*
*
* @return
*/
@Override
public String loadNode () {
Long seriesLoad = gatewayLoadSeriesCache.incrementAndGet();
Long seriesLoadIndex = seriesLoad % nodeLength;
String loadNodeId = gatewayLoadNodeCache.getBydIndex(seriesLoadIndex);
GatewayNodeInfo gatewayNodeInfo = gatewayNodeCache.get(loadNodeId);
return gatewayNodeInfo.getPublicIdAddress();
}
}