This commit is contained in:
linfso 2024-04-09 14:07:52 +08:00
parent 3039a1fabd
commit 1e674dbb62
7 changed files with 1 additions and 177 deletions

View File

@ -7,5 +7,5 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="17" project-jdk-type="JavaSDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="1.8.0_201" project-jdk-type="JavaSDK" />
</project>

View File

@ -83,21 +83,6 @@ spring.rabbitmq.username=ccloud
spring.rabbitmq.password=123#@!
spring.rabbitmq.virtual-host=/pushcus
#nats 相关配置
#nats.nats-urls=nats://nats.base:4222
nats.nats-urls=nats://192.168.88.57:4222
nats.max-reconnect=1000000
nats.reconnect-wait=2
nats.connection-timeout=2
#nats.user-name=nats
#nats.password=908123821Jsdfu123218771923
# 单点登录服务
sso.server.url=http://192.168.88.57:10089
sso.app.id=allinone-server
sso.app.secret=123456
# 单点登录超时时间,单位秒
#sso.timeout=7200
#DXP001_SEND

View File

@ -48,12 +48,6 @@
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>io.nats</groupId>
<artifactId>jnats</artifactId>
<version>2.16.14</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>

View File

@ -1,46 +0,0 @@
package com.xdadan.erp.mq.nats;
import com.xdadan.erp.common.utils.StringUtils;
import io.nats.client.Connection;
import io.nats.client.Nats;
import io.nats.client.Options;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.time.Duration;
@Configuration
public class NatsConfig {
/**
* nats 连接
* @param properties nats 配置文件类
* @return nats 连接
* @throws IOException
* @throws InterruptedException
*/
@Bean(name = "natsConnection")
public Connection natsConnection(NatsProperties properties) throws IOException, InterruptedException {
String[] str = properties.getNatsUrls().split(",");
Options.Builder builder = new Options.Builder()
.servers(str)
.connectionListener(new NatsMessageListener()) // 指定监听器
.maxReconnects(properties.getMaxReconnect())
.reconnectWait(Duration.ofSeconds(properties.getReconnectWait()))
.connectionTimeout(Duration.ofSeconds(properties.getConnectionTimeout()));
// 配置用户名和密码
if (!StringUtils.isEmpty(properties.getUserName()) && !StringUtils.isEmpty(properties.getPassword())) {
builder.userInfo(properties.getUserName().toCharArray(),properties.getPassword().toCharArray());
}
if (!StringUtils.isEmpty(properties.getToken())) {
builder.token(properties.getToken().toCharArray());
}
return Nats.connect(builder.build());
}
}

View File

@ -1,26 +0,0 @@
package com.xdadan.erp.mq.nats;
import com.xdadan.erp.common.utils.spring.SpringUtils;
import io.nats.client.Connection;
import io.nats.client.ConnectionListener;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
@Slf4j
public class NatsMessageListener implements ConnectionListener {
@Override
public void connectionEvent(Connection conn, Events events) {
log.info(String.format("nats connection status: %s", conn.getStatus()));
// 连接成功后进行消息订阅
if(Connection.Status.CONNECTED.equals(conn.getStatus())){
log.info("NATS conneted");
}
}
}

View File

@ -1,35 +0,0 @@
package com.xdadan.erp.mq.nats;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
#nats 相关配置
nats.nats-urls=nats://xxx:4222
nats.max-reconnect=60
nats.reconnect-wait=2
nats.connection-timeout=2
nats.user-name=nats
nats.password=nats
*/
@Component
@ConfigurationProperties(prefix = "nats")
@Data
public class NatsProperties {
private String natsUrls;
private String token = null;
private int maxReconnect = 60;
private int reconnectWait = 2;
private int connectionTimeout = 2;
private String userName;
private String password;
}

View File

@ -1,48 +0,0 @@
package com.xdadan.erp.tst;
import com.xdadan.erp.common.core.controller.BaseController;
import com.xdadan.erp.common.utils.spring.SpringUtils;
import io.nats.client.Connection;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
@Slf4j
@RestController
@RequestMapping("/tst/mq/nats")
public class TestController extends BaseController
{
@Resource(name="natsConnection")
private Connection natsConnection;
@GetMapping("/publish")
public void publish(String subject,String msg)
{
// 发送测试消息
// sendMqMsg("IM-BINDGROUP","111,1");
sendMqMsg(subject,msg);
}
private void sendMqMsg(String subject,String msg){
if(natsConnection==null) {
log.info("NATS服务器未连接!!!");
return;
}
try {
log.info("发送 MQ NATS 消息: subject:{} , msg:{}",subject,msg);
natsConnection.publish(subject, msg.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
log.info("发送 MQ NATS 异常: subject:{} , msg:{}, e:",subject,msg,e.getMessage());
e.printStackTrace();
}
}
}