新建:spring-zhub-start 工程提交
This commit is contained in:
commit
30876306c5
33
.gitignore
vendored
Normal file
33
.gitignore
vendored
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
HELP.md
|
||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
15
README.md
Normal file
15
README.md
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
# Read Me First
|
||||||
|
> 运行本 demo 先启动消息中间件
|
||||||
|
|
||||||
|
## zhub 消息中间间启动
|
||||||
|
|
||||||
|
**1、通过 docker 镜像启动**
|
||||||
|
> docker pull harbor.1216.top/zhub/zhub:v8
|
||||||
|
> docker run -d --name zhub -p 711:711 -p 1216:1216 zhub:v8
|
||||||
|
|
||||||
|
**2、通过二进制文件启动**
|
||||||
|
https://img.1216.top/docs/dl/zhub-rc.zip
|
||||||
|
|
||||||
|

|
||||||
|
- 如果 需要做timer测试,需要首先导入 z-tiemr.sql 到 mysql ;不影响其他运行测试
|
||||||
|
|
47
pom.xml
Normal file
47
pom.xml
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>3.1.1</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>com.example</groupId>
|
||||||
|
<artifactId>zhub-start</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>spring-zhub-start</name>
|
||||||
|
<description>Demo project for Spring Boot</description>
|
||||||
|
<properties>
|
||||||
|
<java.version>17</java.version>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web-services</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.tccn</groupId>
|
||||||
|
<artifactId>zhub-cli</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
13
src/main/java/com/example/demo/DemoApplication.java
Normal file
13
src/main/java/com/example/demo/DemoApplication.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package com.example.demo;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class DemoApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(DemoApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
88
src/main/java/com/example/demo/HelloController.java
Normal file
88
src/main/java/com/example/demo/HelloController.java
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
package com.example.demo;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import tccn.IType;
|
||||||
|
import tccn.zhub.RpcResult;
|
||||||
|
import tccn.zhub.ZHubClient;
|
||||||
|
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/hello")
|
||||||
|
public class HelloController {
|
||||||
|
|
||||||
|
/*
|
||||||
|
【zbus 对象初始化必要参数】
|
||||||
|
- addr: 服务连接地址 ip:端口
|
||||||
|
- groupid: 消费者组ID (非必须,订阅消息存在消费者组的时候协同消费,相同的消费者组之间名称填写相同)
|
||||||
|
- appid: 应用ID (非必须,如果做RPC 调用的时候必须,且全局唯一)
|
||||||
|
- auth: 认证信息,(必须,在中间件中配置) -- 老版本 app.ini 文件中的auth下配置, 最新zhub的 feature/auth 版本在 auth.yml 中配置的token
|
||||||
|
*/
|
||||||
|
private static final ZHubClient zhub = new ZHubClient("127.0.0.1:1216", "group-test", "DEV-LOCAL", "lxy@123456");
|
||||||
|
|
||||||
|
@RequestMapping("/")
|
||||||
|
public String index() {
|
||||||
|
return "Greetings from Spring Boot!";
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------
|
||||||
|
// -------------- 发送消息前,先做相应频道订阅 --------------------------
|
||||||
|
// 简单演示,实际项目中,确保其他服务资源启动完成后再开始处理数据
|
||||||
|
static {
|
||||||
|
// 订阅主题 a
|
||||||
|
zhub.subscribe("topic-t", x -> {
|
||||||
|
System.out.println("accept topic-t: " + x);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 订阅主题 delay-t, 和上面的一样
|
||||||
|
zhub.subscribe("delay-t", x -> {
|
||||||
|
System.out.println("accept delay-t: " + x);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 订阅远程调用: rpc-t
|
||||||
|
zhub.rpcSubscribe("rpc-t", IType.STRING, r -> {
|
||||||
|
Logger.getLogger(HelloController.class.getSimpleName()).info("timer-t running");
|
||||||
|
String value = r.getValue();
|
||||||
|
return r.render("收到rpc消息:" + value);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 订阅定时调度, 中间件中需要连接数据库并创建相应的 定时任务,
|
||||||
|
zhub.timer("timer-t", () -> {
|
||||||
|
Logger.getGlobal().info("timer-t running");
|
||||||
|
System.out.println("timer-t running");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
// 发送 主题消息
|
||||||
|
@RequestMapping("/publish")
|
||||||
|
public String publish(String value) {
|
||||||
|
zhub.publish("topic-t", value);
|
||||||
|
return "+OK!";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送 广播消息,和发布-订阅 频道命名空间相同
|
||||||
|
@RequestMapping("/broadcast")
|
||||||
|
public String broadcast(String value) {
|
||||||
|
zhub.broadcast("topic-t", value);
|
||||||
|
return "+OK!";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送延时消息,和发布-订阅 频道命名空间相同
|
||||||
|
@RequestMapping("/delay")
|
||||||
|
public String broadcast(String value, long delayTime) {
|
||||||
|
zhub.delay("delay-t", value, delayTime);
|
||||||
|
return "+OK!";
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/rpc")
|
||||||
|
public RpcResult<String> rpc(String value) {
|
||||||
|
RpcResult<String> result = zhub.rpc("rpc-t", value, IType.STRING);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
1
src/main/resources/application.properties
Normal file
1
src/main/resources/application.properties
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
13
src/test/java/com/example/demo/DemoApplicationTests.java
Normal file
13
src/test/java/com/example/demo/DemoApplicationTests.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package com.example.demo;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class DemoApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user