From 9fd85196d433a51657a2339b69245c878cecb271 Mon Sep 17 00:00:00 2001 From: crossoverJie Date: Sun, 1 Sep 2024 02:38:53 +0800 Subject: [PATCH 1/7] support integration test --- README.md | 5 +- cim-client/src/main/resources/banner.txt | 15 +--- .../cim/route/cache/ServerCache.java | 85 ------------------ .../cim/route/config/BeanConfig.java | 4 - .../cim/route/kit/ServerListListener.java | 63 ------------- .../com/crossoverjie/cim/route/kit/ZKit.java | 44 ---------- .../src/main/resources/banner.txt | 15 +--- cim-integration-test/pom.xml | 88 +++++++++++++++++++ .../java/com/crossoverjie/netty/Main.java | 7 ++ .../crossoverjie/cim/client/ClientTest.java | 84 ++++++++++++++++++ .../test/resources/application-client.yaml | 37 ++++++++ .../src/test/resources/application-route.yaml | 49 +++++++++++ .../route/api/vo/req/RegisterInfoReqVO.java | 3 + .../cim/server/config/BeanConfig.java | 8 -- .../cim/server/kit/RegistryZK.java | 3 - .../com/crossoverjie/cim/server/kit/ZKit.java | 48 ---------- cim-server/src/main/resources/banner.txt | 14 +-- pom.xml | 18 +++- 18 files changed, 300 insertions(+), 290 deletions(-) delete mode 100644 cim-forward-route/src/main/java/com/crossoverjie/cim/route/cache/ServerCache.java delete mode 100644 cim-forward-route/src/main/java/com/crossoverjie/cim/route/kit/ServerListListener.java delete mode 100644 cim-forward-route/src/main/java/com/crossoverjie/cim/route/kit/ZKit.java create mode 100644 cim-integration-test/pom.xml create mode 100644 cim-integration-test/src/main/java/com/crossoverjie/netty/Main.java create mode 100644 cim-integration-test/src/test/java/com/crossoverjie/cim/client/ClientTest.java create mode 100644 cim-integration-test/src/test/resources/application-client.yaml create mode 100644 cim-integration-test/src/test/resources/application-route.yaml delete mode 100644 cim-server/src/main/java/com/crossoverjie/cim/server/kit/ZKit.java diff --git a/README.md b/README.md index fc4a8938..03207f6d 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@
+[![codecov](https://codecov.io/gh/crossoverJie/cim/graph/badge.svg?token=oW5Gd1oKmf)](https://codecov.io/gh/crossoverJie/cim) [![Build Status](https://img.shields.io/badge/cim-cross--im-brightgreen.svg)](https://github.com/crossoverJie/cim) [![](https://badge.juejin.im/entry/5c2c000e6fb9a049f5713e26/likes.svg?style=flat-square)](https://juejin.im/post/5c2bffdc51882509181395d7) @@ -106,8 +107,8 @@ 首先需要安装 `Zookeeper、Redis` 并保证网络通畅。 ```shell -docker run --name zookeeper -d -p 2181:2181 zookeeper -docker run --rm --name redis -d -p 6379:6379 redis +docker run --name zookeeper -d -p 2181:2181 zookeeper: +docker run --rm --name redis -d -p 6379:6379 redis:7.4.0 ``` ```shell diff --git a/cim-client/src/main/resources/banner.txt b/cim-client/src/main/resources/banner.txt index 97b15b91..c7e303f1 100644 --- a/cim-client/src/main/resources/banner.txt +++ b/cim-client/src/main/resources/banner.txt @@ -1,14 +1,7 @@ - - ▄████▄ ██▓ ███▄ ▄███▓ -▒██▀ ▀█ ▓██▒▓██▒▀█▀ ██▒ -▒▓█ ▄ ▒██▒▓██ ▓██░ -▒▓▓▄ ▄██▒░██░▒██ ▒██ -▒ ▓███▀ ░░██░▒██▒ ░██▒ -░ ░▒ ▒ ░░▓ ░ ▒░ ░ ░ - ░ ▒ ▒ ░░ ░ ░ -░ ▒ ░░ ░ -░ ░ ░ ░ -░ + _ ___ __ + ____(_)_ _ ____/ (_)__ ___ / /_ +/ __/ / ' \ / __/ / / -_) _ \/ __/ +\__/_/_/_/_/ \__/_/_/\__/_//_/\__/ Power by @crossoverJie diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/cache/ServerCache.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/cache/ServerCache.java deleted file mode 100644 index b5e62f27..00000000 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/cache/ServerCache.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.crossoverjie.cim.route.cache; - -import com.crossoverjie.cim.route.kit.ZKit; -import com.google.common.cache.LoadingCache; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -/** - * Function: 服务器节点缓存 - * - * @author crossoverJie - * Date: 2018/8/19 01:31 - * @since JDK 1.8 - */ -@Slf4j -public class ServerCache { - - - @Autowired - private LoadingCache cache; - - @Autowired - private ZKit zkUtil; - - public void addCache(String key) { - cache.put(key, key); - } - - - /** - * 更新所有缓存/先删除 再新增 - * - * @param currentChildren - */ - public void updateCache(List currentChildren) { - cache.invalidateAll(); - for (String currentChild : currentChildren) { - // currentChildren=ip-127.0.0.1:11212:9082 or 127.0.0.1:11212:9082 - String key ; - if (currentChild.split("-").length == 2){ - key = currentChild.split("-")[1]; - }else { - key = currentChild ; - } - addCache(key); - } - } - - - /** - * 获取所有的服务列表 - * - * @return - */ - public List getServerList() { - - List list = new ArrayList<>(); - - if (cache.size() == 0) { - List allNode = zkUtil.getAllNode(); - for (String node : allNode) { - String key = node.split("-")[1]; - addCache(key); - } - } - for (Map.Entry entry : cache.asMap().entrySet()) { - list.add(entry.getKey()); - } - return list; - - } - - /** - * rebuild cache list - */ - public void rebuildCacheList(){ - updateCache(getServerList()) ; - } - -} diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java index 47c8c491..02f7c0a3 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/config/BeanConfig.java @@ -38,10 +38,6 @@ public class BeanConfig { @Autowired private AppConfiguration appConfiguration; - @Bean - public ZkClient buildZKClient() { - return new ZkClient(appConfiguration.getZkAddr(), appConfiguration.getZkConnectTimeout()); - } @Bean public MetaStore metaStore() throws Exception { diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/kit/ServerListListener.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/kit/ServerListListener.java deleted file mode 100644 index a699cc0d..00000000 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/kit/ServerListListener.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.crossoverjie.cim.route.kit; - -import com.crossoverjie.cim.route.cache.ServerCache; -import com.crossoverjie.cim.route.config.AppConfiguration; -import com.crossoverjie.cim.route.util.SpringBeanFactory; -import java.util.List; -import lombok.extern.slf4j.Slf4j; -import org.I0Itec.zkclient.IZkChildListener; -import org.I0Itec.zkclient.ZkClient; - -/** - * Function: - * - * @author crossoverJie - * Date: 2018/12/23 00:35 - * @since JDK 1.8 - */ -@Slf4j -public class ServerListListener implements Runnable{ - - - private ZKit zkUtil; - - private AppConfiguration appConfiguration ; - - private ZkClient zkClient; - - private ServerCache serverCache ; - - - public ServerListListener() { - zkUtil = SpringBeanFactory.getBean(ZKit.class) ; - appConfiguration = SpringBeanFactory.getBean(AppConfiguration.class) ; - zkClient = SpringBeanFactory.getBean(ZkClient.class) ; - serverCache = SpringBeanFactory.getBean(ServerCache.class) ; - } - - @Override - public void run() { - //注册监听服务 - subscribeEvent(appConfiguration.getZkRoot()); - - } - - /** - * 监听事件 - * - * @param path - */ - public void subscribeEvent(String path) { - zkClient.subscribeChildChanges(path, new IZkChildListener() { - @Override - public void handleChildChange(String parentPath, List currentChildren) throws Exception { - log.info("Clear and update local cache parentPath=[{}],currentChildren=[{}]", parentPath,currentChildren.toString()); - - //update local cache, delete and save. - serverCache.updateCache(currentChildren) ; - } - }); - - - } -} diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/kit/ZKit.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/kit/ZKit.java deleted file mode 100644 index c7b7ed22..00000000 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/kit/ZKit.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.crossoverjie.cim.route.kit; - -import com.alibaba.fastjson.JSON; -import com.crossoverjie.cim.route.config.AppConfiguration; -import java.util.List; -import lombok.extern.slf4j.Slf4j; -import org.I0Itec.zkclient.ZkClient; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -/** - * Function: Zookeeper kit - * - * @author crossoverJie - * Date: 2018/8/19 00:33 - * @since JDK 1.8 - */ -@Component -@Slf4j -public class ZKit { - - - - @Autowired - private ZkClient zkClient; - - @Autowired - private AppConfiguration appConfiguration; - - - - - /** - * get all server node from zookeeper - * @return - */ - public List getAllNode(){ - List children = zkClient.getChildren(appConfiguration.getZkRoot()); - log.info("Query all node =[{}] success.", JSON.toJSONString(children)); - return children; - } - - -} diff --git a/cim-forward-route/src/main/resources/banner.txt b/cim-forward-route/src/main/resources/banner.txt index 97b15b91..165abd2e 100644 --- a/cim-forward-route/src/main/resources/banner.txt +++ b/cim-forward-route/src/main/resources/banner.txt @@ -1,14 +1,7 @@ - - ▄████▄ ██▓ ███▄ ▄███▓ -▒██▀ ▀█ ▓██▒▓██▒▀█▀ ██▒ -▒▓█ ▄ ▒██▒▓██ ▓██░ -▒▓▓▄ ▄██▒░██░▒██ ▒██ -▒ ▓███▀ ░░██░▒██▒ ░██▒ -░ ░▒ ▒ ░░▓ ░ ▒░ ░ ░ - ░ ▒ ▒ ░░ ░ ░ -░ ▒ ░░ ░ -░ ░ ░ ░ -░ + _ __ + ____(_)_ _ _______ __ __/ /____ +/ __/ / ' \ / __/ _ \/ // / __/ -_) +\__/_/_/_/_/ /_/ \___/\_,_/\__/\__/ Power by @crossoverJie diff --git a/cim-integration-test/pom.xml b/cim-integration-test/pom.xml new file mode 100644 index 00000000..015367fc --- /dev/null +++ b/cim-integration-test/pom.xml @@ -0,0 +1,88 @@ + + + 4.0.0 + + com.crossoverjie.netty + cim + 1.0.0-SNAPSHOT + + + cim-integration-test + + + 17 + 17 + UTF-8 + + + + + com.crossoverjie.netty + cim-server + ${project.version} + + + com.crossoverjie.netty + cim-forward-route + ${project.version} + + + com.crossoverjie.netty + cim-client + ${project.version} + + + + org.apache.zookeeper + zookeeper + test + + + + + org.junit.jupiter + junit-jupiter + test + + + + org.testcontainers + testcontainers + + + org.testcontainers + junit-jupiter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + com.clever-cloud + testcontainers-zookeeper + + + + com.redis + testcontainers-redis + test + + + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + + \ No newline at end of file diff --git a/cim-integration-test/src/main/java/com/crossoverjie/netty/Main.java b/cim-integration-test/src/main/java/com/crossoverjie/netty/Main.java new file mode 100644 index 00000000..72a55739 --- /dev/null +++ b/cim-integration-test/src/main/java/com/crossoverjie/netty/Main.java @@ -0,0 +1,7 @@ +package com.crossoverjie.netty; + +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} \ No newline at end of file diff --git a/cim-integration-test/src/test/java/com/crossoverjie/cim/client/ClientTest.java b/cim-integration-test/src/test/java/com/crossoverjie/cim/client/ClientTest.java new file mode 100644 index 00000000..6b7cbfce --- /dev/null +++ b/cim-integration-test/src/test/java/com/crossoverjie/cim/client/ClientTest.java @@ -0,0 +1,84 @@ +package com.crossoverjie.cim.client; + +import com.clevercloud.testcontainers.zookeeper.ZooKeeperContainer; +import com.crossoverjie.cim.client.service.MsgHandle; +import com.crossoverjie.cim.client.util.SpringBeanFactory; +import com.crossoverjie.cim.common.res.BaseResponse; +import com.crossoverjie.cim.route.RouteApplication; +import com.crossoverjie.cim.route.api.RouteApi; +import com.crossoverjie.cim.route.api.vo.req.RegisterInfoReqVO; +import com.crossoverjie.cim.route.api.vo.res.RegisterInfoResVO; +import com.crossoverjie.cim.server.CIMServerApplication; +import com.crossoverjie.cim.server.config.AppConfiguration; +import com.redis.testcontainers.RedisContainer; +import jakarta.annotation.Resource; +import java.lang.reflect.Field; +import java.time.Duration; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import lombok.extern.slf4j.Slf4j; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.utility.DockerImageName; + +@Slf4j +public class ClientTest { + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName + .parse("zookeeper") + .withTag("3.9.2"); + + private static final Duration DEFAULT_STARTUP_TIMEOUT = Duration.ofSeconds(60); + + @Container + public final ZooKeeperContainer container = new ZooKeeperContainer(DEFAULT_IMAGE_NAME, DEFAULT_STARTUP_TIMEOUT); + + @Container + RedisContainer redis = new RedisContainer(DockerImageName.parse("redis:7.4.0")); + + @Test + public void olu() throws Exception { + container.start(); + redis.start(); + String host = container.getHost(); + Integer mappedPort = container.getMappedPort(ZooKeeperContainer.DEFAULT_CLIENT_PORT); + String zkAddr = host + ":" + mappedPort; + SpringApplication server = new SpringApplication(CIMServerApplication.class); + server.run("--app.zk.addr=" + zkAddr); + + SpringApplication route = new SpringApplication(RouteApplication.class); + String[] args = new String[]{ + "--spring.data.redis.host=" + redis.getHost(), + "--spring.data.redis.port=" + redis.getMappedPort(6379), + "--app.zk.addr=" + zkAddr, + }; + route.setAdditionalProfiles("route"); + route.run(args); + // register user + String userName = "crossoverJie"; + RouteApi routeApi = com.crossoverjie.cim.route.util.SpringBeanFactory.getBean(RouteApi.class); + BaseResponse account = + routeApi.registerAccount(RegisterInfoReqVO.builder() + .userName(userName) + .build()); + + + SpringApplication client = new SpringApplication(CIMClientApplication.class); + client.setAdditionalProfiles("client"); + args = new String[]{ + "--cim.user.id=" + account.getDataBody().getUserId(), + "--cim.user.userName=" + userName + }; + client.run(args); + MsgHandle msgHandle = SpringBeanFactory.getBean(MsgHandle.class); + msgHandle.innerCommand(":olu"); + + } + +} diff --git a/cim-integration-test/src/test/resources/application-client.yaml b/cim-integration-test/src/test/resources/application-client.yaml new file mode 100644 index 00000000..8131eef9 --- /dev/null +++ b/cim-integration-test/src/test/resources/application-client.yaml @@ -0,0 +1,37 @@ +spring: + application: + name: cim-client + +# web port +server: + port: 8082 + +logging: + level: + root: error + +# enable swagger +springdoc: + swagger-ui: + enabled: true + +# log path +cim: + msg: + logger: + path: /opt/logs/cim/ + route: + url: http://localhost:8083 # route url suggested that this is Nginx address + user: # cim userId and userName + id: 1722343979085 + userName: zhangsan + callback: + thread: + queue: + size: 2 + pool: + size: 2 + heartbeat: + time: 60 # cim heartbeat time (seconds) + reconnect: + count: 3 \ No newline at end of file diff --git a/cim-integration-test/src/test/resources/application-route.yaml b/cim-integration-test/src/test/resources/application-route.yaml new file mode 100644 index 00000000..bfd1a5f0 --- /dev/null +++ b/cim-integration-test/src/test/resources/application-route.yaml @@ -0,0 +1,49 @@ +spring: + application: + name: + cim-forward-route + data: + redis: + host: 127.0.0.1 + port: 6379 + jedis: + pool: + max-active: 100 + max-idle: 100 + max-wait: 1000 + min-idle: 10 +# web port +server: + port: 8083 + +logging: + level: + root: info + + # enable swagger +springdoc: + swagger-ui: + enabled: true + +app: + zk: + connect: + timeout: 30000 + root: /route + + # route strategy + #app.route.way=com.crossoverjie.cim.common.route.algorithm.loop.LoopHandle + + # route strategy + #app.route.way=com.crossoverjie.cim.common.route.algorithm.random.RandomHandle + + # route strategy + route: + way: + handler: com.crossoverjie.cim.common.route.algorithm.consistenthash.ConsistentHashHandle + + #app.route.way.consitenthash=com.crossoverjie.cim.common.route.algorithm.consistenthash.SortArrayMapConsistentHash + + consitenthash: com.crossoverjie.cim.common.route.algorithm.consistenthash.TreeMapConsistentHash + + diff --git a/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/RegisterInfoReqVO.java b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/RegisterInfoReqVO.java index 6417e3ff..ffd51808 100644 --- a/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/RegisterInfoReqVO.java +++ b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/RegisterInfoReqVO.java @@ -4,6 +4,8 @@ import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.NotNull; +import lombok.Builder; +import lombok.Data; /** * Function: @@ -12,6 +14,7 @@ * Date: 2018/12/23 22:04 * @since JDK 1.8 */ +@Builder public class RegisterInfoReqVO extends BaseRequest { @NotNull(message = "用户名不能为空") diff --git a/cim-server/src/main/java/com/crossoverjie/cim/server/config/BeanConfig.java b/cim-server/src/main/java/com/crossoverjie/cim/server/config/BeanConfig.java index cd33161d..728d5d72 100644 --- a/cim-server/src/main/java/com/crossoverjie/cim/server/config/BeanConfig.java +++ b/cim-server/src/main/java/com/crossoverjie/cim/server/config/BeanConfig.java @@ -24,14 +24,6 @@ @Configuration public class BeanConfig { - @Autowired - private AppConfiguration appConfiguration ; - - @Bean - public ZkClient buildZKClient(){ - return new ZkClient(appConfiguration.getZkAddr(), appConfiguration.getZkConnectTimeout()); - } - /** * http client * @return okHttp diff --git a/cim-server/src/main/java/com/crossoverjie/cim/server/kit/RegistryZK.java b/cim-server/src/main/java/com/crossoverjie/cim/server/kit/RegistryZK.java index 7fa86086..72d173bc 100644 --- a/cim-server/src/main/java/com/crossoverjie/cim/server/kit/RegistryZK.java +++ b/cim-server/src/main/java/com/crossoverjie/cim/server/kit/RegistryZK.java @@ -20,8 +20,6 @@ public class RegistryZK implements Runnable { - private ZKit zKit; - private MetaStore metaStore; private AppConfiguration appConfiguration ; @@ -33,7 +31,6 @@ public RegistryZK(MetaStore metaStore, String ip, int cimServerPort,int httpPort this.ip = ip; this.cimServerPort = cimServerPort; this.httpPort = httpPort ; - zKit = SpringBeanFactory.getBean(ZKit.class) ; this.metaStore = metaStore; appConfiguration = SpringBeanFactory.getBean(AppConfiguration.class) ; } diff --git a/cim-server/src/main/java/com/crossoverjie/cim/server/kit/ZKit.java b/cim-server/src/main/java/com/crossoverjie/cim/server/kit/ZKit.java deleted file mode 100644 index 6a9839c9..00000000 --- a/cim-server/src/main/java/com/crossoverjie/cim/server/kit/ZKit.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.crossoverjie.cim.server.kit; - -import com.crossoverjie.cim.server.config.AppConfiguration; -import lombok.extern.slf4j.Slf4j; -import org.I0Itec.zkclient.ZkClient; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -/** - * Function: Zookeeper 工具 - * - * @author crossoverJie - * Date: 2018/8/19 00:33 - * @since JDK 1.8 - */ -@Component -@Slf4j -public class ZKit { - - @Autowired - private ZkClient zkClient; - - @Autowired - private AppConfiguration appConfiguration ; - - /** - * 创建父级节点 - */ - public void createRootNode(){ - boolean exists = zkClient.exists(appConfiguration.getZkRoot()); - if (exists){ - return; - } - - //创建 root - zkClient.createPersistent(appConfiguration.getZkRoot()) ; - } - - /** - * 写入指定节点 临时目录 - * - * @param path - */ - public void createNode(String path) { - zkClient.createEphemeral(path); - } - -} diff --git a/cim-server/src/main/resources/banner.txt b/cim-server/src/main/resources/banner.txt index 97b15b91..1fc2e873 100644 --- a/cim-server/src/main/resources/banner.txt +++ b/cim-server/src/main/resources/banner.txt @@ -1,14 +1,8 @@ - ▄████▄ ██▓ ███▄ ▄███▓ -▒██▀ ▀█ ▓██▒▓██▒▀█▀ ██▒ -▒▓█ ▄ ▒██▒▓██ ▓██░ -▒▓▓▄ ▄██▒░██░▒██ ▒██ -▒ ▓███▀ ░░██░▒██▒ ░██▒ -░ ░▒ ▒ ░░▓ ░ ▒░ ░ ░ - ░ ▒ ▒ ░░ ░ ░ -░ ▒ ░░ ░ -░ ░ ░ ░ -░ + _ + ____(_)_ _ ___ ___ _____ _____ ____ +/ __/ / ' \ (_-cim-forward-route cim-rout-api cim-server-api - + cim-integration-test + @@ -175,6 +176,21 @@ 3.0.0 + + com.clever-cloud + testcontainers-zookeeper + 0.1.3 + test + + + + com.redis + testcontainers-redis + 2.2.2 + test + + + From dac5cc0c5626569c9f31d541b1aeb63d141e7bf8 Mon Sep 17 00:00:00 2001 From: crossoverJie Date: Sun, 1 Sep 2024 13:55:20 +0800 Subject: [PATCH 2/7] support integration test --- README.md | 4 +++- cim-client/pom.xml | 16 -------------- cim-common/pom.xml | 2 +- cim-forward-route/pom.xml | 16 -------------- cim-integration-test/pom.xml | 22 ++++++++++++++++--- .../java/com/crossoverjie/netty/Main.java | 15 +++++++++++++ .../crossoverjie/cim/client/ClientTest.java | 17 +++----------- cim-rout-api/pom.xml | 2 +- .../route/api/vo/req/RegisterInfoReqVO.java | 1 - cim-server-api/pom.xml | 2 +- cim-server/pom.xml | 16 -------------- pom.xml | 6 ++--- script/build.sh | 1 + 13 files changed, 47 insertions(+), 73 deletions(-) create mode 100644 script/build.sh diff --git a/README.md b/README.md index 03207f6d..9e22f012 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,9 @@ docker run --rm --name redis -d -p 6379:6379 redis:7.4.0 ```shell git clone https://github.com/crossoverJie/cim.git cd cim -mvn -Dmaven.test.skip=true clean package +mvn clean package -DskipTests=true +cd cim-server && cim-client && cim-forward-route +mvn clean package spring-boot:repackage -DskipTests=true ``` ### 部署 IM-server(cim-server) diff --git a/cim-client/pom.xml b/cim-client/pom.xml index b85449be..ba3958ca 100644 --- a/cim-client/pom.xml +++ b/cim-client/pom.xml @@ -88,21 +88,5 @@ - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - repackage - - - - - - \ No newline at end of file diff --git a/cim-common/pom.xml b/cim-common/pom.xml index ed8e4981..78308209 100644 --- a/cim-common/pom.xml +++ b/cim-common/pom.xml @@ -8,7 +8,7 @@ 1.0.0-SNAPSHOT 4.0.0 - 1.0.1-SNAPSHOT + 1.0.0-SNAPSHOT cim-common diff --git a/cim-forward-route/pom.xml b/cim-forward-route/pom.xml index fa6c4089..8807a038 100644 --- a/cim-forward-route/pom.xml +++ b/cim-forward-route/pom.xml @@ -88,21 +88,5 @@ - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - repackage - - - - - - \ No newline at end of file diff --git a/cim-integration-test/pom.xml b/cim-integration-test/pom.xml index 015367fc..992687d4 100644 --- a/cim-integration-test/pom.xml +++ b/cim-integration-test/pom.xml @@ -22,21 +22,26 @@ com.crossoverjie.netty cim-server ${project.version} + compile + com.crossoverjie.netty cim-forward-route ${project.version} + compile + com.crossoverjie.netty cim-client ${project.version} + compile - org.apache.zookeeper - zookeeper + org.junit.vintage + junit-vintage-engine test @@ -74,7 +79,6 @@ - @@ -83,6 +87,18 @@ org.apache.maven.plugins maven-surefire-plugin + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + \ No newline at end of file diff --git a/cim-integration-test/src/main/java/com/crossoverjie/netty/Main.java b/cim-integration-test/src/main/java/com/crossoverjie/netty/Main.java index 72a55739..5a360fc9 100644 --- a/cim-integration-test/src/main/java/com/crossoverjie/netty/Main.java +++ b/cim-integration-test/src/main/java/com/crossoverjie/netty/Main.java @@ -1,7 +1,22 @@ package com.crossoverjie.netty; +import com.crossoverjie.cim.client.CIMClientApplication; +import com.crossoverjie.cim.client.service.MsgHandle; +import com.crossoverjie.cim.client.util.SpringBeanFactory; +import com.crossoverjie.cim.common.res.BaseResponse; +import com.crossoverjie.cim.route.RouteApplication; +import com.crossoverjie.cim.server.CIMServerApplication; +import lombok.extern.slf4j.Slf4j; + +@Slf4j public class Main { public static void main(String[] args) { + System.out.println(MsgHandle.class); + System.out.println(SpringBeanFactory.class); + System.out.println(BaseResponse.class); + System.out.println(RouteApplication.class); + System.out.println(CIMServerApplication.class); + System.out.println(CIMClientApplication.class); System.out.println("Hello world!"); } } \ No newline at end of file diff --git a/cim-integration-test/src/test/java/com/crossoverjie/cim/client/ClientTest.java b/cim-integration-test/src/test/java/com/crossoverjie/cim/client/ClientTest.java index 6b7cbfce..42b94e35 100644 --- a/cim-integration-test/src/test/java/com/crossoverjie/cim/client/ClientTest.java +++ b/cim-integration-test/src/test/java/com/crossoverjie/cim/client/ClientTest.java @@ -9,22 +9,11 @@ import com.crossoverjie.cim.route.api.vo.req.RegisterInfoReqVO; import com.crossoverjie.cim.route.api.vo.res.RegisterInfoResVO; import com.crossoverjie.cim.server.CIMServerApplication; -import com.crossoverjie.cim.server.config.AppConfiguration; import com.redis.testcontainers.RedisContainer; -import jakarta.annotation.Resource; -import java.lang.reflect.Field; import java.time.Duration; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CompletableFuture; import lombok.extern.slf4j.Slf4j; import org.junit.Test; -import org.junit.runner.RunWith; import org.springframework.boot.SpringApplication; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringRunner; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.utility.DockerImageName; @@ -63,10 +52,10 @@ public void olu() throws Exception { // register user String userName = "crossoverJie"; RouteApi routeApi = com.crossoverjie.cim.route.util.SpringBeanFactory.getBean(RouteApi.class); + RegisterInfoReqVO reqVO = new RegisterInfoReqVO(); + reqVO.setUserName(userName); BaseResponse account = - routeApi.registerAccount(RegisterInfoReqVO.builder() - .userName(userName) - .build()); + routeApi.registerAccount(reqVO); SpringApplication client = new SpringApplication(CIMClientApplication.class); diff --git a/cim-rout-api/pom.xml b/cim-rout-api/pom.xml index a1164fed..efe9f7b2 100644 --- a/cim-rout-api/pom.xml +++ b/cim-rout-api/pom.xml @@ -10,7 +10,7 @@ 4.0.0 cim-rout-api - 1.0.1-SNAPSHOT + 1.0.0-SNAPSHOT diff --git a/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/RegisterInfoReqVO.java b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/RegisterInfoReqVO.java index ffd51808..712a5e25 100644 --- a/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/RegisterInfoReqVO.java +++ b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/RegisterInfoReqVO.java @@ -14,7 +14,6 @@ * Date: 2018/12/23 22:04 * @since JDK 1.8 */ -@Builder public class RegisterInfoReqVO extends BaseRequest { @NotNull(message = "用户名不能为空") diff --git a/cim-server-api/pom.xml b/cim-server-api/pom.xml index 57897840..634775a9 100644 --- a/cim-server-api/pom.xml +++ b/cim-server-api/pom.xml @@ -10,7 +10,7 @@ 4.0.0 cim-server-api - 1.0.1-SNAPSHOT + 1.0.0-SNAPSHOT diff --git a/cim-server/pom.xml b/cim-server/pom.xml index de73ab52..43e5cab5 100644 --- a/cim-server/pom.xml +++ b/cim-server/pom.xml @@ -88,21 +88,5 @@ - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - repackage - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index 78b749d9..df936b6b 100644 --- a/pom.xml +++ b/pom.xml @@ -117,19 +117,19 @@ com.crossoverjie.netty cim-common - 1.0.1-SNAPSHOT + 1.0.0-SNAPSHOT com.crossoverjie.netty cim-rout-api - 1.0.1-SNAPSHOT + 1.0.0-SNAPSHOT com.crossoverjie.netty cim-server-api - 1.0.1-SNAPSHOT + 1.0.0-SNAPSHOT diff --git a/script/build.sh b/script/build.sh new file mode 100644 index 00000000..d463deb5 --- /dev/null +++ b/script/build.sh @@ -0,0 +1 @@ +# todo build \ No newline at end of file From e8060a2b97508b8b3e79441cb38ea1855cd9d895 Mon Sep 17 00:00:00 2001 From: crossoverJie Date: Sun, 1 Sep 2024 15:49:57 +0800 Subject: [PATCH 3/7] :recycle: Refactoring code. --- .../java/com/crossoverjie/netty/Main.java | 12 --- .../crossoverjie/cim/client/ClientTest.java | 75 ++++++------------- .../cim/route/AbstractRouteBaseTest.java | 42 +++++++++++ .../cim/server/AbstractServerBaseTest.java | 33 ++++++++ 4 files changed, 99 insertions(+), 63 deletions(-) create mode 100644 cim-integration-test/src/test/java/com/crossoverjie/cim/route/AbstractRouteBaseTest.java create mode 100644 cim-integration-test/src/test/java/com/crossoverjie/cim/server/AbstractServerBaseTest.java diff --git a/cim-integration-test/src/main/java/com/crossoverjie/netty/Main.java b/cim-integration-test/src/main/java/com/crossoverjie/netty/Main.java index 5a360fc9..9ad0290f 100644 --- a/cim-integration-test/src/main/java/com/crossoverjie/netty/Main.java +++ b/cim-integration-test/src/main/java/com/crossoverjie/netty/Main.java @@ -1,22 +1,10 @@ package com.crossoverjie.netty; -import com.crossoverjie.cim.client.CIMClientApplication; -import com.crossoverjie.cim.client.service.MsgHandle; -import com.crossoverjie.cim.client.util.SpringBeanFactory; -import com.crossoverjie.cim.common.res.BaseResponse; -import com.crossoverjie.cim.route.RouteApplication; -import com.crossoverjie.cim.server.CIMServerApplication; import lombok.extern.slf4j.Slf4j; @Slf4j public class Main { public static void main(String[] args) { - System.out.println(MsgHandle.class); - System.out.println(SpringBeanFactory.class); - System.out.println(BaseResponse.class); - System.out.println(RouteApplication.class); - System.out.println(CIMServerApplication.class); - System.out.println(CIMClientApplication.class); System.out.println("Hello world!"); } } \ No newline at end of file diff --git a/cim-integration-test/src/test/java/com/crossoverjie/cim/client/ClientTest.java b/cim-integration-test/src/test/java/com/crossoverjie/cim/client/ClientTest.java index 42b94e35..22ebbeaa 100644 --- a/cim-integration-test/src/test/java/com/crossoverjie/cim/client/ClientTest.java +++ b/cim-integration-test/src/test/java/com/crossoverjie/cim/client/ClientTest.java @@ -1,73 +1,46 @@ package com.crossoverjie.cim.client; -import com.clevercloud.testcontainers.zookeeper.ZooKeeperContainer; import com.crossoverjie.cim.client.service.MsgHandle; import com.crossoverjie.cim.client.util.SpringBeanFactory; -import com.crossoverjie.cim.common.res.BaseResponse; -import com.crossoverjie.cim.route.RouteApplication; -import com.crossoverjie.cim.route.api.RouteApi; -import com.crossoverjie.cim.route.api.vo.req.RegisterInfoReqVO; -import com.crossoverjie.cim.route.api.vo.res.RegisterInfoResVO; -import com.crossoverjie.cim.server.CIMServerApplication; -import com.redis.testcontainers.RedisContainer; -import java.time.Duration; +import com.crossoverjie.cim.route.AbstractRouteBaseTest; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.springframework.boot.SpringApplication; -import org.testcontainers.junit.jupiter.Container; -import org.testcontainers.utility.DockerImageName; @Slf4j -public class ClientTest { - private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName - .parse("zookeeper") - .withTag("3.9.2"); - - private static final Duration DEFAULT_STARTUP_TIMEOUT = Duration.ofSeconds(60); - - @Container - public final ZooKeeperContainer container = new ZooKeeperContainer(DEFAULT_IMAGE_NAME, DEFAULT_STARTUP_TIMEOUT); - - @Container - RedisContainer redis = new RedisContainer(DockerImageName.parse("redis:7.4.0")); - - @Test - public void olu() throws Exception { - container.start(); - redis.start(); - String host = container.getHost(); - Integer mappedPort = container.getMappedPort(ZooKeeperContainer.DEFAULT_CLIENT_PORT); - String zkAddr = host + ":" + mappedPort; - SpringApplication server = new SpringApplication(CIMServerApplication.class); - server.run("--app.zk.addr=" + zkAddr); - - SpringApplication route = new SpringApplication(RouteApplication.class); - String[] args = new String[]{ - "--spring.data.redis.host=" + redis.getHost(), - "--spring.data.redis.port=" + redis.getMappedPort(6379), - "--app.zk.addr=" + zkAddr, - }; - route.setAdditionalProfiles("route"); - route.run(args); - // register user - String userName = "crossoverJie"; - RouteApi routeApi = com.crossoverjie.cim.route.util.SpringBeanFactory.getBean(RouteApi.class); - RegisterInfoReqVO reqVO = new RegisterInfoReqVO(); - reqVO.setUserName(userName); - BaseResponse account = - routeApi.registerAccount(reqVO); +public class ClientTest extends AbstractRouteBaseTest { + private void login(String userName, int port) throws Exception { + Long userId = super.registerAccount(userName); SpringApplication client = new SpringApplication(CIMClientApplication.class); client.setAdditionalProfiles("client"); - args = new String[]{ - "--cim.user.id=" + account.getDataBody().getUserId(), + String[] args = new String[]{ + "--server.port=" + port, + "--cim.user.id=" + userId, "--cim.user.userName=" + userName }; client.run(args); + } + + @Test + public void olu() throws Exception { + super.startServer(); + super.startRoute(); + this.login("crossoverJie", 8082); + this.login("cj", 8182); MsgHandle msgHandle = SpringBeanFactory.getBean(MsgHandle.class); msgHandle.innerCommand(":olu"); + } + @Test + public void groupChat() throws Exception { + super.startServer(); + super.startRoute(); + this.login("crossoverJie", 8082); + this.login("cj", 8182); + MsgHandle msgHandle = SpringBeanFactory.getBean(MsgHandle.class); + msgHandle.sendMsg("hello"); } } diff --git a/cim-integration-test/src/test/java/com/crossoverjie/cim/route/AbstractRouteBaseTest.java b/cim-integration-test/src/test/java/com/crossoverjie/cim/route/AbstractRouteBaseTest.java new file mode 100644 index 00000000..ee5a9564 --- /dev/null +++ b/cim-integration-test/src/test/java/com/crossoverjie/cim/route/AbstractRouteBaseTest.java @@ -0,0 +1,42 @@ +package com.crossoverjie.cim.route; + +import com.clevercloud.testcontainers.zookeeper.ZooKeeperContainer; +import com.crossoverjie.cim.common.res.BaseResponse; +import com.crossoverjie.cim.route.api.RouteApi; +import com.crossoverjie.cim.route.api.vo.req.RegisterInfoReqVO; +import com.crossoverjie.cim.route.api.vo.res.RegisterInfoResVO; +import com.crossoverjie.cim.server.AbstractServerBaseTest; +import com.crossoverjie.cim.server.CIMServerApplication; +import com.redis.testcontainers.RedisContainer; +import org.junit.Before; +import org.springframework.boot.SpringApplication; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.utility.DockerImageName; + +public abstract class AbstractRouteBaseTest extends AbstractServerBaseTest { + + @Container + RedisContainer redis = new RedisContainer(DockerImageName.parse("redis:7.4.0")); + + public void startRoute() { + redis.start(); + SpringApplication route = new SpringApplication(RouteApplication.class); + String[] args = new String[]{ + "--spring.data.redis.host=" + redis.getHost(), + "--spring.data.redis.port=" + redis.getMappedPort(6379), + "--app.zk.addr=" + super.getZookeeperAddr(), + }; + route.setAdditionalProfiles("route"); + route.run(args); + } + + public Long registerAccount(String userName) throws Exception { + // register user + RouteApi routeApi = com.crossoverjie.cim.route.util.SpringBeanFactory.getBean(RouteApi.class); + RegisterInfoReqVO reqVO = new RegisterInfoReqVO(); + reqVO.setUserName(userName); + BaseResponse account = + routeApi.registerAccount(reqVO); + return account.getDataBody().getUserId(); + } +} diff --git a/cim-integration-test/src/test/java/com/crossoverjie/cim/server/AbstractServerBaseTest.java b/cim-integration-test/src/test/java/com/crossoverjie/cim/server/AbstractServerBaseTest.java new file mode 100644 index 00000000..efd7a73a --- /dev/null +++ b/cim-integration-test/src/test/java/com/crossoverjie/cim/server/AbstractServerBaseTest.java @@ -0,0 +1,33 @@ +package com.crossoverjie.cim.server; + +import com.clevercloud.testcontainers.zookeeper.ZooKeeperContainer; +import java.time.Duration; +import lombok.Getter; +import org.junit.Before; +import org.springframework.boot.SpringApplication; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.utility.DockerImageName; + +public abstract class AbstractServerBaseTest { + + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName + .parse("zookeeper") + .withTag("3.9.2"); + + private static final Duration DEFAULT_STARTUP_TIMEOUT = Duration.ofSeconds(60); + + @Container + public final ZooKeeperContainer + zooKeeperContainer = new ZooKeeperContainer(DEFAULT_IMAGE_NAME, DEFAULT_STARTUP_TIMEOUT); + + @Getter + private String zookeeperAddr; + + public void startServer() { + zooKeeperContainer.start(); + zookeeperAddr = String.format("%s:%d", zooKeeperContainer.getHost(), zooKeeperContainer.getMappedPort(ZooKeeperContainer.DEFAULT_CLIENT_PORT)); + SpringApplication server = new SpringApplication(CIMServerApplication.class); + server.run("--app.zk.addr=" + zookeeperAddr); + } + +} From d488835b31ff8b1e6cc4039b123856af0ef669f4 Mon Sep 17 00:00:00 2001 From: crossoverJie Date: Sun, 1 Sep 2024 15:50:18 +0800 Subject: [PATCH 4/7] remove code --- .../src/main/java/com/crossoverjie/netty/Main.java | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 cim-integration-test/src/main/java/com/crossoverjie/netty/Main.java diff --git a/cim-integration-test/src/main/java/com/crossoverjie/netty/Main.java b/cim-integration-test/src/main/java/com/crossoverjie/netty/Main.java deleted file mode 100644 index 9ad0290f..00000000 --- a/cim-integration-test/src/main/java/com/crossoverjie/netty/Main.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.crossoverjie.netty; - -import lombok.extern.slf4j.Slf4j; - -@Slf4j -public class Main { - public static void main(String[] args) { - System.out.println("Hello world!"); - } -} \ No newline at end of file From 16ebfd799984e1268a7667dae26d5b107ce8c905 Mon Sep 17 00:00:00 2001 From: crossoverJie Date: Sun, 1 Sep 2024 15:52:22 +0800 Subject: [PATCH 5/7] remove code --- .../java/com/crossoverjie/cim/client/ClientTest.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/cim-integration-test/src/test/java/com/crossoverjie/cim/client/ClientTest.java b/cim-integration-test/src/test/java/com/crossoverjie/cim/client/ClientTest.java index 22ebbeaa..a916598f 100644 --- a/cim-integration-test/src/test/java/com/crossoverjie/cim/client/ClientTest.java +++ b/cim-integration-test/src/test/java/com/crossoverjie/cim/client/ClientTest.java @@ -31,16 +31,8 @@ public void olu() throws Exception { this.login("cj", 8182); MsgHandle msgHandle = SpringBeanFactory.getBean(MsgHandle.class); msgHandle.innerCommand(":olu"); - } - - @Test - public void groupChat() throws Exception { - super.startServer(); - super.startRoute(); - this.login("crossoverJie", 8082); - this.login("cj", 8182); - MsgHandle msgHandle = SpringBeanFactory.getBean(MsgHandle.class); msgHandle.sendMsg("hello"); } + } From 85ee0fbd4e5bad1c46f27992f2673f2b92dec206 Mon Sep 17 00:00:00 2001 From: crossoverJie Date: Sun, 1 Sep 2024 16:01:01 +0800 Subject: [PATCH 6/7] remove plugin --- cim-integration-test/pom.xml | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/cim-integration-test/pom.xml b/cim-integration-test/pom.xml index 992687d4..7017b58e 100644 --- a/cim-integration-test/pom.xml +++ b/cim-integration-test/pom.xml @@ -87,18 +87,6 @@ org.apache.maven.plugins maven-surefire-plugin - - - org.springframework.boot - spring-boot-maven-plugin - - - - repackage - - - - \ No newline at end of file From a13b664db2cbab9218a76f61334d5ec2e512d409 Mon Sep 17 00:00:00 2001 From: crossoverJie Date: Sun, 1 Sep 2024 16:04:00 +0800 Subject: [PATCH 7/7] readme --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9e22f012..bbd4fa1d 100644 --- a/README.md +++ b/README.md @@ -17,15 +17,16 @@ # V2.0 - [x] Upgrade to JDK17 & springboot3.0 +- [x] Client SDK - [ ] Client use [picocli](https://picocli.info/) instead of springboot. -- [ ] Supports binary client(build with golang). -- [ ] Support integration testing. +- [x] Support integration testing. - [ ] Integrate OpenTelemetry . - [ ] Support single node startup(Contains no components). - [ ] Third-party components support replacement(Redis/Zookeeper, etc.). - [ ] Support web client(websocket). - [ ] Support docker container. - [ ] Support kubernetes operation. +- [ ] Supports binary client(build with golang). ## 介绍