58 lines
2.3 KiB
Java
58 lines
2.3 KiB
Java
package com.car.service.impl;
|
||
|
||
import com.aliyun.ecs20140526.Client;
|
||
import com.aliyun.teaopenapi.models.Config;
|
||
import com.car.demos.MqttServerModel;
|
||
import com.car.demos.Result;
|
||
|
||
import com.car.service.ConnectService;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||
import org.springframework.stereotype.Service;
|
||
|
||
/**
|
||
* 业务实现层 ConnectImplImpl
|
||
*
|
||
* @author Yangle
|
||
* Date 2024/5/28 21:50
|
||
*/
|
||
@Service
|
||
public class ConnectServiceImpl implements ConnectService {
|
||
// @Autowired
|
||
// private ConnerMapper connerMapper;
|
||
@Autowired
|
||
private StringRedisTemplate redisTemplate;
|
||
@Override
|
||
public Result<MqttServerModel> getConnect() {
|
||
if (redisTemplate.hasKey("Redis")){
|
||
|
||
Integer count = Integer.valueOf(redisTemplate.opsForValue().get("Redis"));
|
||
if (count == 100){
|
||
redisTemplate.opsForValue().set("Redis",String.valueOf(0));
|
||
}else {
|
||
redisTemplate.opsForValue().set("Redis",String.valueOf(count + 1));
|
||
}
|
||
|
||
String ip = redisTemplate.opsForList().index("ips", count);
|
||
return Result.success(new MqttServerModel("tcp://"+ip+":1883","test1"));
|
||
}else {
|
||
redisTemplate.opsForValue().set("count",String.valueOf(1));
|
||
String ip = redisTemplate.opsForList().index("ips", 0);
|
||
return Result.success(new MqttServerModel("tcp://"+ip+":1883","test1"));
|
||
}
|
||
}
|
||
|
||
public static Client createClient() throws Exception {
|
||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
||
Config config = new Config()
|
||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||
.setAccessKeyId("LTAI5tFVx9F12e5f4EuJzyZj")
|
||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||
.setAccessKeySecret("mn06SdxTmdmCjmaEGBq95bVF6e3Sa9");
|
||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||
config.endpoint = "ecs.cn-shanghai.aliyuncs.com";
|
||
return new Client(config);
|
||
}
|
||
}
|