Compare commits
78 Commits
8edcff3094
...
2b559984aa
Author | SHA1 | Date |
---|---|---|
|
2b559984aa | |
|
ed3a542066 | |
|
2a3d68f51d | |
|
9b41098f32 | |
|
e4fa985e2f | |
|
8ce6502511 | |
|
78c7834036 | |
|
b22e3f54ef | |
|
5894089eef | |
|
51e7c2c0c0 | |
|
5a91f224cd | |
|
9ff1093c03 | |
|
581c3622a9 | |
|
2f0fd35808 | |
|
3ea65346d8 | |
|
5143ef67de | |
|
3180012c6c | |
|
3bd6aeb39d | |
|
cd41265b79 | |
|
5bb57ecbb2 | |
|
eb59a2400a | |
|
53426bf396 | |
|
b7a6278257 | |
|
99be16cde2 | |
|
a095941152 | |
|
c29a08b0ff | |
|
c98804b1f4 | |
|
b12076b5d5 | |
|
ff492390b1 | |
|
f00c530de1 | |
|
f38e42f72f | |
|
f10fe5c241 | |
|
6cb6c8d1fd | |
|
f5d6b5c00d | |
|
fcb6f81a12 | |
|
854df0aab5 | |
|
469eedacb5 | |
|
eb37fb5072 | |
|
a28e5d751b | |
|
0f0883a781 | |
|
f6eaf6b05f | |
|
4cdd40fdc8 | |
|
58d49f9d8b | |
|
479b55ec18 | |
|
441e362f09 | |
|
4765f0bb58 | |
|
b318c0e30b | |
|
7e77b4c0c0 | |
|
53311e2f98 | |
|
ba6ac5b76d | |
|
07bafd5eb4 | |
|
8585f65ff9 | |
|
b7351ee49e | |
|
1e9002ea93 | |
|
723cb6556d | |
|
c90f3fa056 | |
|
1a3b267685 | |
|
2c1765b3a6 | |
|
0c0d9fca3c | |
|
1e668ce238 | |
|
052a0012fc | |
|
b0f3162ceb | |
|
92ffcd21bd | |
|
aaff10bc40 | |
|
09353cd0e9 | |
|
a1fca6b081 | |
|
8741c20ddb | |
|
9b3da53c6b | |
|
d26b1bc5e5 | |
|
aa619a7be0 | |
|
f0a69766d4 | |
|
c980f0d46b | |
|
16cf9b5603 | |
|
04285aea80 | |
|
8332507e79 | |
|
2f12f3d2e0 | |
|
f3c057352f | |
|
85fdb57b17 |
|
@ -16,11 +16,31 @@
|
|||
|
||||
<dependencies>
|
||||
|
||||
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.muyu</groupId>-->
|
||||
<!-- <artifactId>cloud-common-saas</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos Config -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Sentinel -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- SpringBoot Web -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@ -45,6 +65,7 @@
|
|||
<artifactId>cloud-common-api-doc</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
|
@ -87,6 +108,7 @@
|
|||
<artifactId>cloud-common-log</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -10,7 +10,11 @@ import com.muyu.common.security.auth.AuthUtil;
|
|||
import com.muyu.common.security.service.TokenService;
|
||||
import com.muyu.common.security.utils.SecurityUtils;
|
||||
import com.muyu.common.system.domain.LoginUser;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
@ -25,16 +29,20 @@ import jakarta.servlet.http.HttpServletRequest;
|
|||
*/
|
||||
@RestController
|
||||
public class TokenController {
|
||||
|
||||
@Resource
|
||||
private TokenService tokenService;
|
||||
|
||||
@Resource
|
||||
@Autowired
|
||||
|
||||
private SysLoginService sysLoginService;
|
||||
|
||||
@PostMapping("login")
|
||||
public Result<?> login (@RequestBody LoginBody form) {
|
||||
// 用户登录
|
||||
|
||||
LoginUser userInfo = sysLoginService.login(form);
|
||||
|
||||
// 获取登录token
|
||||
return Result.success(tokenService.createToken(userInfo));
|
||||
}
|
||||
|
|
|
@ -1,16 +1,21 @@
|
|||
package com.muyu.auth.form;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 用户登录对象
|
||||
*
|
||||
* @author muyu
|
||||
*/
|
||||
|
||||
@Data
|
||||
public class LoginBody {
|
||||
|
||||
private String firmCode;
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
|
@ -20,4 +25,16 @@ public class LoginBody {
|
|||
* 用户密码
|
||||
*/
|
||||
private String password;
|
||||
|
||||
|
||||
|
||||
public String getUsername () {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername (String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -13,14 +13,13 @@ import com.muyu.common.core.utils.StringUtils;
|
|||
import com.muyu.common.core.utils.ip.IpUtils;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import com.muyu.common.security.utils.SecurityUtils;
|
||||
import com.muyu.common.system.domain.LoginUser;
|
||||
import com.muyu.common.system.domain.SysUser;
|
||||
import com.muyu.common.system.remote.RemoteSaasService;
|
||||
import com.muyu.common.system.remote.RemoteUserService;
|
||||
import com.muyu.common.system.domain.SysUser;
|
||||
import com.muyu.common.system.domain.LoginUser;
|
||||
import javax.annotation.Resource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Set;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -6,7 +6,11 @@ import com.muyu.common.core.exception.ServiceException;
|
|||
import com.muyu.common.redis.service.RedisService;
|
||||
import com.muyu.common.security.utils.SecurityUtils;
|
||||
import com.muyu.common.system.domain.SysUser;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
@ -18,14 +22,22 @@ import java.util.concurrent.TimeUnit;
|
|||
*/
|
||||
@Component
|
||||
public class SysPasswordService {
|
||||
|
||||
@Resource
|
||||
|
||||
@Autowired
|
||||
|
||||
private RedisService redisService;
|
||||
|
||||
private int maxRetryCount = CacheConstants.PASSWORD_MAX_RETRY_COUNT;
|
||||
|
||||
private Long lockTime = CacheConstants.PASSWORD_LOCK_TIME;
|
||||
|
||||
|
||||
@Resource
|
||||
|
||||
@Autowired
|
||||
|
||||
private SysRecordLogService recordLogService;
|
||||
|
||||
/**
|
||||
|
|
|
@ -6,7 +6,11 @@ import com.muyu.common.core.utils.StringUtils;
|
|||
import com.muyu.common.core.utils.ip.IpUtils;
|
||||
import com.muyu.common.system.remote.RemoteLogService;
|
||||
import com.muyu.common.system.domain.SysLogininfor;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
|
@ -16,7 +20,11 @@ import org.springframework.stereotype.Component;
|
|||
*/
|
||||
@Component
|
||||
public class SysRecordLogService {
|
||||
|
||||
@Resource
|
||||
|
||||
@Autowired
|
||||
|
||||
private RemoteLogService remoteLogService;
|
||||
|
||||
/**
|
||||
|
|
|
@ -4,10 +4,10 @@ server:
|
|||
|
||||
# nacos线上地址
|
||||
nacos:
|
||||
addr: 49.235.136.60:8848
|
||||
addr: 47.116.173.119:8848
|
||||
user-name: nacos
|
||||
password: nacos
|
||||
namespace: wyh
|
||||
namespace: oneone
|
||||
# Spring
|
||||
spring:
|
||||
application:
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>cloud-common-cache</artifactId>
|
||||
<description>
|
||||
cloud-common-cache 缓存基准
|
||||
</description>
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
|
||||
<!-- 基础缓存模块 -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,37 @@
|
|||
package com.muyu.common.cache;
|
||||
|
||||
import com.muyu.common.cache.decoration.DecorationKey;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 原子序列缓存基准
|
||||
* @Date 2024-4-1 下午 08:07
|
||||
*/
|
||||
public interface AtomicSequenceCache<K> extends DecorationKey<K> {
|
||||
|
||||
/**
|
||||
* 获取存储的值
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public Long get(K key);
|
||||
|
||||
/**
|
||||
* 自增
|
||||
*/
|
||||
public Long increment(K key);
|
||||
/**
|
||||
* 自减
|
||||
*/
|
||||
public Long decrement(K key);
|
||||
|
||||
/**
|
||||
* 增加数值
|
||||
*/
|
||||
public Long increment(K key, Long number);
|
||||
|
||||
/**
|
||||
* 减少数值
|
||||
*/
|
||||
public Long decrement(K key, Long number);
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.muyu.common.cache;
|
||||
|
||||
/**
|
||||
* 数据转换接口
|
||||
* @param <K> 数据键
|
||||
* @param <V> 数据值
|
||||
*/
|
||||
public interface BasicCacheData <K,V>{
|
||||
|
||||
public V apply(K key);
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.muyu.common.cache;
|
||||
|
||||
import com.muyu.common.cache.decoration.DecorationKey;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 缓存接口基类
|
||||
* @Date 2024-3-26 下午 03:25
|
||||
*/
|
||||
public interface Cache <K, V> extends DecorationKey<K> {
|
||||
|
||||
/**
|
||||
* 通过Key获取value值
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public V get(K key);
|
||||
|
||||
/**
|
||||
* 缓存添加/修改
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
*/
|
||||
public void put(K key, V value);
|
||||
|
||||
/**
|
||||
* 通过键删除
|
||||
* @param key 键
|
||||
*/
|
||||
public void remove(K key);
|
||||
|
||||
/**
|
||||
* 刷新缓存时间
|
||||
* @param key 键
|
||||
*/
|
||||
public void refreshTime (K key);
|
||||
|
||||
/**
|
||||
* 刷新缓存数据
|
||||
* @param key 键
|
||||
*/
|
||||
public void refreshData (K key);
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
package com.muyu.common.cache;
|
||||
|
||||
import com.muyu.common.cache.decoration.DecorationKey;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: Hash缓存基准
|
||||
* @Date 2024-3-29 下午 03:16
|
||||
*/
|
||||
public interface HashCache <K, HK, HV> extends DecorationKey<K> {
|
||||
|
||||
|
||||
/**
|
||||
* 编码
|
||||
* @param hashKey ID
|
||||
* @return 键
|
||||
*/
|
||||
public String encodeHashKey(HK hashKey);
|
||||
|
||||
/**
|
||||
* 解码
|
||||
* @param redisHashKey 数据库键
|
||||
* @return ID
|
||||
*/
|
||||
public HK decodeHashKey(String redisHashKey);
|
||||
|
||||
/**
|
||||
* 通过Key获取所有的map
|
||||
* @param key 数据库键
|
||||
* @return 所有集合Map
|
||||
*/
|
||||
public Map<HK, HV> get(K key);
|
||||
|
||||
/**
|
||||
* 通过键和hashKey获取数据库hashValue
|
||||
* @param key 键
|
||||
* @param hashKey hash键
|
||||
* @return hash值
|
||||
*/
|
||||
public HV get(K key, HK hashKey);
|
||||
|
||||
/**
|
||||
* 通过键和hashKey获取数据库hashValue
|
||||
* @param key 键
|
||||
* @param hashKeyList hash键集合
|
||||
* @return hash值
|
||||
*/
|
||||
public List<HV> get(K key, HK... hashKeyList);
|
||||
|
||||
/**
|
||||
* 获取hash值集合
|
||||
* @param key 键
|
||||
* @return hash值集合
|
||||
*/
|
||||
public List<HV> getToList(K key);
|
||||
|
||||
/**
|
||||
* 存储数据
|
||||
* @param key redis键
|
||||
* @param map hashMap集合
|
||||
*/
|
||||
public void put(K key, Map<HK, HV> map);
|
||||
|
||||
/**
|
||||
* 存储数据
|
||||
* @param key redis键
|
||||
* @param dataList 数据值
|
||||
* @param hashKey hash键
|
||||
*/
|
||||
public void put(K key, List<HV> dataList, Function<HV, HK> hashKey);
|
||||
|
||||
/**
|
||||
* 存储数据
|
||||
* @param key redis键
|
||||
* @param hashKey hash键
|
||||
* @param hashValue hash值
|
||||
*/
|
||||
public void put(K key, HK hashKey, HV hashValue);
|
||||
|
||||
/**
|
||||
* 通过redis键删除
|
||||
* @param key hash键
|
||||
*/
|
||||
public void remove(K key);
|
||||
|
||||
/**
|
||||
* 通过redis键和hash键删除
|
||||
* @param key redis键
|
||||
* @param hashKey hash键
|
||||
*/
|
||||
public void remove(K key, HK hashKey);
|
||||
|
||||
/**
|
||||
* 判断redis中hashKey是否存在
|
||||
* @param key redis键
|
||||
* @param hashKey hash键
|
||||
*/
|
||||
public boolean hasKey(K key, HK hashKey);
|
||||
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
package com.muyu.common.cache.abs;
|
||||
|
||||
import com.muyu.common.cache.AtomicSequenceCache;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 原子序列缓存抽象类
|
||||
* @Date 2024-4-1 下午 08:33
|
||||
*/
|
||||
public abstract class AtomicSequenceCacheAbs<K> implements AtomicSequenceCache<K> {
|
||||
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
/**
|
||||
* 获取存储的值
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
@Override
|
||||
public Long get (K key) {
|
||||
Long cacheValue = this.redisService.getCacheObject(encode(key));
|
||||
if (cacheValue == null){
|
||||
Long data = getData(key);
|
||||
cacheValue = data == null ? 0L : data;
|
||||
this.redisService.setCacheObject(encode(key), cacheValue);
|
||||
}
|
||||
return cacheValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自增
|
||||
* @param key
|
||||
*/
|
||||
@Override
|
||||
public Long increment (K key) {
|
||||
return this.increment(key, 1L);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自减
|
||||
*
|
||||
* @param key
|
||||
*/
|
||||
@Override
|
||||
public Long decrement (K key) {
|
||||
return this.decrement(key, 1L);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加数值
|
||||
*
|
||||
* @param key
|
||||
* @param number
|
||||
*/
|
||||
@Override
|
||||
public Long increment (K key, Long number) {
|
||||
Long numberValue = redisService.getCacheObject(encode(key));
|
||||
if (numberValue == null){
|
||||
Long data = getData(key);
|
||||
data = data == null ? 0L : data;
|
||||
redisService.setCacheObject(encode(key), data);
|
||||
}
|
||||
return redisService.increment(encode(key), number);
|
||||
}
|
||||
|
||||
/**
|
||||
* 减少数值
|
||||
*
|
||||
* @param key
|
||||
* @param number
|
||||
*/
|
||||
@Override
|
||||
public Long decrement (K key, Long number) {
|
||||
Long numberValue = redisService.getCacheObject(encode(key));
|
||||
if (numberValue == null){
|
||||
Long data = getData(key);
|
||||
data = data == null ? 0L : data;
|
||||
redisService.setCacheObject(encode(key), data);
|
||||
}
|
||||
return redisService.decrement(encode(key), number);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码
|
||||
*
|
||||
* @param key ID
|
||||
*
|
||||
* @return 键
|
||||
*/
|
||||
@Override
|
||||
public String encode (K key) {
|
||||
return keyPre() + key;
|
||||
}
|
||||
|
||||
public abstract Long getData(K key);
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
package com.muyu.common.cache.abs;
|
||||
|
||||
import com.muyu.common.cache.Cache;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 缓存抽象类
|
||||
* @Date 2024-3-27 下午 03:10
|
||||
*/
|
||||
public abstract class CacheAbs<K, V> implements Cache<K, V> {
|
||||
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
/**
|
||||
* 编码
|
||||
* @param key ID
|
||||
* @return 键
|
||||
*/
|
||||
@Override
|
||||
public String encode (K key) {
|
||||
return keyPre() + key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过Key获取value值
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
@Override
|
||||
public V get (K key) {
|
||||
V value = redisService.getCacheObject(encode(key));
|
||||
if (value == null){
|
||||
value = getData(key);
|
||||
if (value == null){
|
||||
value = defaultValue();
|
||||
}
|
||||
}
|
||||
this.put(key, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存添加/修改
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
*/
|
||||
@Override
|
||||
public void put (K key, V value) {
|
||||
this.redisService.setCacheObject(encode(key), value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过键删除
|
||||
* @param key 键
|
||||
*/
|
||||
@Override
|
||||
public void remove (K key) {
|
||||
this.redisService.deleteObject(encode(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新缓存
|
||||
* @param key 键
|
||||
*/
|
||||
@Override
|
||||
public void refreshTime (K key) {
|
||||
this.redisService.expire(encode(key), 60, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新缓存数据
|
||||
*
|
||||
* @param key 键
|
||||
*/
|
||||
@Override
|
||||
public void refreshData (K key) {
|
||||
this.put(key, getData(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 从数据库获取数据
|
||||
* @param key ID
|
||||
* @return 缓存对象
|
||||
*/
|
||||
public abstract V getData(K key);
|
||||
|
||||
/**
|
||||
* 默认值
|
||||
*/
|
||||
public abstract V defaultValue();
|
||||
}
|
|
@ -0,0 +1,223 @@
|
|||
package com.muyu.common.cache.abs;
|
||||
|
||||
import com.muyu.common.cache.HashCache;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: hash缓存抽象类
|
||||
* @Date 2024-3-29 下午 07:40
|
||||
*/
|
||||
public abstract class HashCacheAbs<K, HK, HV> implements HashCache<K, HK, HV> {
|
||||
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
/**
|
||||
* 编码
|
||||
*
|
||||
* @param key ID
|
||||
*
|
||||
* @return 键
|
||||
*/
|
||||
@Override
|
||||
public String encode (K key) {
|
||||
return keyPre() + key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码
|
||||
* @param hashKey ID
|
||||
* @return 键
|
||||
*/
|
||||
@Override
|
||||
public String encodeHashKey (HK hashKey) {
|
||||
return hashKey.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过Key获取所有的map
|
||||
* @param key 数据库键
|
||||
* @return 所有集合Map
|
||||
*/
|
||||
@Override
|
||||
public Map<HK, HV> get (K key) {
|
||||
// 获取为null的情况
|
||||
Map<String, HV> cacheMap = redisService.getCacheMap(encode(key));
|
||||
if (cacheMap == null || cacheMap.isEmpty()){
|
||||
Map<HK, HV> dataMap = getData(key);
|
||||
if (dataMap != null && !dataMap.isEmpty()){
|
||||
cacheMap = encodeMap(dataMap);
|
||||
}else {
|
||||
cacheMap = encodeMap(defaultValue());
|
||||
}
|
||||
redisService.setCacheMap(encode(key), cacheMap);
|
||||
}
|
||||
return decodeMap(cacheMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过键和hashKey获取数据库hashValue
|
||||
*
|
||||
* @param key 键
|
||||
* @param hashKey hash键
|
||||
*
|
||||
* @return hash值
|
||||
*/
|
||||
@Override
|
||||
public HV get (K key, HK hashKey) {
|
||||
HV hashValue = redisService.getCacheMapValue(encode(key), encodeHashKey(hashKey));
|
||||
if (hashValue == null){
|
||||
HV dataValue = getData(key, hashKey);
|
||||
hashValue = dataValue != null ? dataValue : defaultHashValue();
|
||||
put(key, hashKey, hashValue);
|
||||
}
|
||||
return hashValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过键和hashKey获取数据库hashValue
|
||||
*
|
||||
* @param key 键
|
||||
* @param hashKeyList hash键集合
|
||||
*
|
||||
* @return hash值
|
||||
*/
|
||||
@Override
|
||||
public List<HV> get (K key, HK... hashKeyList) {
|
||||
List<String> encodeHashKeyList = Arrays.stream(hashKeyList).map(this::encodeHashKey).toList();
|
||||
return redisService.getMultiCacheMapValue(encode(key), encodeHashKeyList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取hash值集合
|
||||
*
|
||||
* @param key 键
|
||||
*
|
||||
* @return hash值集合
|
||||
*/
|
||||
@Override
|
||||
public List<HV> getToList (K key) {
|
||||
Map<HK, HV> hkhvMap = get(key);
|
||||
return hkhvMap.values().stream().toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储数据
|
||||
*
|
||||
* @param key redis键
|
||||
* @param map hashMap集合
|
||||
*/
|
||||
@Override
|
||||
public void put (K key, Map<HK, HV> map) {
|
||||
redisService.setCacheMap(encode(key), encodeMap(map));
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储数据
|
||||
*
|
||||
* @param key redis键
|
||||
* @param dataList 数据值
|
||||
* @param hashKey hash键
|
||||
*/
|
||||
@Override
|
||||
public void put (K key, List<HV> dataList, Function<HV, HK> hashKey) {
|
||||
Map<HK, HV> dataMap = new HashMap<>();
|
||||
dataList.forEach((data) -> dataMap.put(hashKey.apply(data), data));
|
||||
redisService.setCacheMap(encode(key), encodeMap(dataMap));
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储数据
|
||||
*
|
||||
* @param key redis键
|
||||
* @param hashKey hash键
|
||||
* @param hashValue hash值
|
||||
*/
|
||||
@Override
|
||||
public void put (K key, HK hashKey, HV hashValue) {
|
||||
redisService.setCacheMapValue(encode(key), encodeHashKey(hashKey), hashValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过redis键删除
|
||||
*
|
||||
* @param key hash键
|
||||
*/
|
||||
@Override
|
||||
public void remove (K key) {
|
||||
redisService.deleteObject(encode(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过redis键和hash键删除
|
||||
*
|
||||
* @param key redis键
|
||||
* @param hashKey hash键
|
||||
*/
|
||||
@Override
|
||||
public void remove (K key, HK hashKey) {
|
||||
redisService.deleteCacheMapValue(encode(key), encodeHashKey(hashKey));
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断redis中hashKey是否存在
|
||||
*
|
||||
* @param key redis键
|
||||
* @param hashKey hash键
|
||||
*/
|
||||
@Override
|
||||
public boolean hasKey (K key, HK hashKey) {
|
||||
return redisService.hashKey(encode(key), encodeHashKey(hashKey));
|
||||
}
|
||||
|
||||
/**
|
||||
* 原始数据转编码数据
|
||||
* @param dataMap 原始数据
|
||||
* @return 编码数据
|
||||
*/
|
||||
private Map<String, HV> encodeMap(Map<HK, HV> dataMap){
|
||||
Map<String, HV> encodeDataMap = new HashMap<>();
|
||||
dataMap.forEach((hashKey, HashValue) -> encodeDataMap.put(encodeHashKey(hashKey), HashValue));
|
||||
return encodeDataMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码数据转原始数据
|
||||
* @param encodeDataMap 编码数据
|
||||
* @return 原始数据
|
||||
*/
|
||||
private Map<HK, HV> decodeMap(Map<String, HV> encodeDataMap){
|
||||
Map<HK, HV> dataMap = new HashMap<>();
|
||||
encodeDataMap.forEach((hashKey, hashValue) -> dataMap.put(decodeHashKey(hashKey), hashValue));
|
||||
return dataMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过键获取所有的hash数据
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
public abstract Map<HK, HV> getData(K key);
|
||||
|
||||
/**
|
||||
* 通过缓存键和hash键获取hash值
|
||||
* @param key 缓存键
|
||||
* @param hashKey hash键
|
||||
* @return hash值
|
||||
*/
|
||||
public abstract HV getData(K key, HK hashKey);
|
||||
|
||||
/**
|
||||
* 默认值
|
||||
*/
|
||||
public abstract Map<HK, HV> defaultValue();
|
||||
public abstract HV defaultHashValue();
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.muyu.common.cache.decoration;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 装饰Key
|
||||
* @Date 2024-3-29 下午 03:19
|
||||
*/
|
||||
public interface DecorationKey <K>{
|
||||
|
||||
/**
|
||||
* key前缀
|
||||
* @return key前缀
|
||||
*/
|
||||
public String keyPre();
|
||||
|
||||
|
||||
/**
|
||||
* 编码
|
||||
* @param key ID
|
||||
* @return 键
|
||||
*/
|
||||
public String encode(K key);
|
||||
|
||||
/**
|
||||
* 解码
|
||||
* @param redisKey 数据库键
|
||||
* @return ID
|
||||
*/
|
||||
public K decode(String redisKey);
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
com.muyu.common.kafka.config.KafkaConsumerConfig
|
||||
com.muyu.common.kafka.config.KafkaProviderConfig
|
|
@ -0,0 +1,35 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>cloud-common-caffeine</artifactId>
|
||||
<description>
|
||||
cloud-common-caffeine caffeine缓存模块
|
||||
</description>
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||
<artifactId>caffeine</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,51 @@
|
|||
package com.muyu.common.caffeine.bean;
|
||||
|
||||
|
||||
import com.muyu.common.caffeine.enums.CacheNameEnums;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.caffeine.CaffeineCache;
|
||||
import org.springframework.cache.support.SimpleCacheManager;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Caffeine管理器
|
||||
* @Author: 胡杨
|
||||
* @Name: CaffeineCacheConfig
|
||||
* @Description: Caffeine管理器
|
||||
* @CreatedDate: 2024/9/26 上午11:52
|
||||
* @FilePath: com.muyu.common.caffeine.config
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class CaffeineManager {
|
||||
|
||||
/**
|
||||
* 创建缓存管理器
|
||||
* @return 缓存管理器实例
|
||||
*/
|
||||
@Bean
|
||||
public SimpleCacheManager simpleCacheManager() {
|
||||
SimpleCacheManager cacheManager = new SimpleCacheManager();
|
||||
List<String> cacheNames = CacheNameEnums.getCodes();
|
||||
cacheManager.setCaches(cacheNames.stream()
|
||||
.map(name -> new CaffeineCache(
|
||||
name,
|
||||
Caffeine.newBuilder()
|
||||
.recordStats()
|
||||
.build()))
|
||||
.toList());
|
||||
log.info("缓存管理器初始化完成,缓存分区:{}", cacheNames);
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.muyu.common.caffeine.constents;
|
||||
|
||||
/**
|
||||
* Caffeine常量
|
||||
* @Author: 胡杨
|
||||
* @Name: CaffeineContent
|
||||
* @Description: Caffeine常量
|
||||
* @CreatedDate: 2024/9/26 下午12:06
|
||||
* @FilePath: com.muyu.common.caffeine.constents
|
||||
*/
|
||||
|
||||
public class CaffeineContent {
|
||||
|
||||
public static final String CAR_VIN_KEY = "car:vin";
|
||||
|
||||
public static final String VIN = "vin";
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package com.muyu.common.caffeine.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 缓存分区枚举
|
||||
*
|
||||
* @Author: 胡杨
|
||||
* @Name: CacheNameEnums
|
||||
* @Description: 缓存分区枚举
|
||||
* @CreatedDate: 2024/10/2 上午9:17
|
||||
* @FilePath: com.muyu.common.caffeine.enums
|
||||
*/
|
||||
|
||||
@Getter
|
||||
public enum CacheNameEnums {
|
||||
STORAGE("storage", "持久化"),
|
||||
FAULT("fault", "故障"),
|
||||
FENCE("fence", "围栏"),
|
||||
WARMING("warming", "预警"),
|
||||
REALTIME("realTime", "实时信息");
|
||||
|
||||
private final String code;
|
||||
private final String info;
|
||||
|
||||
CacheNameEnums(String code, String info) {
|
||||
this.code = code;
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 鉴别参数是否是枚举的值
|
||||
*
|
||||
* @param code 需鉴别参数
|
||||
* @return 如果存在返回结果turn, 否则返回false
|
||||
*/
|
||||
public static boolean isCode(String code) {
|
||||
return Arrays.stream(values())
|
||||
.map(CacheNameEnums::getCode)
|
||||
.anyMatch(c -> c.equals(code));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取枚举Value
|
||||
* @param code 编码
|
||||
* @return Value
|
||||
*/
|
||||
public static String getInfo(String code) {
|
||||
return Arrays.stream(values())
|
||||
.filter(c -> c.getCode().equals(code))
|
||||
.map(CacheNameEnums::getInfo)
|
||||
.findFirst()
|
||||
.orElse("");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有code
|
||||
* @return code集合
|
||||
*/
|
||||
public static List<String> getCodes() {
|
||||
return Arrays.stream(values())
|
||||
.map(CacheNameEnums::getCode)
|
||||
.toList();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
package com.muyu.common.caffeine.utils;
|
||||
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import com.muyu.common.caffeine.constents.CaffeineContent;
|
||||
import com.muyu.common.caffeine.enums.CacheNameEnums;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.caffeine.CaffeineCache;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Caffeine缓存工具
|
||||
* @Author: 胡杨
|
||||
* @Name: CaffeineUtils
|
||||
* @Description: 缓存工具类
|
||||
* @CreatedDate: 2024/9/26 下午2:53
|
||||
* @FilePath: com.muyu.common.caffeine
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class CaffeineCacheUtils {
|
||||
@Resource
|
||||
private CacheManager cacheManager;
|
||||
@Resource
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* 车辆上线 - 新增缓存
|
||||
*/
|
||||
public void addCarCache(String vin) {
|
||||
// 从Redis中获取缓存信息
|
||||
for (String name : CacheNameEnums.getCodes()) {
|
||||
String value = redisTemplate.opsForValue().get(name+":"+vin);
|
||||
cacheManager.getCache(name).put(vin, value);
|
||||
log.info("存储缓存, 缓存分区:[{}], 车辆编码:[{}], 存储值:[{}]", name, vin, value);
|
||||
}
|
||||
log.info("车辆编码:{},本地缓存完成...",vin);
|
||||
}
|
||||
|
||||
/**
|
||||
* 车辆下线 - 删除缓存
|
||||
*/
|
||||
public void deleteCarCache(String cacheName) {
|
||||
if (!hasCarVinCache(cacheName,null)) {
|
||||
log.warn("车辆编码:{},本地缓存不存在该车辆信息...", cacheName);
|
||||
return;
|
||||
}
|
||||
cacheManager.getCache(cacheName).invalidate();
|
||||
log.info("车辆编码:{},本地缓存删除完成...", cacheName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取车辆信息缓存
|
||||
*/
|
||||
public Object getCarCache(String cacheName, String key) {
|
||||
if (!hasCarVinCache(cacheName, key)){
|
||||
log.warn("车辆编码:{},本地缓存不存在该车辆信息...",cacheName);
|
||||
return null;
|
||||
}
|
||||
return cacheManager.getCache(cacheName).get(key).get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取车辆信息缓存
|
||||
*/
|
||||
public <T> T getCarCache(String cacheName, String key, Class<T> type) {
|
||||
if (!hasCarVinCache(cacheName,key)){
|
||||
log.warn("车辆编码:{},本地缓存不存在该车辆信息...",cacheName);
|
||||
return null;
|
||||
}
|
||||
return cacheManager.getCache(cacheName).get(key, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存存在与否
|
||||
*/
|
||||
public Boolean hasCarVinCache(String cacheName,String key) {
|
||||
boolean notEmpty = ObjectUtils.isNotEmpty(cacheManager.getCache(cacheName));
|
||||
if (notEmpty && StringUtils.isNotEmpty(key)){
|
||||
return ObjectUtils.isNotEmpty(cacheManager.getCache(cacheName).get(key).get());
|
||||
}
|
||||
return notEmpty;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
com.muyu.common.caffeine.utils.CaffeineCacheUtils
|
||||
com.muyu.common.caffeine.bean.CaffeineManager
|
|
@ -16,11 +16,13 @@
|
|||
</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.yulichang</groupId>
|
||||
<artifactId>mybatis-plus-join-boot-starter</artifactId>
|
||||
<version>1.4.11</version>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Openfeign -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
|
@ -168,6 +170,13 @@
|
|||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
<version>1.2.2</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -46,8 +46,10 @@ public class SecurityConstants {
|
|||
*/
|
||||
public static final String ROLE_PERMISSION = "role_permission";
|
||||
|
||||
|
||||
/**
|
||||
* SAAS请求头的key
|
||||
*/
|
||||
public static final String SAAS_KEY = "ent-code";
|
||||
|
||||
}
|
||||
|
|
|
@ -29,4 +29,5 @@ public class ServiceNameConstants {
|
|||
public static final String ENT_SERVICE = "cloud-ent";
|
||||
|
||||
public static final String SAAS_SERVICE = "cloud-system-saas";
|
||||
|
||||
}
|
||||
|
|
|
@ -81,6 +81,7 @@ public class SecurityContextHolder {
|
|||
THREAD_LOCAL.remove();
|
||||
}
|
||||
|
||||
|
||||
public static String getSaasKey() {
|
||||
return get(SecurityConstants.SAAS_KEY);
|
||||
}
|
||||
|
@ -88,4 +89,5 @@ public class SecurityContextHolder {
|
|||
set(SecurityConstants.SAAS_KEY,saasKey);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
package com.muyu.common.core.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 类型枚举
|
||||
*
|
||||
* @Author: 胡杨
|
||||
* @Name: ClassType
|
||||
* @Description: 类型枚举
|
||||
* @CreatedDate: 2024/9/29 上午9:28
|
||||
* @FilePath: com.muyu.common.core.enums
|
||||
*/
|
||||
|
||||
@Getter
|
||||
public enum ClassType {
|
||||
BYTE("byte", byte.class),
|
||||
SHORT("short", short.class),
|
||||
INT("int", int.class),
|
||||
LONG("long", long.class),
|
||||
FLOAT("float", float.class),
|
||||
DOUBLE("double", double.class),
|
||||
BOOLEAN("boolean", boolean.class),
|
||||
CHAR("char", char.class),
|
||||
STRING("String", String.class),
|
||||
SET("Set", Set.class),
|
||||
MAP("Map", Map.class),
|
||||
LIST("List", List.class);
|
||||
|
||||
private final String code;
|
||||
private final Class<?> info;
|
||||
|
||||
ClassType(String code, Class<?> info) {
|
||||
this.code = code;
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 鉴别参数是否是枚举的值
|
||||
*
|
||||
* @param code 需鉴别参数
|
||||
* @return 如果存在返回结果turn, 否则返回false
|
||||
*/
|
||||
public static boolean isCode(String code) {
|
||||
return Arrays.stream(values())
|
||||
.map(ClassType::getCode)
|
||||
.anyMatch(c -> c.equals(code));
|
||||
}
|
||||
|
||||
|
||||
public static Class<?> getInfo(String code) {
|
||||
return Arrays.stream(values())
|
||||
.filter(c -> c.getCode().equals(code))
|
||||
.findFirst()
|
||||
.map(ClassType::getInfo)
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
|
@ -163,6 +163,7 @@ public class JwtUtils {
|
|||
return Convert.toStr(claims.get(key), "");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据身份信息获取SAASKey
|
||||
*
|
||||
|
@ -173,4 +174,5 @@ public class JwtUtils {
|
|||
public static String getSaasKey(Claims claims) {
|
||||
return getValue(claims, SecurityConstants.SAAS_KEY);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -58,7 +58,8 @@ public class IpUtils {
|
|||
ip = request.getRemoteAddr();
|
||||
}
|
||||
|
||||
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : getMultistageReverseProxyIp(ip);
|
||||
|
||||
return "0:0:0:0:0:0:0:1".equals(ip) ? "106.54.193.225" : getMultistageReverseProxyIp(ip);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -70,7 +71,10 @@ public class IpUtils {
|
|||
*/
|
||||
public static boolean internalIp (String ip) {
|
||||
byte[] addr = textToNumericFormatV4(ip);
|
||||
return internalIp(addr) || "127.0.0.1".equals(ip);
|
||||
|
||||
return internalIp(addr) || "106.54.193.225".equals(ip);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -197,7 +201,8 @@ public class IpUtils {
|
|||
return InetAddress.getLocalHost().getHostAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
}
|
||||
return "127.0.0.1";
|
||||
|
||||
return "106.54.193.225";
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>cloud-common-iotdb</artifactId>
|
||||
<description>
|
||||
cloud-common-iotdb 时序数据库模块
|
||||
</description>
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.iotdb</groupId>
|
||||
<artifactId>iotdb-session</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,53 @@
|
|||
package com.muyu.common.iotdb.config;
|
||||
|
||||
import org.apache.iotdb.rpc.IoTDBConnectionException;
|
||||
import org.apache.iotdb.rpc.StatementExecutionException;
|
||||
import org.apache.iotdb.session.Session;
|
||||
import org.apache.iotdb.session.pool.SessionPool;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 时序数据库配置
|
||||
*
|
||||
* @Author: 胡杨
|
||||
* @Name: IotDBConfig
|
||||
* @Description: 时序数据库配置
|
||||
* @CreatedDate: 2024/9/29 下午9:30
|
||||
* @FilePath: com.muyu.data.processing.config
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
public class IotDBSessionConfig {
|
||||
|
||||
@Value("${spring.iotdb.ip}")
|
||||
private String ip;
|
||||
|
||||
@Value("${spring.iotdb.port}")
|
||||
private int port;
|
||||
|
||||
@Value("${spring.iotdb.user}")
|
||||
private String user;
|
||||
|
||||
@Value("${spring.iotdb.password}")
|
||||
private String password;
|
||||
|
||||
@Value("${spring.iotdb.fetchSize}")
|
||||
private int fetchSize;
|
||||
|
||||
private static SessionPool sessionPool;
|
||||
@Bean
|
||||
public SessionPool getSessionPool(){
|
||||
if (sessionPool == null) {
|
||||
sessionPool = new SessionPool(ip, port, user, password, fetchSize);
|
||||
try {
|
||||
sessionPool.setTimeZone("+08:00");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return sessionPool;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
com.muyu.common.iotdb.config.IotDBSessionConfig
|
|
@ -0,0 +1,37 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>cloud-common-kafka</artifactId>
|
||||
<description>
|
||||
cloud-common-kafka kafka中间件模块
|
||||
</description>
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.kafka</groupId>
|
||||
<artifactId>kafka-clients</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,54 @@
|
|||
package com.muyu.common.kafka.config;
|
||||
|
||||
import com.muyu.common.kafka.constants.KafkaConstants;
|
||||
import org.apache.kafka.clients.consumer.KafkaConsumer;
|
||||
import org.apache.kafka.common.serialization.Deserializer;
|
||||
import org.apache.kafka.common.serialization.StringDeserializer;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* kafka 消息的消费者 配置类
|
||||
*/
|
||||
@Configuration
|
||||
public class KafkaConsumerConfig {
|
||||
|
||||
@Bean
|
||||
public KafkaConsumer kafkaConsumer() {
|
||||
Map<String, Object> configs = new HashMap<>();
|
||||
//kafka服务端的IP和端口,格式:(ip:port)
|
||||
configs.put("bootstrap.servers", "47.116.173.119:9092");
|
||||
//开启consumer的偏移量(offset)自动提交到Kafka
|
||||
configs.put("enable.auto.commit", true);
|
||||
//consumer的偏移量(offset) 自动提交的时间间隔,单位毫秒
|
||||
configs.put("auto.commit.interval", 5000);
|
||||
//在Kafka中没有初始化偏移量或者当前偏移量不存在情况
|
||||
//earliest, 在偏移量无效的情况下, 自动重置为最早的偏移量
|
||||
//latest, 在偏移量无效的情况下, 自动重置为最新的偏移量
|
||||
//none, 在偏移量无效的情况下, 抛出异常.
|
||||
configs.put("auto.offset.reset", "latest");
|
||||
//请求阻塞的最大时间(毫秒)
|
||||
configs.put("fetch.max.wait", 500);
|
||||
//请求应答的最小字节数
|
||||
configs.put("fetch.min.size", 1);
|
||||
//心跳间隔时间(毫秒)
|
||||
configs.put("heartbeat-interval", 3000);
|
||||
//一次调用poll返回的最大记录条数
|
||||
configs.put("max.poll.records", 500);
|
||||
//指定消费组
|
||||
configs.put("group.id", KafkaConstants.KafkaGrop);
|
||||
//指定key使用的反序列化类
|
||||
Deserializer keyDeserializer = new StringDeserializer();
|
||||
//指定value使用的反序列化类
|
||||
Deserializer valueDeserializer = new StringDeserializer();
|
||||
//创建Kafka消费者
|
||||
KafkaConsumer kafkaConsumer = new KafkaConsumer(configs, keyDeserializer, valueDeserializer);
|
||||
return kafkaConsumer;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package com.muyu.common.kafka.config;
|
||||
|
||||
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||
import org.apache.kafka.common.serialization.Serializer;
|
||||
import org.apache.kafka.common.serialization.StringSerializer;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* kafka 消息的生产者 配置类
|
||||
*/
|
||||
@Configuration
|
||||
public class KafkaProviderConfig {
|
||||
|
||||
@Bean
|
||||
public KafkaProducer kafkaProducer() {
|
||||
Map<String, Object> configs = new HashMap<>();
|
||||
//#kafka服务端的IP和端口,格式:(ip:port)
|
||||
configs.put("bootstrap.servers", "47.116.173.119:9092");
|
||||
//客户端发送服务端失败的重试次数
|
||||
configs.put("retries", 2);
|
||||
//多个记录被发送到同一个分区时,生产者将尝试将记录一起批处理成更少的请求.
|
||||
//此设置有助于提高客户端和服务器的性能,配置控制默认批量大小(以字节为单位)
|
||||
configs.put("batch.size", 16384);
|
||||
//生产者可用于缓冲等待发送到服务器的记录的总内存字节数(以字节为单位)
|
||||
configs.put("buffer-memory", 33554432);
|
||||
//生产者producer要求leader节点在考虑完成请求之前收到的确认数,用于控制发送记录在服务端的持久化
|
||||
//acks=0,设置为0,则生产者producer将不会等待来自服务器的任何确认.该记录将立即添加到套接字(socket)缓冲区并视为已发送.在这种情况下,无法保证服务器已收到记录,并且重试配置(retries)将不会生效(因为客户端通常不会知道任何故障),每条记录返回的偏移量始终设置为-1.
|
||||
//acks=1,设置为1,leader节点会把记录写入本地日志,不需要等待所有follower节点完全确认就会立即应答producer.在这种情况下,在follower节点复制前,leader节点确认记录后立即失败的话,记录将会丢失.
|
||||
//acks=all,acks=-1,leader节点将等待所有同步复制副本完成再确认记录,这保证了只要至少有一个同步复制副本存活,记录就不会丢失.
|
||||
configs.put("acks", "-1");
|
||||
//指定key使用的序列化类
|
||||
Serializer keySerializer = new StringSerializer();
|
||||
//指定value使用的序列化类
|
||||
Serializer valueSerializer = new StringSerializer();
|
||||
//创建Kafka生产者
|
||||
KafkaProducer kafkaProducer = new KafkaProducer(configs, keySerializer, valueSerializer);
|
||||
return kafkaProducer;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.muyu.common.kafka.constants;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @date: 2024/7/10
|
||||
* @Description: kafka常量
|
||||
* @Version 1.0.0
|
||||
*/
|
||||
public class KafkaConstants {
|
||||
|
||||
public final static String KafkaTopic = "kafka_topic";
|
||||
|
||||
public final static String KafkaGrop = "kafka_grop";
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
com.muyu.common.kafka.config.KafkaConsumerConfig
|
||||
com.muyu.common.kafka.config.KafkaProviderConfig
|
|
@ -18,7 +18,11 @@ import org.aspectj.lang.annotation.Aspect;
|
|||
import org.aspectj.lang.annotation.Before;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import org.springframework.core.NamedThreadLocal;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
@ -48,7 +52,11 @@ public class LogAspect {
|
|||
*/
|
||||
private static final ThreadLocal<Long> TIME_THREADLOCAL = new NamedThreadLocal<Long>("Cost Time");
|
||||
|
||||
|
||||
@Resource
|
||||
|
||||
@Autowired
|
||||
|
||||
private AsyncLogService asyncLogService;
|
||||
|
||||
/**
|
||||
|
@ -82,7 +90,7 @@ public class LogAspect {
|
|||
|
||||
protected void handleLog (final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult) {
|
||||
try {
|
||||
// *========数据库日志=========*//
|
||||
// *=数据库日志==*//
|
||||
SysOperLog operLog = new SysOperLog();
|
||||
operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
|
||||
// 请求的地址
|
||||
|
|
|
@ -4,6 +4,7 @@ import com.muyu.common.core.constant.SecurityConstants;
|
|||
import com.muyu.common.system.remote.RemoteLogService;
|
||||
import com.muyu.common.system.domain.SysOperLog;
|
||||
import javax.annotation.Resource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
@ -14,7 +15,7 @@ import org.springframework.stereotype.Service;
|
|||
*/
|
||||
@Service
|
||||
public class AsyncLogService {
|
||||
@Resource
|
||||
@Autowired
|
||||
private RemoteLogService remoteLogService;
|
||||
|
||||
/**
|
||||
|
|
|
@ -10,7 +10,9 @@
|
|||
</parent>
|
||||
|
||||
<artifactId>cloud-common-rabbit</artifactId>
|
||||
|
||||
<description>
|
||||
cloud-common-rabbit rabbit中间件模块
|
||||
</description>
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
|
@ -32,4 +34,4 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
package com.muyu.common.rabbit.callback;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.rabbit.connection.CorrelationData;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Lenovo
|
||||
* @Description 消息发送到broker确认回调
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class ConfirmCallback implements RabbitTemplate.ConfirmCallback {
|
||||
|
||||
@Resource
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
this.rabbitTemplate.setConfirmCallback(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 回调确认方法(消息发送之后 消息无论发送成功还是失败都会执行这个回调方法)
|
||||
* @param correlationData 回调的相关数据
|
||||
* @param ack 确认结果(true表示消息已经被broker接收,false表示消息未被broker接收)
|
||||
* @param cause 失败原因(当ack为false时,表示拒绝接收消息的原因;当ack为true时,该值为空)
|
||||
*/
|
||||
@Override
|
||||
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
|
||||
if (ack) {
|
||||
log.info("消息发送到broker成功!");
|
||||
} else {
|
||||
log.info("消息发送到broker失败,失败原因:{}", cause);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.muyu.common.rabbit.callback;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.core.ReturnedMessage;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Lenovo
|
||||
* @Description 消息发送失败时回调
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class ReturnsCallback implements RabbitTemplate.ReturnsCallback {
|
||||
|
||||
@Resource
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
rabbitTemplate.setReturnsCallback(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息发送到队列失败时执行
|
||||
* @param returnedMessage 返回的消息和元数据
|
||||
*/
|
||||
@Override
|
||||
public void returnedMessage(ReturnedMessage returnedMessage) {
|
||||
log.info("消息:{}被交换机:{}回退!回退原因:{}", returnedMessage.getMessage().toString(),
|
||||
returnedMessage.getExchange(), returnedMessage.getReplyText());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package com.muyu.common.rabbit.config;
|
||||
|
||||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.core.RabbitAdmin;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author Lenovo
|
||||
* @Description RabbitMQ连接和管理功能配置
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
public class RabbitAdminConfig {
|
||||
|
||||
/**
|
||||
* RabbitMQ服务器的主机地址
|
||||
*/
|
||||
@Value("${spring.rabbitmq.host}")
|
||||
private String host;
|
||||
|
||||
/**
|
||||
* RabbitMQ的用户名
|
||||
*/
|
||||
@Value("${spring.rabbitmq.username}")
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* RabbitMQ的密码
|
||||
*/
|
||||
@Value("${spring.rabbitmq.password}")
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* RabbitMQ的虚拟主机
|
||||
*/
|
||||
@Value("${spring.rabbitmq.virtualhost}")
|
||||
private String virtualhost;
|
||||
|
||||
/**
|
||||
* 创建并初始化RabbitAdmin实例
|
||||
*
|
||||
* @return RabbitAdmin 实例
|
||||
*/
|
||||
@Bean
|
||||
public RabbitAdmin rabbitAdmin() {
|
||||
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory());
|
||||
rabbitAdmin.setAutoStartup(true);
|
||||
return rabbitAdmin;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建RabbitMQ连接工厂
|
||||
*
|
||||
* @return ConnectionFactory 实例
|
||||
*/
|
||||
@Bean
|
||||
public ConnectionFactory connectionFactory() {
|
||||
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
|
||||
connectionFactory.setAddresses(host);
|
||||
connectionFactory.setUsername(username);
|
||||
connectionFactory.setPassword(password);
|
||||
connectionFactory.setVirtualHost(virtualhost);
|
||||
|
||||
// 配置发送确认回调时,次配置必须配置,否则即使在RabbitTemplate配置了ConfirmCallback也不会生效
|
||||
connectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);
|
||||
connectionFactory.setPublisherReturns(true);
|
||||
return connectionFactory;
|
||||
}
|
||||
}
|
|
@ -1,28 +1,38 @@
|
|||
package com.muyu.common.rabbit;
|
||||
package com.muyu.common.rabbit.config;
|
||||
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
|
||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
|
||||
import javax.annotation.Resource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
|
||||
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
|
||||
|
||||
/**
|
||||
* @author Lenovo
|
||||
* @Description rabbitMQ的监听器配置
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
public class RabbitListenerConfigurer implements org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer {
|
||||
public class RabbitListenerConfig implements RabbitListenerConfigurer {
|
||||
|
||||
static {
|
||||
// 设置为信任所有类型的反序列化,确保消息能够正确反序列化
|
||||
System.setProperty("spring.amqp.deserialization.trust.all", "true");
|
||||
}
|
||||
|
||||
//以下配置RabbitMQ消息服务
|
||||
@Resource
|
||||
/**
|
||||
* RabbitMQ连接工厂,用于创建连接
|
||||
*/
|
||||
@Autowired
|
||||
public ConnectionFactory connectionFactory;
|
||||
|
||||
|
||||
/**
|
||||
* 处理器方法工厂
|
||||
* @return
|
||||
* 创建处理器方法工厂的bean
|
||||
*
|
||||
* @return DefaultMessageHandlerMethodFactory 实例,用于处理消息的转换和方法调用
|
||||
*/
|
||||
@Bean
|
||||
public DefaultMessageHandlerMethodFactory handlerMethodFactory() {
|
||||
|
@ -32,8 +42,14 @@ public class RabbitListenerConfigurer implements org.springframework.amqp.rabbit
|
|||
return factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置RabbitMQ监听器的消息处理方法工厂。
|
||||
*
|
||||
* @param rabbitListenerEndpointRegistrar 实例,用于注册监听器的配置
|
||||
*/
|
||||
@Override
|
||||
public void configureRabbitListeners(RabbitListenerEndpointRegistrar rabbitListenerEndpointRegistrar) {
|
||||
// 注册自定义的消息处理方法工厂
|
||||
rabbitListenerEndpointRegistrar.setMessageHandlerMethodFactory(handlerMethodFactory());
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
|
||||
package com.muyu.common.rabbit.config;
|
||||
|
||||
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
|
||||
import org.springframework.amqp.support.converter.MessageConverter;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author Lenovo
|
||||
* @Description rabbitMQ消息转换器配置
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
public class RabbitMQMessageConverterConfig {
|
||||
|
||||
/**
|
||||
* 消息转换配置
|
||||
*
|
||||
* @return 消息转换器
|
||||
*/
|
||||
@Bean
|
||||
public MessageConverter jsonMessageConverter() {
|
||||
return new Jackson2JsonMessageConverter();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.muyu.common.rabbit.constants;
|
||||
|
||||
/**
|
||||
* rabbit常量
|
||||
* @Author: 胡杨
|
||||
* @date: 2024/7/10
|
||||
* @Description: rabbit常量
|
||||
* @Version 1.0.0
|
||||
*/
|
||||
public class RabbitConstants {
|
||||
|
||||
public final static String GO_ONLINE_QUEUE= "GoOnline";
|
||||
|
||||
public final static String DOWNLINE_QUEUE= "Downline";
|
||||
}
|
|
@ -1 +1,3 @@
|
|||
com.muyu.common.rabbit.RabbitListenerConfigurer
|
||||
com.muyu.common.rabbit.config.RabbitListenerConfig
|
||||
com.muyu.common.rabbit.config.RabbitAdminConfig
|
||||
com.muyu.common.rabbit.config.RabbitMQMessageConverterConfig
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
package com.muyu.common.redis.service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.BoundSetOperations;
|
||||
import org.springframework.data.redis.core.HashOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
@ -18,7 +20,11 @@ import java.util.concurrent.TimeUnit;
|
|||
@SuppressWarnings(value = {"unchecked", "rawtypes"})
|
||||
@Component
|
||||
public class RedisService {
|
||||
|
||||
@Resource
|
||||
|
||||
@Autowired
|
||||
|
||||
public RedisTemplate redisTemplate;
|
||||
|
||||
/**
|
||||
|
@ -229,7 +235,7 @@ public class RedisService {
|
|||
*
|
||||
* @return Hash对象集合
|
||||
*/
|
||||
public <T> List<T> getMultiCacheMapValue (final String key, final Collection<Object> hKeys) {
|
||||
public <T> List<T> getMultiCacheMapValue (final String key, final Collection<String> hKeys) {
|
||||
return redisTemplate.opsForHash().multiGet(key, hKeys);
|
||||
}
|
||||
|
||||
|
@ -255,4 +261,33 @@ public class RedisService {
|
|||
public Collection<String> keys (final String pattern) {
|
||||
return redisTemplate.keys(pattern);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断redis中hashKey是否存在
|
||||
* @param key redis键
|
||||
* @param hashKey hash键
|
||||
*/
|
||||
public boolean hashKey(final String key, final String hashKey){
|
||||
return this.redisTemplate.opsForHash().hasKey(key, hashKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 减少序列值
|
||||
* @param key key
|
||||
* @param number 值
|
||||
* @return 操作后的值
|
||||
*/
|
||||
public Long decrement (final String key, Long number) {
|
||||
return redisTemplate.opsForValue().decrement(key,number);
|
||||
}
|
||||
/**
|
||||
* 增加序列值
|
||||
* @param key key
|
||||
* @param number 值
|
||||
* @return 操作后的值
|
||||
*/
|
||||
public Long increment (final String key, Long number) {
|
||||
return redisTemplate.opsForValue().increment(key,number);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,9 @@
|
|||
</parent>
|
||||
|
||||
<artifactId>cloud-common-saas</artifactId>
|
||||
|
||||
<description>
|
||||
cloud-common-saas saas数据源切换模块
|
||||
</description>
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
|
|
|
@ -71,8 +71,10 @@ public class ManyDataSource implements ApplicationRunner{
|
|||
Objects.requireNonNull(dataSourceInfoList())
|
||||
.stream()
|
||||
.map(DataSourceInfo::hostAndPortBuild)
|
||||
.forEach(dataSourceInfo -> {
|
||||
dataSourceMap.put(dataSourceInfo.getKey(), druidDataSourceFactory.create(dataSourceInfo));
|
||||
.map(druidDataSourceFactory::create)
|
||||
.filter(Objects::nonNull)
|
||||
.forEach( druidDataSource -> {
|
||||
dataSourceMap.put(druidDataSource.getName(), druidDataSource);
|
||||
});
|
||||
//设置动态数据源
|
||||
DynamicDataSource dynamicDataSource = new DynamicDataSource();
|
||||
|
|
|
@ -13,7 +13,7 @@ public class DatasourceContent {
|
|||
|
||||
public final static String PASSWORD = "bawei2112A";
|
||||
|
||||
public final static String IP = "127.0.0.1";
|
||||
public final static String IP = "106.54.193.225";
|
||||
|
||||
public final static Integer PORT = 3306;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.muyu.cloud.common.many.datasource.factory;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.muyu.cloud.common.many.datasource.domain.model.DataSourceInfo;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
@ -23,6 +24,7 @@ public class DruidDataSourceFactory {
|
|||
*/
|
||||
public DruidDataSource create(DataSourceInfo dataSourceInfo) {
|
||||
DruidDataSource druidDataSource = new DruidDataSource();
|
||||
druidDataSource.setName(dataSourceInfo.getKey());
|
||||
druidDataSource.setUrl(dataSourceInfo.getUrl());
|
||||
druidDataSource.setConnectTimeout(10000);
|
||||
druidDataSource.setMaxWait(60000);
|
||||
|
@ -31,8 +33,9 @@ public class DruidDataSourceFactory {
|
|||
druidDataSource.setBreakAfterAcquireFailure(true);
|
||||
druidDataSource.setConnectionErrorRetryAttempts(0);
|
||||
try {
|
||||
druidDataSource.getConnection(2000);
|
||||
DruidPooledConnection connection = druidDataSource.getConnection(2000);
|
||||
log.info("{} -> 数据源连接成功", dataSourceInfo.getKey());
|
||||
connection.close();
|
||||
return druidDataSource;
|
||||
} catch (SQLException throwables) {
|
||||
log.error("数据源 {} 连接失败,用户名:{},密码 {}, 原因:{}",dataSourceInfo.getUrl(),dataSourceInfo.getUserName(),dataSourceInfo.getPassword(), throwables);
|
||||
|
|
|
@ -28,7 +28,10 @@ public class HeaderInterceptor implements AsyncHandlerInterceptor {
|
|||
}
|
||||
|
||||
SecurityContextHolder.setUserId(ServletUtils.getHeader(request, SecurityConstants.DETAILS_USER_ID));
|
||||
|
||||
SecurityContextHolder.setSaasKey(ServletUtils.getHeader(request, SecurityConstants.SAAS_KEY));
|
||||
|
||||
|
||||
SecurityContextHolder.setUserName(ServletUtils.getHeader(request, SecurityConstants.DETAILS_USERNAME));
|
||||
SecurityContextHolder.setUserKey(ServletUtils.getHeader(request, SecurityConstants.USER_KEY));
|
||||
|
||||
|
|
|
@ -30,10 +30,13 @@ public class SecurityUtils {
|
|||
return SecurityContextHolder.getUserName();
|
||||
}
|
||||
|
||||
|
||||
public static String getSaasKey () {
|
||||
return SecurityContextHolder.getSaasKey();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取用户key
|
||||
*/
|
||||
|
|
|
@ -65,4 +65,7 @@ public class LoginUser implements Serializable {
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -34,8 +34,11 @@ public class SysDept extends BaseEntity {
|
|||
*/
|
||||
private Long parentId;
|
||||
|
||||
|
||||
private String firmCode;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 祖级列表
|
||||
*/
|
||||
|
|
|
@ -36,8 +36,11 @@ public class SysRole extends BaseEntity {
|
|||
@Excel(name = "角色名称")
|
||||
private String roleName;
|
||||
|
||||
|
||||
private String firmCode;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 角色权限
|
||||
*/
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.muyu.common.system.remote;
|
|||
import com.muyu.common.core.constant.SecurityConstants;
|
||||
import com.muyu.common.core.constant.ServiceNameConstants;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
|
||||
import com.muyu.common.system.domain.*;
|
||||
import com.muyu.common.system.remote.factory.RemoteUserFallbackFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
|
@ -11,6 +12,14 @@ import org.springframework.web.bind.annotation.*;
|
|||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
import com.muyu.common.system.domain.SysUser;
|
||||
import com.muyu.common.system.remote.factory.RemoteUserFallbackFactory;
|
||||
import com.muyu.common.system.domain.LoginUser;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
|
||||
/**
|
||||
* 用户服务
|
||||
*
|
||||
|
@ -19,6 +28,19 @@ import java.util.Set;
|
|||
@FeignClient(contextId = "remoteUserService", value = ServiceNameConstants.SYSTEM_SERVICE, fallbackFactory = RemoteUserFallbackFactory.class)
|
||||
public interface RemoteUserService {
|
||||
|
||||
|
||||
/**
|
||||
* 通过用户名查询用户信息
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param source 请求来源
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
@GetMapping("/user/info/{username}")
|
||||
public Result<LoginUser> getUserInfo (@PathVariable("username") String username, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||
|
||||
|
||||
/**
|
||||
* 注册用户信息
|
||||
*
|
||||
|
@ -30,6 +52,7 @@ public interface RemoteUserService {
|
|||
@PostMapping("/user/register")
|
||||
public Result<Boolean> registerUserInfo (@RequestBody SysUser sysUser, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||
|
||||
|
||||
@GetMapping("/user/companyList")
|
||||
public Result<List<SysUser>> companyList ();
|
||||
|
||||
|
@ -98,4 +121,6 @@ public interface RemoteUserService {
|
|||
*/
|
||||
@PostMapping("/user")
|
||||
public Result addUser (@RequestBody SysUser user);
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,17 +1,26 @@
|
|||
package com.muyu.common.system.remote.factory;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
|
||||
import com.muyu.common.core.web.page.TableDataInfo;
|
||||
import com.muyu.common.system.domain.*;
|
||||
import com.muyu.common.system.remote.RemoteUserService;
|
||||
|
||||
import com.muyu.common.system.remote.RemoteUserService;
|
||||
import com.muyu.common.system.domain.SysUser;
|
||||
import com.muyu.common.system.domain.LoginUser;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 用户服务降级处理
|
||||
*
|
||||
|
@ -26,11 +35,19 @@ public class RemoteUserFallbackFactory implements FallbackFactory<RemoteUserServ
|
|||
log.error("用户服务调用失败:{}", throwable.getMessage());
|
||||
return new RemoteUserService() {
|
||||
|
||||
|
||||
@Override
|
||||
public Result<LoginUser> getUserInfo (String username, String source) {
|
||||
return Result.error("获取用户失败:" + throwable.getMessage());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Result<Boolean> registerUserInfo (SysUser sysUser, String source) {
|
||||
return Result.error("注册用户失败:" + throwable.getMessage());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Result<List<SysUser>> companyList() {
|
||||
return Result.error("获取企业列表失败:" + throwable.getMessage());
|
||||
|
@ -76,6 +93,8 @@ public class RemoteUserFallbackFactory implements FallbackFactory<RemoteUserServ
|
|||
return Result.error(throwable);
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
com.muyu.common.system.remote.factory.RemoteUserFallbackFactory
|
||||
com.muyu.common.system.remote.factory.RemoteLogFallbackFactory
|
||||
com.muyu.common.system.remote.factory.RemoteFileFallbackFactory
|
||||
|
||||
com.muyu.common.system.remote.factory.RemoteSaasFallbackFactory
|
||||
|
||||
|
||||
|
|
|
@ -7,9 +7,9 @@ import org.springframework.context.annotation.Bean;
|
|||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Log4j2
|
||||
//@Component
|
||||
@Component
|
||||
public class XXLJobConfig {
|
||||
// @Bean
|
||||
@Bean
|
||||
public XxlJobSpringExecutor xxlJobExecutor(XxlJobProperties xxlJobProperties) {
|
||||
if (StringUtils.isEmpty(xxlJobProperties.getAdminAddresses())){
|
||||
throw new RuntimeException("请在bootstrap.yml当中配置shared-configs项,xxl-job共享配置[application-xxl-config]");
|
||||
|
|
|
@ -21,6 +21,10 @@
|
|||
<module>cloud-common-xxl</module>
|
||||
<module>cloud-common-rabbit</module>
|
||||
<module>cloud-common-saas</module>
|
||||
<module>cloud-common-cache</module>
|
||||
<module>cloud-common-caffeine</module>
|
||||
<module>cloud-common-iotdb</module>
|
||||
<module>cloud-common-kafka</module>
|
||||
</modules>
|
||||
|
||||
<artifactId>cloud-common</artifactId>
|
||||
|
|
|
@ -81,6 +81,8 @@
|
|||
<artifactId>knife4j-gateway-spring-boot-starter</artifactId>
|
||||
<version>4.5.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -13,6 +13,9 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
|||
public class CloudGatewayApplication {
|
||||
public static void main (String[] args) {
|
||||
SpringApplication.run(CloudGatewayApplication.class, args);
|
||||
|
||||
System.out.println("CloudGateway 模块启动成功!");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
package com.muyu.gateway.config;
|
||||
|
||||
import com.muyu.gateway.handler.ValidateCodeHandler;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.MediaType;
|
||||
|
@ -16,7 +20,11 @@ import org.springframework.web.reactive.function.server.RouterFunctions;
|
|||
*/
|
||||
@Configuration
|
||||
public class RouterFunctionConfiguration {
|
||||
|
||||
@Resource
|
||||
|
||||
@Autowired
|
||||
|
||||
private ValidateCodeHandler validateCodeHandler;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
|
|
|
@ -177,7 +177,7 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
|||
};
|
||||
}
|
||||
|
||||
// ========== 参考 ModifyRequestBodyGatewayFilterFactory 中的方法 ==========
|
||||
// === 参考 ModifyRequestBodyGatewayFilterFactory 中的方法 ===
|
||||
|
||||
/**
|
||||
* 请求装饰器,支持重新计算 headers、body 缓存
|
||||
|
@ -210,7 +210,7 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
|||
};
|
||||
}
|
||||
|
||||
// ========== 参考 ModifyResponseBodyGatewayFilterFactory 中的方法 ==========
|
||||
// === 参考 ModifyResponseBodyGatewayFilterFactory 中的方法 ===
|
||||
|
||||
private byte[] readContent(List<? extends DataBuffer> dataBuffers) {
|
||||
// 合并多个流集合,解决返回体分段传输
|
||||
|
|
|
@ -4,7 +4,11 @@ import com.muyu.common.core.utils.StringUtils;
|
|||
import com.muyu.common.core.utils.html.EscapeUtil;
|
||||
import com.muyu.gateway.config.properties.XssProperties;
|
||||
import io.netty.buffer.ByteBufAllocator;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
|
@ -31,7 +35,11 @@ import java.nio.charset.StandardCharsets;
|
|||
@ConditionalOnProperty(value = "security.xss.enabled", havingValue = "true")
|
||||
public class XssFilter implements GlobalFilter, Ordered {
|
||||
// 跨站脚本的 xss 配置,nacos自行添加
|
||||
|
||||
@Resource
|
||||
|
||||
@Autowired
|
||||
|
||||
private XssProperties xss;
|
||||
|
||||
@Override
|
||||
|
|
|
@ -3,7 +3,11 @@ package com.muyu.gateway.handler;
|
|||
import com.muyu.common.core.exception.CaptchaException;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.gateway.service.ValidateCodeService;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
|
@ -21,7 +25,11 @@ import java.io.IOException;
|
|||
*/
|
||||
@Component
|
||||
public class ValidateCodeHandler implements HandlerFunction<ServerResponse> {
|
||||
|
||||
@Resource
|
||||
|
||||
@Autowired
|
||||
|
||||
private ValidateCodeService validateCodeService;
|
||||
|
||||
@Override
|
||||
|
|
|
@ -12,7 +12,7 @@ import com.muyu.common.redis.service.RedisService;
|
|||
import com.muyu.gateway.config.properties.CaptchaProperties;
|
||||
import com.muyu.gateway.model.resp.CaptchaCodeResp;
|
||||
import com.muyu.gateway.service.ValidateCodeService;
|
||||
import javax.annotation.Resource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.FastByteArrayOutputStream;
|
||||
|
||||
|
@ -35,10 +35,10 @@ public class ValidateCodeServiceImpl implements ValidateCodeService {
|
|||
@Resource(name = "captchaProducerMath")
|
||||
private Producer captchaProducerMath;
|
||||
|
||||
@Resource
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
@Resource
|
||||
@Autowired
|
||||
private CaptchaProperties captchaProperties;
|
||||
|
||||
/**
|
||||
|
|
|
@ -4,6 +4,11 @@ import cn.hutool.core.net.NetUtil;
|
|||
import cn.hutool.core.util.ArrayUtil;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.cloud.gateway.route.Route;
|
||||
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
|
||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
|
@ -16,6 +21,11 @@ import reactor.core.publisher.Mono;
|
|||
|
||||
/**
|
||||
* Web 工具类
|
||||
|
||||
|
||||
*
|
||||
*
|
||||
|
||||
*/
|
||||
@Log4j2
|
||||
public class WebFrameworkUtils {
|
||||
|
@ -68,6 +78,11 @@ public class WebFrameworkUtils {
|
|||
|
||||
/**
|
||||
* 获得客户端 IP
|
||||
|
||||
|
||||
*
|
||||
*
|
||||
|
||||
* @param exchange 请求
|
||||
* @param otherHeaderNames 其它 header 名字的数组
|
||||
* @return 客户端 IP
|
||||
|
@ -85,6 +100,10 @@ public class WebFrameworkUtils {
|
|||
return NetUtil.getMultistageReverseProxyIp(ip);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 方式二,通过 remoteAddress 获取
|
||||
if (exchange.getRequest().getRemoteAddress() == null) {
|
||||
return null;
|
||||
|
@ -95,6 +114,10 @@ public class WebFrameworkUtils {
|
|||
|
||||
/**
|
||||
* 获得请求匹配的 Route 路由
|
||||
|
||||
|
||||
*
|
||||
|
||||
* @param exchange 请求
|
||||
* @return 路由
|
||||
*/
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
# Tomcat
|
||||
server:
|
||||
port: 8080
|
||||
port: 8081
|
||||
|
||||
# nacos线上地址
|
||||
nacos:
|
||||
addr: 49.235.136.60:8848
|
||||
addr: 47.116.173.119:8848
|
||||
user-name: nacos
|
||||
password: nacos
|
||||
namespace: wyh
|
||||
namespace: oneone
|
||||
|
||||
# Spring
|
||||
spring:
|
||||
|
|
|
@ -0,0 +1,114 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-modules</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>cloud-modules-car-gateway</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-security</artifactId>
|
||||
</dependency>
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos Config -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Sentinel -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringBoot Actuator -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Mysql Connector -->
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MuYu Common DataSource -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-datasource</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MuYu Common DataScope -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-datascope</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 接口模块 -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-api-doc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-rabbit</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- http协议 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpcore</artifactId>
|
||||
<version>4.4.12</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.13</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>ecs20140526</artifactId>
|
||||
<version>5.1.8</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>tea-openapi</artifactId>
|
||||
<version>0.3.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>tea-console</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>tea-util</artifactId>
|
||||
<version>0.2.21</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,41 @@
|
|||
package com.muyu.cargateway.Aliyun;
|
||||
|
||||
import com.aliyun.ecs20140526.Client;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.muyu.cargateway.config.AliProperties;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author Lenovo
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-10-07-18:40
|
||||
* @ Version:1.0
|
||||
* @ Description:Ali客户端
|
||||
*/
|
||||
@Configuration
|
||||
public class AliYunConfig {
|
||||
|
||||
@Autowired
|
||||
private AliProperties aliProperties;
|
||||
|
||||
@Bean
|
||||
public Client createClient() {
|
||||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||
Config config = new Config()
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||
.setAccessKeyId(aliProperties.getAccessKeyId())
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||
.setAccessKeySecret(aliProperties.getAccessKeySecret());
|
||||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||
config.endpoint = aliProperties.getEndpoint();
|
||||
try {
|
||||
return new Client(config);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,199 @@
|
|||
package com.muyu.cargateway.Aliyun.service;
|
||||
|
||||
import com.aliyun.ecs20140526.Client;
|
||||
import com.aliyun.ecs20140526.models.*;
|
||||
import com.aliyun.tea.TeaException;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import com.muyu.cargateway.config.AliProperties;
|
||||
import com.muyu.cargateway.domain.AliInstance;
|
||||
import com.muyu.common.core.exception.ServiceException;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Lenovo
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-10-07-18:43
|
||||
* @ Version:1.0
|
||||
* @ Description:ali业务层
|
||||
*/
|
||||
@Log4j2
|
||||
@Service
|
||||
public class AliYunEcsService {
|
||||
/**
|
||||
* 阿里云配置
|
||||
*/
|
||||
@Autowired
|
||||
private AliProperties aliProperties;
|
||||
/**
|
||||
* 阿里云客户端
|
||||
*/
|
||||
@Autowired
|
||||
private Client client;
|
||||
/**
|
||||
* redis缓存
|
||||
*/
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
/**
|
||||
* 生成实例
|
||||
*
|
||||
* @param amount 生成数量
|
||||
* @return 实例id集合
|
||||
*/
|
||||
public List<String> generateInstance(Integer amount) {
|
||||
redisService.deleteObject("instanceIds");
|
||||
redisService.deleteObject("instanceList");
|
||||
// 检查生成实例的数量是否有效
|
||||
if (amount == null || amount <= 0) {
|
||||
throw new ServiceException("生成数量不能小于1");
|
||||
}
|
||||
|
||||
// 初始化系统盘配置
|
||||
RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = new RunInstancesRequest.RunInstancesRequestSystemDisk();
|
||||
systemDisk.setSize("40");
|
||||
systemDisk.setCategory("cloud_essd");
|
||||
|
||||
// 创建创建实例请求对象并设置参数
|
||||
RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
|
||||
// 设置地域ID
|
||||
.setRegionId(aliProperties.getRegionId())
|
||||
// 设置镜像ID
|
||||
.setImageId(aliProperties.getImageId())
|
||||
// 设置实例规格类型
|
||||
.setInstanceType(aliProperties.getInstanceType())
|
||||
// 设置安全组ID
|
||||
.setSecurityGroupId(aliProperties.getSecurityGroupId())
|
||||
// 设置虚拟交换机ID
|
||||
.setVSwitchId(aliProperties.getSwitchId())
|
||||
// 设置实例名称
|
||||
.setInstanceName("server-mqtt")
|
||||
// 设置付费类型 按量付费
|
||||
.setInstanceChargeType("PostPaid")
|
||||
// 设置系统盘配置
|
||||
.setSystemDisk(systemDisk)
|
||||
// 设置用户名
|
||||
.setHostName("root")
|
||||
// 设置密码
|
||||
.setPassword("10160810@a")
|
||||
// 设置要创建的实例数量
|
||||
.setAmount(amount)
|
||||
.setInternetChargeType("PayByTraffic")
|
||||
.setInternetMaxBandwidthOut(1);
|
||||
|
||||
// 创建运行时选项对象
|
||||
RuntimeOptions runtimeOptions = new RuntimeOptions();
|
||||
|
||||
// 尝试执行创建实例请求
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
RunInstancesResponse runInstancesResponse = client.runInstancesWithOptions(runInstancesRequest, runtimeOptions);
|
||||
if (runInstancesResponse.getStatusCode() != 200) {
|
||||
throw new ServiceException("查询实例状态失败");
|
||||
}
|
||||
log.info("实例创建成功: {}", runInstancesResponse.getBody().getInstanceIdSets().instanceIdSet);
|
||||
RunInstancesResponseBody body = runInstancesResponse.getBody();
|
||||
RunInstancesResponseBody.RunInstancesResponseBodyInstanceIdSets instanceIdSets = body.getInstanceIdSets();
|
||||
return new ArrayList<>(instanceIdSets.instanceIdSet);
|
||||
} catch (Exception error) {
|
||||
log.error("创建阿里云实例报错:[{}]", error.getMessage());
|
||||
throw new ServiceException(error.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据实例id查询实例信息
|
||||
*
|
||||
* @param instanceIds 实例id集合
|
||||
*/
|
||||
public List<AliInstance> selectInstance(List<String> instanceIds) throws Exception {
|
||||
// 检查实例ID列表是否为空,如果为空则抛出异常
|
||||
if (instanceIds == null || instanceIds.isEmpty()) {
|
||||
throw new ServiceException("实例id不能为空");
|
||||
}
|
||||
// 创建查询实例的请求对象
|
||||
DescribeInstancesRequest request = new DescribeInstancesRequest()
|
||||
.setRegionId(aliProperties.getRegionId());
|
||||
|
||||
// 创建运行时选项对象,用于配置请求的额外参数
|
||||
RuntimeOptions runtimeOptions = new RuntimeOptions();
|
||||
List<AliInstance> aliInstances = new ArrayList<>();
|
||||
try {
|
||||
// 发送请求并获取响应对象
|
||||
DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(request, runtimeOptions);
|
||||
// 检查响应状态码,如果为200则表示查询失败,抛出异常
|
||||
if (describeInstancesResponse.getStatusCode() != 200) {
|
||||
throw new ServiceException("查询实例状态失败");
|
||||
}
|
||||
List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance> instance = describeInstancesResponse.getBody().getInstances().getInstance();
|
||||
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance bodyInstance : instance) {
|
||||
// 实例id
|
||||
String instanceId = bodyInstance.getInstanceId();
|
||||
log.info("实例id为:{}", instanceId);
|
||||
// ip地址
|
||||
String ipAddress = bodyInstance.getPublicIpAddress().getIpAddress().get(0);
|
||||
log.info("实例ip为:{}", ipAddress);
|
||||
// 实例状态
|
||||
String status = bodyInstance.getStatus();
|
||||
log.info("实例状态为:{}", status);
|
||||
AliInstance aliInstance = new AliInstance(instanceId, ipAddress, status);
|
||||
aliInstances.add(aliInstance);
|
||||
}
|
||||
log.info("查询成功");
|
||||
} catch (Exception e) {
|
||||
log.error("查询服务器实例错误:[{}]", e.getMessage(), e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return aliInstances;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除实例
|
||||
*/
|
||||
public void deleteInstance() throws Exception {
|
||||
DescribeInstancesRequest attributeRequest = new DescribeInstancesRequest();
|
||||
attributeRequest.setRegionId(aliProperties.getRegionId());
|
||||
|
||||
RuntimeOptions runtimeOptions = new RuntimeOptions();
|
||||
|
||||
DescribeInstancesResponse instancesWithOptions = client.describeInstancesWithOptions(attributeRequest, runtimeOptions);
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
DescribeInstancesResponseBody body = instancesWithOptions.getBody();
|
||||
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.instances.instance) {
|
||||
list.add(instance.getInstanceId());
|
||||
}
|
||||
log.info("list:" + list);
|
||||
DeleteInstancesRequest deleteInstancesRequest = new DeleteInstancesRequest();
|
||||
deleteInstancesRequest.setRegionId(aliProperties.getRegionId())
|
||||
.setDryRun(false)
|
||||
.setForce(true)
|
||||
.setTerminateSubscription(true)
|
||||
.setInstanceId(list);
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
try {
|
||||
client.deleteInstancesWithOptions(deleteInstancesRequest, runtime);
|
||||
} catch (TeaException error) {
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
System.out.println(error.getMessage());
|
||||
// 诊断地址
|
||||
System.out.println(error.getData().get("Recommend"));
|
||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
} catch (Exception _error) {
|
||||
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
System.out.println(error.getMessage());
|
||||
// 诊断地址
|
||||
System.out.println(error.getData().get("Recommend"));
|
||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.muyu.cargateway;
|
||||
|
||||
import com.muyu.common.security.annotation.EnableCustomConfig;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-09-17-15:00
|
||||
* @ Version:1.0
|
||||
* @ Description:故障启动类
|
||||
* @author Lenovo
|
||||
*/
|
||||
@Log4j2
|
||||
@EnableCustomConfig
|
||||
@EnableFeignClients
|
||||
@SpringBootApplication
|
||||
public class CloudVehicleGatewayApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CloudVehicleGatewayApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package com.muyu.cargateway.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author Lenovo
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-10-07-18:34
|
||||
* @ Version:1.0
|
||||
* @ Description:Ali配置类
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "aliyun")
|
||||
public class AliProperties {
|
||||
/**
|
||||
* key
|
||||
*/
|
||||
private String accessKeyId;
|
||||
/**
|
||||
* secret
|
||||
*/
|
||||
private String accessKeySecret;
|
||||
/**
|
||||
* 地域
|
||||
*/
|
||||
private String endpoint;
|
||||
/**
|
||||
* 地域id
|
||||
*/
|
||||
private String regionId;
|
||||
/**
|
||||
* 镜像id
|
||||
*/
|
||||
private String imageId;
|
||||
/**
|
||||
* 实例规格
|
||||
*/
|
||||
private String instanceType;
|
||||
/**
|
||||
* 安全组id
|
||||
*/
|
||||
private String securityGroupId;
|
||||
/**
|
||||
* 虚拟交换机ID
|
||||
*/
|
||||
private String switchId;
|
||||
/**
|
||||
* 生成实例数量
|
||||
*/
|
||||
private Integer amount;
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
package com.muyu.cargateway.config;
|
||||
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.amqp.core.*;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author Lenovo
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-10-04-15:13
|
||||
* @ Version:1.0
|
||||
* @ Description:rabbitmq配置类
|
||||
*/
|
||||
@Log4j2
|
||||
@Configuration
|
||||
public class RabbitmqConfig {
|
||||
// 日志
|
||||
private static final Logger logger = LoggerFactory.getLogger(RabbitmqConfig.class);
|
||||
|
||||
/**
|
||||
* 队列
|
||||
*/
|
||||
public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
|
||||
/**
|
||||
* 队列
|
||||
*/
|
||||
public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
|
||||
/**
|
||||
* 交换机
|
||||
*/
|
||||
public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";
|
||||
/**
|
||||
* 路由key
|
||||
*/
|
||||
public static final String ROUTINGKEY_EMAIL = "inform.#.email.#";
|
||||
/**
|
||||
* 路由key
|
||||
*/
|
||||
public static final String ROUTINGKEY_SMS = "inform.#.sms.#";
|
||||
|
||||
/**
|
||||
* 声明交换机,做持久化
|
||||
*/
|
||||
@Bean(EXCHANGE_TOPICS_INFORM)
|
||||
public Exchange exchangeTopicsInform() {
|
||||
try {
|
||||
Exchange exchange = ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
|
||||
log.info("创建的交换机为: {}", EXCHANGE_TOPICS_INFORM);
|
||||
return exchange;
|
||||
} catch (Exception e) {
|
||||
log.error("创建该: {} 交换机失败", EXCHANGE_TOPICS_INFORM, e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
// 声明QUEUE_INFORM_EMAIL队列
|
||||
@Bean(QUEUE_INFORM_EMAIL)
|
||||
public Queue queueInformEmail() {
|
||||
try {
|
||||
Queue queue = new Queue(QUEUE_INFORM_EMAIL);
|
||||
log.info("创建的队列为: {}", QUEUE_INFORM_EMAIL);
|
||||
return queue;
|
||||
} catch (Exception e) {
|
||||
log.error("创建该: {} 队列失败", QUEUE_INFORM_EMAIL, e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
// 声明QUEUE_INFORM_SMS队列
|
||||
@Bean(QUEUE_INFORM_SMS)
|
||||
public Queue queueInformSms() {
|
||||
try {
|
||||
Queue queue = new Queue(QUEUE_INFORM_SMS);
|
||||
log.info("创建的队列为: {}", QUEUE_INFORM_SMS);
|
||||
return queue;
|
||||
} catch (Exception e) {
|
||||
log.error("创建该: {} 队列失败", QUEUE_INFORM_SMS, e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
//ROUTINGKEY_EMAIL队列绑定交换机,指定routingKey
|
||||
@Bean
|
||||
public Binding bindingQueueInformEmail(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
|
||||
@Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
|
||||
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs();
|
||||
}
|
||||
|
||||
//ROUTINGKEY_SMS队列绑定交换机,指定routingKey
|
||||
@Bean
|
||||
public Binding bindingRoutingKeySms(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
|
||||
@Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
|
||||
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.muyu.cargateway.controller;
|
||||
|
||||
import com.muyu.cargateway.domain.model.MqttServerModel;
|
||||
import com.muyu.cargateway.domain.req.VehicleConnectionReq;
|
||||
import com.muyu.cargateway.service.CarOneClickOperationService;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-09-26-20:14
|
||||
* @ Version:1.0
|
||||
* @ Description:车辆
|
||||
* @author Lenovo
|
||||
*/
|
||||
@Log4j2
|
||||
@RestController
|
||||
@RequestMapping("/vehicleGateway")
|
||||
@Tag(name = "连接车辆控制层")
|
||||
public class CarOneClickOperationController {
|
||||
@Autowired
|
||||
private CarOneClickOperationService carOneClickOperationService;
|
||||
|
||||
/**
|
||||
* 获取http连接的参数
|
||||
* @param vehicleConnectionReq
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/receiveMsg/connect")
|
||||
public Result<MqttServerModel> receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq){
|
||||
log.info(">"+vehicleConnectionReq);
|
||||
MqttServerModel mqttServerModel =carOneClickOperationService.getConnect(vehicleConnectionReq);
|
||||
return Result.success(mqttServerModel);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.muyu.cargateway.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-09-30-9:33
|
||||
* @ Version:1.0
|
||||
* @ Description:
|
||||
* @author Lenovo
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class AliInstance {
|
||||
/**
|
||||
*实例ID
|
||||
*/
|
||||
private String instanceId;
|
||||
/**
|
||||
* 实例IP
|
||||
*/
|
||||
private String ipAddress;
|
||||
/**
|
||||
* 实例状态
|
||||
*/
|
||||
private String status;
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.muyu.cargateway.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-09-28-16:37
|
||||
* @ Version:1.0
|
||||
* @ Description:创建实例的配置
|
||||
* @author Lenovo
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class AliServerConfig {
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
private Long id;
|
||||
/**
|
||||
* 地域ID
|
||||
*/
|
||||
private String regionId;
|
||||
/**
|
||||
* 镜像ID
|
||||
*/
|
||||
private String imageId;
|
||||
/**
|
||||
* 实例规格
|
||||
*/
|
||||
private String instanceType;
|
||||
/**
|
||||
* 安全组ID
|
||||
*/
|
||||
private String securityGroupId;
|
||||
/**
|
||||
* 虚拟交换机ID
|
||||
*/
|
||||
private String vSwitchId;
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package com.muyu.cargateway.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-10-04-15:16
|
||||
* @ Version:1.0
|
||||
* @ Description:车辆服务器
|
||||
* @author Lenovo
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ConnectWeight {
|
||||
/**
|
||||
* 车辆服务器IP
|
||||
*/
|
||||
private String carServerIp;
|
||||
/**
|
||||
* 权重值
|
||||
*/
|
||||
private Integer weightValue;
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
package com.muyu.cargateway.domain;
|
||||
|
||||
/**
|
||||
* 返回状态码
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class HttpStatus {
|
||||
/**
|
||||
* 操作成功
|
||||
*/
|
||||
public static final int SUCCESS = 200;
|
||||
|
||||
/**
|
||||
* 对象创建成功
|
||||
*/
|
||||
public static final int CREATED = 201;
|
||||
|
||||
/**
|
||||
* 请求已经被接受
|
||||
*/
|
||||
public static final int ACCEPTED = 202;
|
||||
|
||||
/**
|
||||
* 操作已经执行成功,但是没有返回数据
|
||||
*/
|
||||
public static final int NO_CONTENT = 204;
|
||||
|
||||
/**
|
||||
* 资源已被移除
|
||||
*/
|
||||
public static final int MOVED_PERM = 301;
|
||||
|
||||
/**
|
||||
* 重定向
|
||||
*/
|
||||
public static final int SEE_OTHER = 303;
|
||||
|
||||
/**
|
||||
* 资源没有被修改
|
||||
*/
|
||||
public static final int NOT_MODIFIED = 304;
|
||||
|
||||
/**
|
||||
* 参数列表错误(缺少,格式不匹配)
|
||||
*/
|
||||
public static final int BAD_REQUEST = 400;
|
||||
|
||||
/**
|
||||
* 未授权
|
||||
*/
|
||||
public static final int UNAUTHORIZED = 401;
|
||||
|
||||
/**
|
||||
* 访问受限,授权过期
|
||||
*/
|
||||
public static final int FORBIDDEN = 403;
|
||||
|
||||
/**
|
||||
* 资源,服务未找到
|
||||
*/
|
||||
public static final int NOT_FOUND = 404;
|
||||
|
||||
/**
|
||||
* 不允许的http方法
|
||||
*/
|
||||
public static final int BAD_METHOD = 405;
|
||||
|
||||
/**
|
||||
* 资源冲突,或者资源被锁
|
||||
*/
|
||||
public static final int CONFLICT = 409;
|
||||
|
||||
/**
|
||||
* 不支持的数据,媒体类型
|
||||
*/
|
||||
public static final int UNSUPPORTED_TYPE = 415;
|
||||
|
||||
/**
|
||||
* 系统内部错误
|
||||
*/
|
||||
public static final int ERROR = 500;
|
||||
|
||||
/**
|
||||
* 接口未实现
|
||||
*/
|
||||
public static final int NOT_IMPLEMENTED = 501;
|
||||
|
||||
/**
|
||||
* 系统警告消息
|
||||
*/
|
||||
public static final int WARN = 601;
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package com.muyu.cargateway.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-09-27-20:56
|
||||
* @ Version:1.0
|
||||
* @ Description:服务器配置
|
||||
* @author Lenovo
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@TableName(value="server_config")
|
||||
public class ServerConfig {
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@TableId(value = "id",type = IdType.AUTO)
|
||||
private Long id;
|
||||
/**
|
||||
* 租户id
|
||||
*/
|
||||
private String tenantId;
|
||||
/**
|
||||
* 主机地址
|
||||
*/
|
||||
private String host;
|
||||
/**
|
||||
* 端口
|
||||
*/
|
||||
private String port;
|
||||
/**
|
||||
* 负载的地址
|
||||
*/
|
||||
private String uri;
|
||||
/**
|
||||
* 默认MQTT地址
|
||||
*/
|
||||
private String defaultMqttAddr;
|
||||
/**
|
||||
* 默认MQTT主题
|
||||
*/
|
||||
private String defaultMqttTopic;
|
||||
/**
|
||||
* 默认MQTT QOS
|
||||
*/
|
||||
private Integer defaultMqttQos;
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.muyu.cargateway.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-10-06-11:05
|
||||
* @ Version:1.0
|
||||
* @ Description:车辆鉴权的参数
|
||||
* @author Lenovo
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class VehicleConnection {
|
||||
/**
|
||||
* 车辆VIN
|
||||
*/
|
||||
private String vehicleVin;
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String username;
|
||||
/**
|
||||
* 时间戳
|
||||
*/
|
||||
private String timestamp;
|
||||
/**
|
||||
* 随机数
|
||||
*/
|
||||
private String nonce;
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String password;
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.muyu.cargateway.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-10-03-10:10
|
||||
* @ Version:1.0
|
||||
* @ Description:车辆vin
|
||||
* @author Lenovo
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class VinIp {
|
||||
/**
|
||||
* 车辆的vin
|
||||
*/
|
||||
String vehicleVin;
|
||||
/**
|
||||
* 车辆的ip
|
||||
*/
|
||||
String ipAddress;
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.muyu.cargateway.domain.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-10-03-10:12
|
||||
* @ Version:1.0
|
||||
* @ Description:Mqtt服务模型
|
||||
* @author Lenovo
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class MqttServerModel {
|
||||
/**
|
||||
* Mqtt服务节点
|
||||
*/
|
||||
private String broker;
|
||||
/**
|
||||
* MQTT订阅主题
|
||||
*/
|
||||
private String topic;
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
package com.muyu.cargateway.domain.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-09-26-20:23
|
||||
* @ Version:1.0
|
||||
* @ Description:任务执行模型
|
||||
* @author Lenovo
|
||||
*/
|
||||
@Data
|
||||
@Log4j2
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class TaskModel {
|
||||
/**
|
||||
* 任务状态 默认为false状态
|
||||
* true为执行中,false为未执行
|
||||
*/
|
||||
private final AtomicBoolean status =new AtomicBoolean(Boolean.FALSE);
|
||||
/**
|
||||
* 堵塞计数器
|
||||
*/
|
||||
private CountDownLatch countDownLatch;
|
||||
/**
|
||||
* 任务执行堵塞队列
|
||||
*/
|
||||
private LinkedBlockingDeque<String> carQueue =new LinkedBlockingDeque<>();
|
||||
/**
|
||||
* 任务是否执行
|
||||
* true 执行中
|
||||
* false 未执行
|
||||
* @return 是否有任务执行
|
||||
*/
|
||||
private boolean isExecution(){
|
||||
return !status.get();
|
||||
}
|
||||
/**
|
||||
* 任务名称
|
||||
*/
|
||||
private String taskName;
|
||||
/**
|
||||
* 任务执行次数
|
||||
*/
|
||||
private Integer taskExecutionCount=0;
|
||||
/**
|
||||
* 任务开始时间
|
||||
*/
|
||||
private Long taskStartTime;
|
||||
/**
|
||||
* 任务成功执行次数
|
||||
*/
|
||||
private AtomicInteger taskSuccessSum=new AtomicInteger();
|
||||
/**
|
||||
* 任务执行失败次数
|
||||
*/
|
||||
private AtomicInteger taskErrorSum=new AtomicInteger();
|
||||
|
||||
/**
|
||||
* 判断是否有任务
|
||||
* @return true 有任务
|
||||
*/
|
||||
public boolean hashNext(){
|
||||
return !carQueue.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下一个任务节点
|
||||
* @return 任务VIN
|
||||
*/
|
||||
public String next(){
|
||||
return carQueue.poll();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package com.muyu.cargateway.domain.properties;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author Lenovo
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-09-26-22:13
|
||||
* @ Version:1.0
|
||||
* @ Description:Mqtt的配置
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class MqttProperties {
|
||||
/**
|
||||
* 节点
|
||||
*/
|
||||
private String broker;
|
||||
/**
|
||||
* 主题
|
||||
*/
|
||||
private String topic;
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String userName;
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String password;
|
||||
/**
|
||||
* 客户端id
|
||||
*/
|
||||
private String clientId;
|
||||
/**
|
||||
* 上报级别
|
||||
*/
|
||||
private int qos = 0;
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.muyu.cargateway.domain.req;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-10-03-10:04
|
||||
* @ Version:1.0
|
||||
* @ Description:车辆获取连接地址
|
||||
* @author Lenovo
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class VehicleConnectionReq {
|
||||
/**
|
||||
* 车辆VIN
|
||||
*/
|
||||
private String vehicleVin;
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String username;
|
||||
/**
|
||||
* 时间戳
|
||||
*/
|
||||
private String timestamp;
|
||||
/**
|
||||
* 随机数
|
||||
*/
|
||||
private String nonce;
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.muyu.cargateway.domain.resp;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-09-26-15:53
|
||||
* @ Version:1.0
|
||||
* @ Description:调用Ali服务器配置实体类
|
||||
* @author Lenovo
|
||||
*/
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class AliServerConfig {
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
private Long id;
|
||||
/**
|
||||
* 地域id (实例所属的地域ID)
|
||||
*/
|
||||
private String regionId;
|
||||
/**
|
||||
* 镜像id
|
||||
*/
|
||||
private String imageId;
|
||||
/**
|
||||
* 实例规格 (实例的资源规格)
|
||||
*/
|
||||
private String instanceType;
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.muyu.cargateway.instance;
|
||||
|
||||
import com.muyu.cargateway.Aliyun.service.AliYunEcsService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-10-07-21:51
|
||||
* @ Version:1.0
|
||||
* @ Description:删除实例方法
|
||||
* @author Lenovo
|
||||
*/
|
||||
@Log4j2
|
||||
@Component
|
||||
public class DeleteSample implements DisposableBean {
|
||||
@Autowired
|
||||
private AliYunEcsService aliYunEcsService;
|
||||
@Override
|
||||
public void destroy() {
|
||||
try {
|
||||
log.info("===开始执行删除实例方法");
|
||||
Thread.sleep(10000);
|
||||
aliYunEcsService.deleteInstance();
|
||||
} catch (Exception e) {
|
||||
log.info("删除实例失败");
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
log.info("删除实例成功");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
package com.muyu.cargateway.instance;
|
||||
|
||||
import com.muyu.cargateway.Aliyun.service.AliYunEcsService;
|
||||
import com.muyu.cargateway.config.AliProperties;
|
||||
import com.muyu.cargateway.domain.AliInstance;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Lenovo
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-09-28-21:10
|
||||
* @ Version:1.0
|
||||
* @ Description:调用ali的类
|
||||
*/
|
||||
@Log4j2
|
||||
@Component
|
||||
public class Sample implements ApplicationRunner{
|
||||
|
||||
@Autowired
|
||||
private AliYunEcsService aliYunEcsService;
|
||||
@Autowired
|
||||
private AliProperties aliProperties;
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
List<String> list;
|
||||
try {
|
||||
log.info("开始创建实例");
|
||||
list = aliYunEcsService.generateInstance(aliProperties.getAmount());
|
||||
} catch (Exception e) {
|
||||
log.info("创建实例失败");
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
log.info("创建实例成功");
|
||||
redisService.setCacheList("instanceIds", list);
|
||||
try {
|
||||
Thread.sleep(6000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
List<AliInstance> aliInstances = aliYunEcsService.selectInstance(list);
|
||||
log.info("查询实例信息成功:{}",aliInstances);
|
||||
// 将查询到的实例信息列表存储到Redis中
|
||||
redisService.setCacheList("instanceList", aliInstances);
|
||||
log.info("redis存储成功:{}", aliInstances);
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public void destroy(){
|
||||
// try {
|
||||
// log.info("===开始执行删除实例方法");
|
||||
// aliYunEcsService.deleteInstance();
|
||||
// redisService.deleteObject("instanceIds");
|
||||
// redisService.deleteObject("instanceList");
|
||||
// } catch (Exception e) {
|
||||
// log.info("删除实例失败");
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
// log.info("删除实例成功");
|
||||
// }
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.muyu.cargateway.mapper;
|
||||
|
||||
import com.muyu.cargateway.domain.VehicleConnection;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-09-26-20:15
|
||||
* @ Version:1.0
|
||||
* @ Description:车辆一键操作持久层
|
||||
* @author Lenovo
|
||||
*/
|
||||
@Mapper
|
||||
public interface CarOneClickOperationMapper {
|
||||
void addConnect(VehicleConnection vehicleConnection);
|
||||
|
||||
List<String> selectByVehicleVin(String vehicleVin);
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.muyu.cargateway.service;
|
||||
|
||||
import com.muyu.cargateway.domain.model.MqttServerModel;
|
||||
import com.muyu.cargateway.domain.req.VehicleConnectionReq;
|
||||
|
||||
/**
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-09-26-20:15
|
||||
* @ Version:1.0
|
||||
* @ Description:车辆一键操作业务层
|
||||
* @author Lenovo
|
||||
*/
|
||||
public interface CarOneClickOperationService {
|
||||
|
||||
/**
|
||||
* 获取连接
|
||||
* @param vehicleConnectionReq 车辆连接请求参数
|
||||
* @return
|
||||
*/
|
||||
MqttServerModel getConnect(VehicleConnectionReq vehicleConnectionReq);
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
package com.muyu.cargateway.service.Impl;
|
||||
|
||||
import com.muyu.cargateway.domain.VehicleConnection;
|
||||
import com.muyu.cargateway.domain.VinIp;
|
||||
import com.muyu.cargateway.domain.model.MqttServerModel;
|
||||
import com.muyu.cargateway.domain.req.VehicleConnectionReq;
|
||||
import com.muyu.cargateway.mapper.CarOneClickOperationMapper;
|
||||
import com.muyu.cargateway.service.CarOneClickOperationService;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-09-26-20:16
|
||||
* @ Version:1.0
|
||||
* @ Description:车辆一键操作业务实现层
|
||||
* @author Lenovo
|
||||
*/
|
||||
@Log4j2
|
||||
@Service
|
||||
public class CarOneClickOperationServiceImpl implements CarOneClickOperationService {
|
||||
|
||||
@Autowired
|
||||
private CarOneClickOperationMapper carOneClickOperationMapper;
|
||||
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
/**
|
||||
* 获取连接信息
|
||||
* @param vehicleConnectionReq 车辆连接请求参数
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public MqttServerModel getConnect(VehicleConnectionReq vehicleConnectionReq) {
|
||||
log.info("车辆连接请求:{}",vehicleConnectionReq.toString());
|
||||
|
||||
// 使用交换机发送消息
|
||||
rabbitTemplate.convertAndSend("exchange_topics_inform","inform.#.email.#",vehicleConnectionReq.getVehicleVin());
|
||||
log.info("发送消息成功:{}",vehicleConnectionReq.getVehicleVin());
|
||||
|
||||
|
||||
VehicleConnection vehicleConnection = new VehicleConnection();
|
||||
//车辆vin
|
||||
vehicleConnection.setVehicleVin(vehicleConnectionReq.getVehicleVin());
|
||||
//用户名
|
||||
vehicleConnection.setUsername(vehicleConnectionReq.getUsername());
|
||||
//密码(vin+时间戳+随机数)
|
||||
vehicleConnection.setPassword(vehicleConnectionReq.getVehicleVin()+vehicleConnectionReq.getTimestamp()+vehicleConnectionReq.getNonce());
|
||||
//查询车辆vin集合
|
||||
List<String> vehicleConnections =carOneClickOperationMapper.selectByVehicleVin(vehicleConnectionReq.getVehicleVin());
|
||||
if(vehicleConnections.isEmpty()){
|
||||
//添加
|
||||
carOneClickOperationMapper.addConnect(vehicleConnection);
|
||||
}
|
||||
log.info("该车辆已存在,不能重复预上线");
|
||||
//TODO 返回连接信息 做轮询操作
|
||||
|
||||
|
||||
return new MqttServerModel("tcp://"+"106.15.136.7"+":1883","vehicle");
|
||||
|
||||
}
|
||||
/**
|
||||
* 添加车辆绑定IP地址存入redis中
|
||||
*/
|
||||
public void addIpAddress(VinIp vinIp){
|
||||
redisService.setCacheObject("vehicle_ip_address:"+vinIp.getVehicleVin(),vinIp.getIpAddress());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,135 @@
|
|||
package com.muyu.cargateway.utils;
|
||||
|
||||
import com.aliyun.ecs20140526.Client;
|
||||
import com.aliyun.ecs20140526.models.DeleteInstanceRequest;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesRequest;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
|
||||
import com.aliyun.ecs20140526.models.RunInstancesRequest;
|
||||
import com.aliyun.tea.TeaException;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.aliyun.teautil.Common;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @ Tool:IntelliJ IDEA
|
||||
* @ Author:CHX
|
||||
* @ Date:2024-10-02-16:04
|
||||
* @ Version:1.0
|
||||
* @ Description:ecs实例工具类
|
||||
* @author Lenovo
|
||||
*/
|
||||
@Log4j2
|
||||
public class ECSTool {
|
||||
|
||||
public static final String ACCESS_KEY_ID = "LTAI5tDH3FyRx4PRr6anx2TL";
|
||||
public static final String ACCESS_KEY_SECRET = "xdQnX2tDattY50raNkUWmHzE2tondP";
|
||||
|
||||
public static Client createClient() throws Exception {
|
||||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||
Config config = new Config()
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||
.setAccessKeyId(ACCESS_KEY_ID)
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||
.setAccessKeySecret(ACCESS_KEY_SECRET);
|
||||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||
config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
|
||||
return new Client(config);
|
||||
}
|
||||
public static void runEcsInstance(String regionId, String launchTemplateId) throws Exception {
|
||||
Client client = ECSTool.createClient();
|
||||
RunInstancesRequest request = new RunInstancesRequest();
|
||||
request.setRegionId(regionId)
|
||||
.setLaunchTemplateId(launchTemplateId);
|
||||
RuntimeOptions runtimeOptions = new RuntimeOptions();
|
||||
try{
|
||||
client.runInstancesWithOptions(request, runtimeOptions);
|
||||
}catch (Exception error){
|
||||
// 处理API调用过程中出现的异常
|
||||
System.out.println(error.getMessage());
|
||||
if (error instanceof TeaException) {
|
||||
// 处理特定类型的异常,如TeaException
|
||||
TeaException teaError = (TeaException) error;
|
||||
// 打印诊断推荐链接
|
||||
System.out.println(teaError.getData().get("Recommend"));
|
||||
// 断言错误信息
|
||||
com.aliyun.teautil.Common.assertAsString(teaError.getMessage());
|
||||
} else {
|
||||
// 处理其他类型的异常
|
||||
System.out.println(error.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*销毁实例
|
||||
*/
|
||||
public static void runEcsRemove(String instanceId) throws Exception {
|
||||
Client client = ECSTool.createClient();
|
||||
DeleteInstanceRequest deleteInstancesRequest = new DeleteInstanceRequest();
|
||||
deleteInstancesRequest.setInstanceId(instanceId);
|
||||
RuntimeOptions runtimeOptions = new RuntimeOptions();
|
||||
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
client.deleteInstanceWithOptions(deleteInstancesRequest, runtimeOptions);
|
||||
} catch (TeaException error) {
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
System.out.println(error.getMessage());
|
||||
// 诊断地址
|
||||
System.out.println(error.getData().get("Recommend"));
|
||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
} catch (Exception _error) {
|
||||
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
System.out.println(error.getMessage());
|
||||
// 诊断地址
|
||||
System.out.println(error.getData().get("Recommend"));
|
||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询实例列表
|
||||
* @param regionId 地域ID
|
||||
*/
|
||||
public static List<String> findInstance(String regionId) throws Exception {
|
||||
Client client = ECSTool.createClient();
|
||||
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
|
||||
describeInstancesRequest.setRegionId(regionId);
|
||||
RuntimeOptions runtimeOptions = new RuntimeOptions();
|
||||
List<String> stringArrayList = new ArrayList<>();
|
||||
try {
|
||||
DescribeInstancesResponse response = client.describeInstancesWithOptions(describeInstancesRequest, runtimeOptions);
|
||||
List<List<String>> ipListList = response.getBody().instances.getInstance().stream().map(instance -> instance.publicIpAddress.ipAddress).collect(Collectors.toList());
|
||||
for (List<String> strings : ipListList) {
|
||||
for (String ip : strings) {
|
||||
stringArrayList.add(ip);
|
||||
}
|
||||
return stringArrayList;
|
||||
}
|
||||
} catch (TeaException error) {
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
System.out.println(error.getMessage());
|
||||
// 诊断地址
|
||||
System.out.println(error.getData().get("Recommend"));
|
||||
Common.assertAsString(error.message);
|
||||
} catch (Exception _error) {
|
||||
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
System.out.println(error.getMessage());
|
||||
// 诊断地址
|
||||
System.out.println(error.getData().get("Recommend"));
|
||||
Common.assertAsString(error.message);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
Spring Boot Version: ${spring-boot.version}
|
||||
Spring Application Name: ${spring.application.name}
|
|
@ -0,0 +1,85 @@
|
|||
# Tomcat
|
||||
server:
|
||||
port: 12900
|
||||
|
||||
# nacos线上地址
|
||||
nacos:
|
||||
addr: 47.116.173.119:8848
|
||||
user-name: nacos
|
||||
password: nacos
|
||||
namespace: one
|
||||
# SPRING_AMQP_DESERIALIZATION_TRUST_ALL=true spring.amqp.deserialization.trust.all
|
||||
# Spring
|
||||
spring:
|
||||
rabbitmq:
|
||||
host: 47.116.173.119
|
||||
port: 5672
|
||||
username: guest
|
||||
password: guest
|
||||
virtual-host: /
|
||||
listener:
|
||||
simple:
|
||||
prefetch: 1 # 默认每次取出一条消息消费, 消费完成取下一条
|
||||
acknowledge-mode: manual # 设置消费端手动ack确认
|
||||
retry:
|
||||
enabled: true # 是否支持重试
|
||||
publisher-confirm-type: correlated #确认消息已发送到交换机(Exchange)
|
||||
publisher-returns: true #确认消息已发送到队列(Queue)
|
||||
amqp:
|
||||
deserialization:
|
||||
trust:
|
||||
all: true
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
|
||||
application:
|
||||
# 应用名称
|
||||
name: cloud-car-gateway
|
||||
profiles:
|
||||
# 环境配置
|
||||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: ${nacos.addr}
|
||||
# nacos用户名
|
||||
username: ${nacos.user-name}
|
||||
# nacos密码
|
||||
password: ${nacos.password}
|
||||
# 命名空间
|
||||
namespace: ${nacos.namespace}
|
||||
config:
|
||||
# 服务注册地址
|
||||
server-addr: ${nacos.addr}
|
||||
# nacos用户名
|
||||
username: ${nacos.user-name}
|
||||
# nacos密码
|
||||
password: ${nacos.password}
|
||||
# 命名空间
|
||||
namespace: ${nacos.namespace}
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
shared-configs:
|
||||
# 系统共享配置
|
||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
# 系统环境Config共享配置
|
||||
- application-config-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
# xxl-job 配置文件
|
||||
- application-xxl-config-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
# rabbit 配置文件
|
||||
- application-rabbit-config-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
logging:
|
||||
level:
|
||||
com.muyu.breakdown.mapper: DEBUG
|
||||
aliyun:
|
||||
access-key-id: LTAI5tDH3FyRx4PRr6anx2TL
|
||||
access-key-secret: xdQnX2tDattY50raNkUWmHzE2tondP
|
||||
endpoint: ecs-cn-hangzhou.aliyuncs.com
|
||||
region-id: cn-shanghai
|
||||
image-id: m-uf6ih0vnl5f51pquns11
|
||||
instance-type: ecs.t6-c1m1.large
|
||||
security-group-id: sg-uf642d5u4ja5gsiitx8y
|
||||
switch-id: vsw-uf66lifrkhxqc94xi06v3
|
||||
amount: 1
|
|
@ -0,0 +1,74 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="true" scanPeriod="60 seconds" debug="false">
|
||||
<!-- 日志存放路径 -->
|
||||
<property name="log.path" value="logs/cloud-breakdown"/>
|
||||
<!-- 日志输出格式 -->
|
||||
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n"/>
|
||||
|
||||
<!-- 控制台输出 -->
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- 系统日志输出 -->
|
||||
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}/info.log</file>
|
||||
<!-- 循环政策:基于时间创建日志文件 -->
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<!-- 日志文件名格式 -->
|
||||
<fileNamePattern>${log.path}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||
<!-- 日志最大的历史 60天 -->
|
||||
<maxHistory>60</maxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<!-- 过滤的级别 -->
|
||||
<level>INFO</level>
|
||||
<!-- 匹配时的操作:接收(记录) -->
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<!-- 不匹配时的操作:拒绝(不记录) -->
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}/error.log</file>
|
||||
<!-- 循环政策:基于时间创建日志文件 -->
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<!-- 日志文件名格式 -->
|
||||
<fileNamePattern>${log.path}/error.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||
<!-- 日志最大的历史 60天 -->
|
||||
<maxHistory>60</maxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<!-- 过滤的级别 -->
|
||||
<level>ERROR</level>
|
||||
<!-- 匹配时的操作:接收(记录) -->
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<!-- 不匹配时的操作:拒绝(不记录) -->
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<!-- 系统模块日志级别控制 -->
|
||||
<logger name="com.muyu" level="info"/>
|
||||
<!-- Spring日志级别控制 -->
|
||||
<logger name="org.springframework" level="warn"/>
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="console"/>
|
||||
</root>
|
||||
|
||||
<!--系统操作日志-->
|
||||
<root level="info">
|
||||
<appender-ref ref="file_info"/>
|
||||
<appender-ref ref="file_error"/>
|
||||
</root>
|
||||
</configuration>
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue