正在显示
16 个修改的文件
包含
11 行增加
和
673 行删除
| @@ -6,7 +6,7 @@ server.port=8082 | @@ -6,7 +6,7 @@ server.port=8082 | ||
| 6 | # 是否打开swagger | 6 | # 是否打开swagger |
| 7 | swagger.enable = true | 7 | swagger.enable = true |
| 8 | 8 | ||
| 9 | -logging.level.root=info | 9 | +logging.level.root=error |
| 10 | 10 | ||
| 11 | #消息记录存放路径 | 11 | #消息记录存放路径 |
| 12 | cim.msg.logger.path = /opt/logs/cim/ | 12 | cim.msg.logger.path = /opt/logs/cim/ |
| @@ -4,8 +4,6 @@ package com.crossoverjie.cim.server.test; | @@ -4,8 +4,6 @@ package com.crossoverjie.cim.server.test; | ||
| 4 | import com.alibaba.fastjson.JSON; | 4 | import com.alibaba.fastjson.JSON; |
| 5 | import com.crossoverjie.cim.client.vo.res.CIMServerResVO; | 5 | import com.crossoverjie.cim.client.vo.res.CIMServerResVO; |
| 6 | import com.crossoverjie.cim.client.vo.res.OnlineUsersResVO; | 6 | import com.crossoverjie.cim.client.vo.res.OnlineUsersResVO; |
| 7 | -import com.vdurmont.emoji.Emoji; | ||
| 8 | -import com.vdurmont.emoji.EmojiManager; | ||
| 9 | import com.vdurmont.emoji.EmojiParser; | 7 | import com.vdurmont.emoji.EmojiParser; |
| 10 | import org.junit.Test; | 8 | import org.junit.Test; |
| 11 | import org.slf4j.Logger; | 9 | import org.slf4j.Logger; |
| @@ -21,7 +19,6 @@ import java.nio.file.StandardOpenOption; | @@ -21,7 +19,6 @@ import java.nio.file.StandardOpenOption; | ||
| 21 | import java.time.LocalDate; | 19 | import java.time.LocalDate; |
| 22 | import java.util.ArrayList; | 20 | import java.util.ArrayList; |
| 23 | import java.util.Arrays; | 21 | import java.util.Arrays; |
| 24 | -import java.util.Collection; | ||
| 25 | import java.util.List; | 22 | import java.util.List; |
| 26 | 23 | ||
| 27 | /** | 24 | /** |
| @@ -199,11 +196,11 @@ public class CommonTest { | @@ -199,11 +196,11 @@ public class CommonTest { | ||
| 199 | 196 | ||
| 200 | result = EmojiParser.parseToAliases(str); | 197 | result = EmojiParser.parseToAliases(str); |
| 201 | System.out.println(result); | 198 | System.out.println(result); |
| 202 | - | ||
| 203 | - Collection<Emoji> all = EmojiManager.getAll(); | ||
| 204 | - for (Emoji emoji : all) { | ||
| 205 | - System.out.println(EmojiParser.parseToAliases(emoji.getUnicode()) + "--->" + emoji.getUnicode() ); | ||
| 206 | - } | 199 | +// |
| 200 | +// Collection<Emoji> all = EmojiManager.getAll(); | ||
| 201 | +// for (Emoji emoji : all) { | ||
| 202 | +// System.out.println(EmojiParser.parseToAliases(emoji.getUnicode()) + "--->" + emoji.getUnicode() ); | ||
| 203 | +// } | ||
| 207 | 204 | ||
| 208 | } | 205 | } |
| 209 | 206 |
| @@ -13,7 +13,7 @@ logging.level.root=info | @@ -13,7 +13,7 @@ logging.level.root=info | ||
| 13 | management.security.enabled=false | 13 | management.security.enabled=false |
| 14 | 14 | ||
| 15 | # zk 地址 | 15 | # zk 地址 |
| 16 | -app.zk.addr=47.98.194.60:2182 | 16 | +app.zk.addr=ip:port |
| 17 | 17 | ||
| 18 | # zk 连接超时时限 | 18 | # zk 连接超时时限 |
| 19 | app.zk.connect.timeout=15000 | 19 | app.zk.connect.timeout=15000 |
| @@ -37,10 +37,11 @@ app.route.way=com.crossoverjie.cim.common.route.algorithm.consistenthash.Consist | @@ -37,10 +37,11 @@ app.route.way=com.crossoverjie.cim.common.route.algorithm.consistenthash.Consist | ||
| 37 | app.route.way.consitenthash=com.crossoverjie.cim.common.route.algorithm.consistenthash.TreeMapConsistentHash | 37 | app.route.way.consitenthash=com.crossoverjie.cim.common.route.algorithm.consistenthash.TreeMapConsistentHash |
| 38 | 38 | ||
| 39 | # Redis 配置 | 39 | # Redis 配置 |
| 40 | -spring.redis.host=47.98.194.60 | 40 | +spring.redis.host=xx |
| 41 | spring.redis.port=6379 | 41 | spring.redis.port=6379 |
| 42 | spring.redis.pool.max-active=100 | 42 | spring.redis.pool.max-active=100 |
| 43 | spring.redis.pool.max-idle=100 | 43 | spring.redis.pool.max-idle=100 |
| 44 | spring.redis.pool.max-wait=1000 | 44 | spring.redis.pool.max-wait=1000 |
| 45 | spring.redis.pool.min-idle=10 | 45 | spring.redis.pool.min-idle=10 |
| 46 | +spring.redis.password=xx | ||
| 46 | 47 |
cim-zk/pom.xml
已删除
100644 → 0
| 1 | -<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | -<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| 3 | - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 4 | - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 5 | - <parent> | ||
| 6 | - <artifactId>cim</artifactId> | ||
| 7 | - <groupId>com.crossoverjie.netty</groupId> | ||
| 8 | - <version>1.0.0-SNAPSHOT</version> | ||
| 9 | - </parent> | ||
| 10 | - <modelVersion>4.0.0</modelVersion> | ||
| 11 | - | ||
| 12 | - <artifactId>cim-zk</artifactId> | ||
| 13 | - | ||
| 14 | - | ||
| 15 | - | ||
| 16 | - <properties> | ||
| 17 | - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| 18 | - <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
| 19 | - <java.version>1.8</java.version> | ||
| 20 | - </properties> | ||
| 21 | - | ||
| 22 | - <dependencies> | ||
| 23 | - <dependency> | ||
| 24 | - <groupId>com.crossoverjie.netty</groupId> | ||
| 25 | - <artifactId>cim-common</artifactId> | ||
| 26 | - <exclusions> | ||
| 27 | - <exclusion> | ||
| 28 | - <artifactId>log4j</artifactId> | ||
| 29 | - <groupId>log4j</groupId> | ||
| 30 | - </exclusion> | ||
| 31 | - </exclusions> | ||
| 32 | - </dependency> | ||
| 33 | - | ||
| 34 | - | ||
| 35 | - <dependency> | ||
| 36 | - <groupId>io.springfox</groupId> | ||
| 37 | - <artifactId>springfox-swagger2</artifactId> | ||
| 38 | - <scope>compile</scope> | ||
| 39 | - </dependency> | ||
| 40 | - <dependency> | ||
| 41 | - <groupId>io.springfox</groupId> | ||
| 42 | - <artifactId>springfox-swagger-ui</artifactId> | ||
| 43 | - </dependency> | ||
| 44 | - | ||
| 45 | - <dependency> | ||
| 46 | - <groupId>org.springframework.boot</groupId> | ||
| 47 | - <artifactId>spring-boot-starter-web</artifactId> | ||
| 48 | - </dependency> | ||
| 49 | - <dependency> | ||
| 50 | - <groupId>org.springframework.boot</groupId> | ||
| 51 | - <artifactId>spring-boot-starter-test</artifactId> | ||
| 52 | - <scope>test</scope> | ||
| 53 | - </dependency> | ||
| 54 | - | ||
| 55 | - <dependency> | ||
| 56 | - <groupId>org.springframework.boot</groupId> | ||
| 57 | - <artifactId>spring-boot-configuration-processor</artifactId> | ||
| 58 | - <optional>true</optional> | ||
| 59 | - </dependency> | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - <dependency> | ||
| 63 | - <groupId>org.springframework.boot</groupId> | ||
| 64 | - <artifactId>spring-boot-starter-actuator</artifactId> | ||
| 65 | - </dependency> | ||
| 66 | - | ||
| 67 | - <dependency> | ||
| 68 | - <groupId>de.codecentric</groupId> | ||
| 69 | - <artifactId>spring-boot-admin-starter-client</artifactId> | ||
| 70 | - </dependency> | ||
| 71 | - | ||
| 72 | - <dependency> | ||
| 73 | - <groupId>ch.qos.logback</groupId> | ||
| 74 | - <artifactId>logback-classic</artifactId> | ||
| 75 | - </dependency> | ||
| 76 | - <dependency> | ||
| 77 | - <groupId>org.slf4j</groupId> | ||
| 78 | - <artifactId>slf4j-api</artifactId> | ||
| 79 | - </dependency> | ||
| 80 | - <dependency> | ||
| 81 | - <groupId>ch.qos.logback</groupId> | ||
| 82 | - <artifactId>logback-core</artifactId> | ||
| 83 | - </dependency> | ||
| 84 | - | ||
| 85 | - | ||
| 86 | - <dependency> | ||
| 87 | - <groupId>junit</groupId> | ||
| 88 | - <artifactId>junit</artifactId> | ||
| 89 | - </dependency> | ||
| 90 | - | ||
| 91 | - <dependency> | ||
| 92 | - <groupId>com.alibaba</groupId> | ||
| 93 | - <artifactId>fastjson</artifactId> | ||
| 94 | - </dependency> | ||
| 95 | - | ||
| 96 | - <dependency> | ||
| 97 | - <groupId>com.google.guava</groupId> | ||
| 98 | - <artifactId>guava</artifactId> | ||
| 99 | - </dependency> | ||
| 100 | - </dependencies> | ||
| 101 | - | ||
| 102 | - | ||
| 103 | - <build> | ||
| 104 | - <plugins> | ||
| 105 | - <!-- spring-boot-maven-plugin (提供了直接运行项目的插件:如果是通过parent方式继承spring-boot-starter-parent则不用此插件) --> | ||
| 106 | - <plugin> | ||
| 107 | - <groupId>org.springframework.boot</groupId> | ||
| 108 | - <artifactId>spring-boot-maven-plugin</artifactId> | ||
| 109 | - <executions> | ||
| 110 | - <execution> | ||
| 111 | - <goals> | ||
| 112 | - <goal>repackage</goal> | ||
| 113 | - </goals> | ||
| 114 | - </execution> | ||
| 115 | - </executions> | ||
| 116 | - </plugin> | ||
| 117 | - </plugins> | ||
| 118 | - </build> | ||
| 119 | - | ||
| 120 | -</project> |
| 1 | -package com.crossoverjie.cim.server.zk; | ||
| 2 | - | ||
| 3 | -import com.crossoverjie.cim.server.zk.util.AppConfiguration; | ||
| 4 | -import com.crossoverjie.cim.server.zk.thread.RegistryZK; | ||
| 5 | -import com.crossoverjie.cim.server.zk.util.ZKit; | ||
| 6 | -import org.slf4j.Logger; | ||
| 7 | -import org.slf4j.LoggerFactory; | ||
| 8 | -import org.springframework.beans.factory.annotation.Autowired; | ||
| 9 | -import org.springframework.boot.CommandLineRunner; | ||
| 10 | -import org.springframework.boot.SpringApplication; | ||
| 11 | -import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
| 12 | - | ||
| 13 | -import java.net.InetAddress; | ||
| 14 | - | ||
| 15 | -/** | ||
| 16 | - * @author crossoverJie | ||
| 17 | - */ | ||
| 18 | -@SpringBootApplication | ||
| 19 | -public class Application implements CommandLineRunner{ | ||
| 20 | - | ||
| 21 | - private final static Logger LOGGER = LoggerFactory.getLogger(Application.class); | ||
| 22 | - | ||
| 23 | - @Autowired | ||
| 24 | - private AppConfiguration appConfiguration ; | ||
| 25 | - | ||
| 26 | - @Autowired | ||
| 27 | - private static ZKit zkUtil ; | ||
| 28 | - | ||
| 29 | - public static void main(String[] args) { | ||
| 30 | - SpringApplication.run(Application.class, args); | ||
| 31 | - LOGGER.info("启动应用成功"); | ||
| 32 | - | ||
| 33 | - } | ||
| 34 | - | ||
| 35 | - @Override | ||
| 36 | - public void run(String... args) throws Exception { | ||
| 37 | - //获得本机IP | ||
| 38 | - String addr = InetAddress.getLocalHost().getHostAddress(); | ||
| 39 | - Thread thread = new Thread(new RegistryZK(addr, appConfiguration.getPort())); | ||
| 40 | - thread.setName("registry-zk"); | ||
| 41 | - //thread.start() ; | ||
| 42 | - } | ||
| 43 | -} |
| 1 | -package com.crossoverjie.cim.server.zk.cache; | ||
| 2 | - | ||
| 3 | -import com.crossoverjie.cim.server.zk.util.ZKit; | ||
| 4 | -import com.google.common.cache.LoadingCache; | ||
| 5 | -import org.springframework.beans.factory.annotation.Autowired; | ||
| 6 | -import org.springframework.stereotype.Component; | ||
| 7 | - | ||
| 8 | -import java.util.ArrayList; | ||
| 9 | -import java.util.List; | ||
| 10 | -import java.util.Map; | ||
| 11 | -import java.util.concurrent.atomic.AtomicLong; | ||
| 12 | - | ||
| 13 | -/** | ||
| 14 | - * Function: 服务器节点缓存 | ||
| 15 | - * | ||
| 16 | - * @author crossoverJie | ||
| 17 | - * Date: 2018/8/19 01:31 | ||
| 18 | - * @since JDK 1.8 | ||
| 19 | - */ | ||
| 20 | -@Component | ||
| 21 | -public class ServerCache { | ||
| 22 | - | ||
| 23 | - | ||
| 24 | - @Autowired | ||
| 25 | - private LoadingCache<String, String> cache; | ||
| 26 | - | ||
| 27 | - @Autowired | ||
| 28 | - private ZKit zkUtil; | ||
| 29 | - | ||
| 30 | - private AtomicLong index = new AtomicLong(); | ||
| 31 | - | ||
| 32 | - | ||
| 33 | - public void addCache(String key) { | ||
| 34 | - cache.put(key, key); | ||
| 35 | - } | ||
| 36 | - | ||
| 37 | - | ||
| 38 | - /** | ||
| 39 | - * 更新所有缓存/先删除 再新增 | ||
| 40 | - * | ||
| 41 | - * @param currentChilds | ||
| 42 | - */ | ||
| 43 | - public void updateCache(List<String> currentChilds) { | ||
| 44 | - cache.invalidateAll(); | ||
| 45 | - for (String currentChild : currentChilds) { | ||
| 46 | - String key = currentChild.split("-")[1]; | ||
| 47 | - addCache(key); | ||
| 48 | - } | ||
| 49 | - } | ||
| 50 | - | ||
| 51 | - | ||
| 52 | - /** | ||
| 53 | - * 获取所有的服务列表 | ||
| 54 | - * | ||
| 55 | - * @return | ||
| 56 | - */ | ||
| 57 | - public List<String> getAll() { | ||
| 58 | - | ||
| 59 | - List<String> list = new ArrayList<>(); | ||
| 60 | - | ||
| 61 | - if (cache.size() == 0) { | ||
| 62 | - List<String> allNode = zkUtil.getAllNode(); | ||
| 63 | - for (String node : allNode) { | ||
| 64 | - String key = node.split("-")[1]; | ||
| 65 | - addCache(key); | ||
| 66 | - } | ||
| 67 | - } | ||
| 68 | - for (Map.Entry<String, String> entry : cache.asMap().entrySet()) { | ||
| 69 | - list.add(entry.getKey()); | ||
| 70 | - } | ||
| 71 | - return list; | ||
| 72 | - | ||
| 73 | - } | ||
| 74 | - | ||
| 75 | - /** | ||
| 76 | - * 选取服务器 | ||
| 77 | - * | ||
| 78 | - * @return | ||
| 79 | - */ | ||
| 80 | - public String selectServer() { | ||
| 81 | - List<String> all = getAll(); | ||
| 82 | - if (all.size() == 0) { | ||
| 83 | - throw new RuntimeException("路由列表为空"); | ||
| 84 | - } | ||
| 85 | - Long position = index.incrementAndGet() % all.size(); | ||
| 86 | - if (position < 0) { | ||
| 87 | - position = 0L; | ||
| 88 | - } | ||
| 89 | - | ||
| 90 | - return all.get(position.intValue()); | ||
| 91 | - } | ||
| 92 | -} |
| 1 | -package com.crossoverjie.cim.server.zk.config; | ||
| 2 | - | ||
| 3 | -import com.crossoverjie.cim.server.zk.util.AppConfiguration; | ||
| 4 | -import com.google.common.cache.CacheBuilder; | ||
| 5 | -import com.google.common.cache.CacheLoader; | ||
| 6 | -import com.google.common.cache.LoadingCache; | ||
| 7 | -import org.I0Itec.zkclient.ZkClient; | ||
| 8 | -import org.springframework.beans.factory.annotation.Autowired; | ||
| 9 | -import org.springframework.context.annotation.Bean; | ||
| 10 | -import org.springframework.context.annotation.Configuration; | ||
| 11 | - | ||
| 12 | -/** | ||
| 13 | - * Function: | ||
| 14 | - * | ||
| 15 | - * @author crossoverJie | ||
| 16 | - * Date: 2018/8/24 01:28 | ||
| 17 | - * @since JDK 1.8 | ||
| 18 | - */ | ||
| 19 | -@Configuration | ||
| 20 | -public class AppConfig { | ||
| 21 | - | ||
| 22 | - @Autowired | ||
| 23 | - private AppConfiguration appConfiguration ; | ||
| 24 | - | ||
| 25 | - @Bean | ||
| 26 | - public ZkClient buildZKClient(){ | ||
| 27 | - return new ZkClient(appConfiguration.getZkAddr(), 5000); | ||
| 28 | - } | ||
| 29 | - | ||
| 30 | - | ||
| 31 | - @Bean | ||
| 32 | - public LoadingCache<String,String> buildCache(){ | ||
| 33 | - return CacheBuilder.newBuilder() | ||
| 34 | - .build(new CacheLoader<String, String>() { | ||
| 35 | - @Override | ||
| 36 | - public String load(String s) throws Exception { | ||
| 37 | - return null; | ||
| 38 | - } | ||
| 39 | - }); | ||
| 40 | - } | ||
| 41 | -} |
| 1 | -package com.crossoverjie.cim.server.zk.config; | ||
| 2 | - | ||
| 3 | -import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; | ||
| 4 | -import org.springframework.context.annotation.Bean; | ||
| 5 | -import org.springframework.context.annotation.Configuration; | ||
| 6 | -import springfox.documentation.builders.ApiInfoBuilder; | ||
| 7 | -import springfox.documentation.builders.PathSelectors; | ||
| 8 | -import springfox.documentation.builders.RequestHandlerSelectors; | ||
| 9 | -import springfox.documentation.service.ApiInfo; | ||
| 10 | -import springfox.documentation.spi.DocumentationType; | ||
| 11 | -import springfox.documentation.spring.web.plugins.Docket; | ||
| 12 | -import springfox.documentation.swagger2.annotations.EnableSwagger2; | ||
| 13 | - | ||
| 14 | - | ||
| 15 | -@Configuration | ||
| 16 | -@EnableSwagger2 | ||
| 17 | -/** 是否打开swagger **/ | ||
| 18 | -@ConditionalOnExpression("'${swagger.enable}' == 'true'") | ||
| 19 | -public class SwaggerConfig { | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - @Bean | ||
| 23 | - public Docket createRestApi() { | ||
| 24 | - return new Docket(DocumentationType.SWAGGER_2) | ||
| 25 | - .apiInfo(apiInfo()) | ||
| 26 | - .select() | ||
| 27 | - .apis(RequestHandlerSelectors.basePackage("com.crossoverjie.netty.action.zk.controller")) | ||
| 28 | - .paths(PathSelectors.any()) | ||
| 29 | - .build(); | ||
| 30 | - } | ||
| 31 | - | ||
| 32 | - private ApiInfo apiInfo() { | ||
| 33 | - return new ApiInfoBuilder() | ||
| 34 | - .title("netty-action-zk api") | ||
| 35 | - .description("netty-action-zk api") | ||
| 36 | - .termsOfServiceUrl("https://crossoverJie.top") | ||
| 37 | - .contact("crossoverJie") | ||
| 38 | - .version("1.0.0") | ||
| 39 | - .build(); | ||
| 40 | - } | ||
| 41 | - | ||
| 42 | -} |
| 1 | -package com.crossoverjie.cim.server.zk.controller; | ||
| 2 | - | ||
| 3 | -import com.crossoverjie.cim.common.enums.StatusEnum; | ||
| 4 | -import com.crossoverjie.cim.common.res.BaseResponse; | ||
| 5 | -import com.crossoverjie.cim.server.zk.cache.ServerCache; | ||
| 6 | -import io.swagger.annotations.ApiOperation; | ||
| 7 | -import org.springframework.beans.factory.annotation.Autowired; | ||
| 8 | -import org.springframework.stereotype.Controller; | ||
| 9 | -import org.springframework.web.bind.annotation.RequestMapping; | ||
| 10 | -import org.springframework.web.bind.annotation.RequestMethod; | ||
| 11 | -import org.springframework.web.bind.annotation.ResponseBody; | ||
| 12 | - | ||
| 13 | -import java.util.List; | ||
| 14 | - | ||
| 15 | -/** | ||
| 16 | - * Function: | ||
| 17 | - * | ||
| 18 | - * @author crossoverJie | ||
| 19 | - * Date: 22/05/2018 14:46 | ||
| 20 | - * @since JDK 1.8 | ||
| 21 | - */ | ||
| 22 | -@Controller | ||
| 23 | -@RequestMapping("/") | ||
| 24 | -public class IndexController { | ||
| 25 | - | ||
| 26 | - | ||
| 27 | - @Autowired | ||
| 28 | - private ServerCache serverCache ; | ||
| 29 | - | ||
| 30 | - /** | ||
| 31 | - * 获取所有路由节点 | ||
| 32 | - * @return | ||
| 33 | - */ | ||
| 34 | - @ApiOperation("获取所有路由节点") | ||
| 35 | - @RequestMapping(value = "getAllRoute",method = RequestMethod.POST) | ||
| 36 | - @ResponseBody() | ||
| 37 | - public BaseResponse<List<String>> getAllRoute(){ | ||
| 38 | - BaseResponse<List<String>> res = new BaseResponse(); | ||
| 39 | - List<String> all = serverCache.getAll(); | ||
| 40 | - res.setDataBody(all); | ||
| 41 | - res.setCode(StatusEnum.SUCCESS.getCode()) ; | ||
| 42 | - res.setMessage(StatusEnum.SUCCESS.getMessage()) ; | ||
| 43 | - return res ; | ||
| 44 | - } | ||
| 45 | - | ||
| 46 | - /** | ||
| 47 | - * 获取所有路由节点 | ||
| 48 | - * @return | ||
| 49 | - */ | ||
| 50 | - @ApiOperation("获取所有路由节点") | ||
| 51 | - @RequestMapping(value = "getOneOfRoute",method = RequestMethod.POST) | ||
| 52 | - @ResponseBody() | ||
| 53 | - public BaseResponse<String> getOneOfRoute(){ | ||
| 54 | - BaseResponse<String> res = new BaseResponse(); | ||
| 55 | - String server = serverCache.selectServer(); | ||
| 56 | - res.setDataBody(server); | ||
| 57 | - res.setCode(StatusEnum.SUCCESS.getCode()) ; | ||
| 58 | - res.setMessage(StatusEnum.SUCCESS.getMessage()) ; | ||
| 59 | - return res ; | ||
| 60 | - } | ||
| 61 | - | ||
| 62 | - | ||
| 63 | - | ||
| 64 | - | ||
| 65 | -} |
| 1 | -package com.crossoverjie.cim.server.zk.thread; | ||
| 2 | - | ||
| 3 | -import com.crossoverjie.cim.server.zk.util.AppConfiguration; | ||
| 4 | -import com.crossoverjie.cim.server.zk.util.SpringBeanFactory; | ||
| 5 | -import com.crossoverjie.cim.server.zk.util.ZKit; | ||
| 6 | -import org.slf4j.Logger; | ||
| 7 | -import org.slf4j.LoggerFactory; | ||
| 8 | - | ||
| 9 | -/** | ||
| 10 | - * Function: | ||
| 11 | - * | ||
| 12 | - * @author crossoverJie | ||
| 13 | - * Date: 2018/8/24 01:37 | ||
| 14 | - * @since JDK 1.8 | ||
| 15 | - */ | ||
| 16 | -public class RegistryZK implements Runnable { | ||
| 17 | - | ||
| 18 | - private static Logger logger = LoggerFactory.getLogger(RegistryZK.class); | ||
| 19 | - | ||
| 20 | - private ZKit zkUtil; | ||
| 21 | - | ||
| 22 | - private AppConfiguration appConfiguration ; | ||
| 23 | - | ||
| 24 | - private String ip; | ||
| 25 | - private int port; | ||
| 26 | - | ||
| 27 | - public RegistryZK(String ip, int port) { | ||
| 28 | - this.ip = ip; | ||
| 29 | - this.port = port; | ||
| 30 | - zkUtil = SpringBeanFactory.getBean(ZKit.class) ; | ||
| 31 | - appConfiguration = SpringBeanFactory.getBean(AppConfiguration.class) ; | ||
| 32 | - } | ||
| 33 | - | ||
| 34 | - @Override | ||
| 35 | - public void run() { | ||
| 36 | - | ||
| 37 | - //创建父节点 | ||
| 38 | - zkUtil.createRootNode(); | ||
| 39 | - | ||
| 40 | - //是否要将自己注册到 ZK | ||
| 41 | - if (appConfiguration.isZkSwitch()){ | ||
| 42 | - String path = appConfiguration.getZkRoot() + "/ip-" + ip + ":" + port; | ||
| 43 | - zkUtil.createNode(path, path); | ||
| 44 | - logger.info("注册 zookeeper 成功,msg=[{}]", path); | ||
| 45 | - } | ||
| 46 | - | ||
| 47 | - //注册监听服务 | ||
| 48 | - zkUtil.subscribeEvent(appConfiguration.getZkRoot()); | ||
| 49 | - | ||
| 50 | - } | ||
| 51 | -} |
| 1 | -package com.crossoverjie.cim.server.zk.util; | ||
| 2 | - | ||
| 3 | -import org.springframework.beans.factory.annotation.Value; | ||
| 4 | -import org.springframework.stereotype.Component; | ||
| 5 | - | ||
| 6 | -/** | ||
| 7 | - * Function: | ||
| 8 | - * | ||
| 9 | - * @author crossoverJie | ||
| 10 | - * Date: 2018/8/24 01:43 | ||
| 11 | - * @since JDK 1.8 | ||
| 12 | - */ | ||
| 13 | -@Component | ||
| 14 | -public class AppConfiguration { | ||
| 15 | - | ||
| 16 | - @Value("${app.zk.root}") | ||
| 17 | - private String zkRoot; | ||
| 18 | - | ||
| 19 | - @Value("${app.zk.addr}") | ||
| 20 | - private String zkAddr; | ||
| 21 | - | ||
| 22 | - @Value("${app.zk.switch}") | ||
| 23 | - private boolean zkSwitch; | ||
| 24 | - | ||
| 25 | - @Value("${server.port}") | ||
| 26 | - private int port; | ||
| 27 | - | ||
| 28 | - public int getPort() { | ||
| 29 | - return port; | ||
| 30 | - } | ||
| 31 | - | ||
| 32 | - public void setPort(int port) { | ||
| 33 | - this.port = port; | ||
| 34 | - } | ||
| 35 | - | ||
| 36 | - public String getZkRoot() { | ||
| 37 | - return zkRoot; | ||
| 38 | - } | ||
| 39 | - | ||
| 40 | - public void setZkRoot(String zkRoot) { | ||
| 41 | - this.zkRoot = zkRoot; | ||
| 42 | - } | ||
| 43 | - | ||
| 44 | - public String getZkAddr() { | ||
| 45 | - return zkAddr; | ||
| 46 | - } | ||
| 47 | - | ||
| 48 | - public void setZkAddr(String zkAddr) { | ||
| 49 | - this.zkAddr = zkAddr; | ||
| 50 | - } | ||
| 51 | - | ||
| 52 | - public boolean isZkSwitch() { | ||
| 53 | - return zkSwitch; | ||
| 54 | - } | ||
| 55 | - | ||
| 56 | - public void setZkSwitch(boolean zkSwitch) { | ||
| 57 | - this.zkSwitch = zkSwitch; | ||
| 58 | - } | ||
| 59 | -} |
| 1 | -package com.crossoverjie.cim.server.zk.util; | ||
| 2 | - | ||
| 3 | -import org.springframework.beans.BeansException; | ||
| 4 | -import org.springframework.context.ApplicationContext; | ||
| 5 | -import org.springframework.context.ApplicationContextAware; | ||
| 6 | -import org.springframework.stereotype.Component; | ||
| 7 | - | ||
| 8 | -@Component | ||
| 9 | -public final class SpringBeanFactory implements ApplicationContextAware{ | ||
| 10 | - private static ApplicationContext context; | ||
| 11 | - | ||
| 12 | - public static <T> T getBean(Class<T> c){ | ||
| 13 | - return context.getBean(c); | ||
| 14 | - } | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - public static <T> T getBean(String name,Class<T> clazz){ | ||
| 18 | - return context.getBean(name,clazz); | ||
| 19 | - } | ||
| 20 | - | ||
| 21 | - @Override | ||
| 22 | - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | ||
| 23 | - context = applicationContext; | ||
| 24 | - } | ||
| 25 | - | ||
| 26 | - | ||
| 27 | -} |
| 1 | -package com.crossoverjie.cim.server.zk.util; | ||
| 2 | - | ||
| 3 | -import com.alibaba.fastjson.JSON; | ||
| 4 | -import com.crossoverjie.cim.server.zk.cache.ServerCache; | ||
| 5 | -import org.I0Itec.zkclient.IZkChildListener; | ||
| 6 | -import org.I0Itec.zkclient.ZkClient; | ||
| 7 | -import org.slf4j.Logger; | ||
| 8 | -import org.slf4j.LoggerFactory; | ||
| 9 | -import org.springframework.beans.factory.annotation.Autowired; | ||
| 10 | -import org.springframework.stereotype.Component; | ||
| 11 | - | ||
| 12 | -import java.util.List; | ||
| 13 | - | ||
| 14 | -/** | ||
| 15 | - * Function: Zookeeper 工具 | ||
| 16 | - * | ||
| 17 | - * @author crossoverJie | ||
| 18 | - * Date: 2018/8/19 00:33 | ||
| 19 | - * @since JDK 1.8 | ||
| 20 | - */ | ||
| 21 | -@Component | ||
| 22 | -public class ZKit { | ||
| 23 | - | ||
| 24 | - private static Logger logger = LoggerFactory.getLogger(ZKit.class); | ||
| 25 | - | ||
| 26 | - | ||
| 27 | - @Autowired | ||
| 28 | - private ZkClient zkClient; | ||
| 29 | - | ||
| 30 | - @Autowired | ||
| 31 | - private AppConfiguration appConfiguration ; | ||
| 32 | - | ||
| 33 | - @Autowired | ||
| 34 | - private ServerCache serverCache ; | ||
| 35 | - | ||
| 36 | - | ||
| 37 | - /** | ||
| 38 | - * 创建父级节点 | ||
| 39 | - */ | ||
| 40 | - public void createRootNode(){ | ||
| 41 | - boolean exists = zkClient.exists(appConfiguration.getZkRoot()); | ||
| 42 | - if (exists){ | ||
| 43 | - return; | ||
| 44 | - } | ||
| 45 | - | ||
| 46 | - //创建 root | ||
| 47 | - zkClient.createPersistent(appConfiguration.getZkRoot()) ; | ||
| 48 | - } | ||
| 49 | - | ||
| 50 | - /** | ||
| 51 | - * 写入指定节点 临时目录 | ||
| 52 | - * | ||
| 53 | - * @param path | ||
| 54 | - * @param value | ||
| 55 | - */ | ||
| 56 | - public void createNode(String path, String value) { | ||
| 57 | - zkClient.createEphemeral(path, value); | ||
| 58 | - } | ||
| 59 | - | ||
| 60 | - | ||
| 61 | - /** | ||
| 62 | - * 监听事件 | ||
| 63 | - * | ||
| 64 | - * @param path | ||
| 65 | - */ | ||
| 66 | - public void subscribeEvent(String path) { | ||
| 67 | - zkClient.subscribeChildChanges(path, new IZkChildListener() { | ||
| 68 | - @Override | ||
| 69 | - public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception { | ||
| 70 | - logger.info("清除/更新本地缓存 parentPath=【{}】,currentChilds=【{}】", parentPath,currentChilds.toString()); | ||
| 71 | - | ||
| 72 | - //更新所有缓存/先删除 再新增 | ||
| 73 | - serverCache.updateCache(currentChilds) ; | ||
| 74 | - } | ||
| 75 | - }); | ||
| 76 | - | ||
| 77 | - | ||
| 78 | - } | ||
| 79 | - | ||
| 80 | - | ||
| 81 | - /** | ||
| 82 | - * 获取所有注册节点 | ||
| 83 | - * @return | ||
| 84 | - */ | ||
| 85 | - public List<String> getAllNode(){ | ||
| 86 | - List<String> children = zkClient.getChildren("/route"); | ||
| 87 | - logger.info("查询所有节点成功=【{}】", JSON.toJSONString(children)); | ||
| 88 | - return children; | ||
| 89 | - } | ||
| 90 | - | ||
| 91 | - /** | ||
| 92 | - * 关闭 ZK | ||
| 93 | - */ | ||
| 94 | - public void closeZK() { | ||
| 95 | - logger.info("正在关闭 ZK"); | ||
| 96 | - zkClient.close(); | ||
| 97 | - logger.info("关闭 ZK 成功"); | ||
| 98 | - | ||
| 99 | - } | ||
| 100 | -} |
| @@ -30,7 +30,6 @@ | @@ -30,7 +30,6 @@ | ||
| 30 | <module>cim-server</module> | 30 | <module>cim-server</module> |
| 31 | <module>cim-client</module> | 31 | <module>cim-client</module> |
| 32 | <module>cim-common</module> | 32 | <module>cim-common</module> |
| 33 | - <module>cim-zk</module> | ||
| 34 | <module>cim-forward-route</module> | 33 | <module>cim-forward-route</module> |
| 35 | </modules> | 34 | </modules> |
| 36 | 35 |
-
请 注册 或 登录 后发表评论