├── .gitignore ├── ISSUE_TEMPLATE └── BUG_TEMPLATE.md ├── README.md ├── pom.xml ├── zns-api ├── pom.xml ├── src │ └── main │ │ └── java │ │ └── com │ │ └── buildupchao │ │ └── zns │ │ └── api │ │ └── annotation │ │ ├── ZnsClient.java │ │ └── ZnsService.java └── target │ ├── maven-archiver │ └── pom.properties │ └── maven-status │ └── maven-compiler-plugin │ ├── compile │ └── default-compile │ │ ├── createdFiles.lst │ │ └── inputFiles.lst │ └── testCompile │ └── default-testCompile │ └── inputFiles.lst ├── zns-client ├── pom.xml ├── src │ └── main │ │ └── java │ │ └── com │ │ └── buildupchao │ │ └── zns │ │ └── client │ │ ├── ZnsClientPackage.java │ │ ├── ZnsClientRunner.java │ │ ├── bean │ │ ├── ChannelHolder.java │ │ └── ProviderService.java │ │ ├── cache │ │ └── ServiceRouteCache.java │ │ ├── cluster │ │ ├── ClusterStrategy.java │ │ ├── ClusterStrategyEnum.java │ │ ├── engine │ │ │ └── ClusterEngine.java │ │ └── impl │ │ │ ├── HashClusterStrategyImpl.java │ │ │ ├── PollingClusterStrategyImpl.java │ │ │ ├── RandomClusterStrategyImpl.java │ │ │ ├── WeightPollingClusterStrategyImpl.java │ │ │ └── WeightRandomClusterStrategyImpl.java │ │ ├── config │ │ ├── BeanConfig.java │ │ └── ZnsClientConfiguration.java │ │ ├── connector │ │ ├── ZnsClientConnector.java │ │ ├── handler │ │ │ ├── ZnsClientDecodeHandler.java │ │ │ ├── ZnsClientEncodeHandler.java │ │ │ └── ZnsResponseHandler.java │ │ └── init │ │ │ └── ZnsClientInitializer.java │ │ ├── proxy │ │ ├── ProxyHelper.java │ │ └── ServiceProxyManager.java │ │ ├── pull │ │ └── ServicePullManager.java │ │ ├── runner │ │ ├── ZnsRequestManager.java │ │ └── ZnsRequestPool.java │ │ └── util │ │ ├── RequestIdUtil.java │ │ ├── SpringBeanFactory.java │ │ └── ZKit.java └── target │ ├── maven-archiver │ └── pom.properties │ └── maven-status │ └── maven-compiler-plugin │ ├── compile │ └── default-compile │ │ ├── createdFiles.lst │ │ └── inputFiles.lst │ └── testCompile │ └── default-testCompile │ └── inputFiles.lst ├── zns-common ├── pom.xml ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── buildupchao │ │ │ │ └── zns │ │ │ │ └── common │ │ │ │ ├── bean │ │ │ │ ├── ZnsRequest.java │ │ │ │ └── ZnsResponse.java │ │ │ │ ├── exception │ │ │ │ ├── StatusCode.java │ │ │ │ └── ZnsException.java │ │ │ │ ├── serialize │ │ │ │ ├── ISerializer.java │ │ │ │ └── impl │ │ │ │ │ └── JsonSerializer.java │ │ │ │ └── util │ │ │ │ ├── IpUtil.java │ │ │ │ └── SerializationUtil.java │ │ └── resources │ │ │ └── log4j.properties │ └── test │ │ └── java │ │ └── com │ │ └── buildupchao │ │ └── zns │ │ └── common │ │ └── util │ │ └── IpUtilTests.java └── target │ ├── classes │ └── log4j.properties │ ├── maven-archiver │ └── pom.properties │ └── maven-status │ └── maven-compiler-plugin │ ├── compile │ └── default-compile │ │ ├── createdFiles.lst │ │ └── inputFiles.lst │ └── testCompile │ └── default-testCompile │ ├── createdFiles.lst │ └── inputFiles.lst ├── zns-monitor ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── buildupchao │ │ └── zns │ │ └── monitor │ │ ├── ZnsMonitorApplication.java │ │ ├── config │ │ ├── SwaggerConfig.java │ │ ├── ZnsMonitorConfig.java │ │ └── ZnsMonitorConfigProperties.java │ │ └── controller │ │ └── MonitorController.java │ └── resources │ └── application.yml ├── zns-server ├── pom.xml ├── src │ └── main │ │ └── java │ │ └── com │ │ └── buildupchao │ │ └── zns │ │ └── server │ │ ├── ZnsServerPackage.java │ │ ├── ZnsServerRunner.java │ │ ├── acceptor │ │ ├── ZnsServerAcceptor.java │ │ ├── handler │ │ │ ├── ZnsRequestHandler.java │ │ │ ├── ZnsServerDecodeHandler.java │ │ │ └── ZnsServerEncodeHandler.java │ │ └── init │ │ │ └── ZnsServerInitializer.java │ │ ├── config │ │ ├── BeanConfig.java │ │ └── ZnsServerConfiguration.java │ │ ├── push │ │ └── ServicePushManager.java │ │ ├── runner │ │ └── HeartBeatChecker.java │ │ └── util │ │ ├── ClassUtils.java │ │ ├── SpringBeanFactory.java │ │ └── ZKit.java └── target │ ├── maven-archiver │ └── pom.properties │ └── maven-status │ └── maven-compiler-plugin │ ├── compile │ └── default-compile │ │ ├── createdFiles.lst │ │ └── inputFiles.lst │ └── testCompile │ └── default-testCompile │ └── inputFiles.lst ├── zns-service-api ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── buildupchao │ │ └── zns │ │ └── service │ │ └── api │ │ └── ChatService.java │ └── resources │ └── application.properties ├── zns-service-consumer ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── buildupchao │ │ │ └── zns │ │ │ └── service │ │ │ └── consumer │ │ │ ├── ZnsServiceConsumerApplication.java │ │ │ └── controller │ │ │ └── TestController.java │ └── resources │ │ ├── application.yml │ │ └── logback-spring.xml │ └── test │ └── java │ └── com │ └── buildupchao │ └── zns │ └── service │ └── consumer │ └── ZnsServiceConsumerApplicationTests.java ├── zns-service-provider ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── buildupchao │ │ │ └── zns │ │ │ └── service │ │ │ └── provider │ │ │ ├── ZnsServiceProviderApplication.java │ │ │ └── service │ │ │ └── ChatServiceImpl.java │ └── resources │ │ ├── application.yml │ │ ├── log4j.properties │ │ └── logback-spring.xml │ └── test │ └── java │ └── com │ └── buildupchao │ └── zns │ └── service │ └── provider │ └── ZnsServiceProviderApplicationTests.java └── zns-zk ├── pom.xml └── src └── main └── java └── com └── buildupchao └── zns └── zk ├── ZnsZKApplication.java ├── bean ├── ConsumerService.java └── ProviderService.java ├── config ├── BeanConfig.java └── ZnsZkConfiguration.java ├── registry ├── IRegisterCenter2Provider.java └── RegisterCenter.java └── util └── SpringBeanFactory.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | .idea/ 25 | *.iml 26 | ### macOS template 27 | # General 28 | .DS_Store 29 | .AppleDouble 30 | .LSOverride 31 | 32 | # Icon must end with two \r 33 | Icon 34 | 35 | # Thumbnails 36 | ._* 37 | 38 | # Files that might appear in the root of a volume 39 | .DocumentRevisions-V100 40 | .fseventsd 41 | .Spotlight-V100 42 | .TemporaryItems 43 | .Trashes 44 | .VolumeIcon.icns 45 | .com.apple.timemachine.donotpresent 46 | 47 | # Directories potentially created on remote AFP share 48 | .AppleDB 49 | .AppleDesktop 50 | Network Trash Folder 51 | Temporary Items 52 | .apdisk 53 | 54 | -------------------------------------------------------------------------------- /ISSUE_TEMPLATE/BUG_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | --- 2 | Bug Name: Pls name for bug that you find. 3 | Reporter: Your name. U can provide email so that I notify U about the process of bug resolution. 4 | Description: What's the bug? How to reproduce it? Please provide as concise and clear a description as possible. 5 | Expected Results: The result you expect. 6 | Others: 7 | maybe screenshot, etc. 8 | --- -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ZNS 2 | What the ZNS is an implementation of distribution RPC. 3 | 4 | ## 1 Introduction 5 | 6 | ZNS是一个分布式RPC框架。 7 | 8 | - 通过Zookeeper实现服务的注册与发现 9 | 10 | - 基于Netty实现底层网络通信 11 | 12 | - 采用Spring实现最小依赖注入 13 | 14 | ## 2 Feature LIST 15 | 16 | * [x] 服务注册 17 | * [x] 服务发现 18 | * [x] 网络通信层 19 | * [x] 服务异步调用 20 | * [x] 灵活的水平扩容、缩容 21 | * [x] 更多的集群服务路由策略 22 | * [ ] 更可靠的服务调用 23 | * [ ] 服务生产者与Zookeeper心跳机制 24 | * [ ] 服务监控 25 | * [ ] 服务降级 26 | * [ ] 服务版本控制 27 | 28 | 29 | ## 3 Architecture 30 | 31 | ![](https://github.com/buildupchao/ImgStore/blob/master/blog/RPC%E5%8E%9F%E7%90%86.png?raw=true) 32 | 33 | ![](https://github.com/buildupchao/ImgStore/blob/master/blog/zns.png?raw=true) 34 | 35 | ### 3.1 zns-server 36 | 37 | `ZNS` 服务端:提供了服务注册,接受`client`端Connector连接,反射调用服务等 38 | 39 | ### 3.2 zns-client 40 | 41 | `ZNS` 客户端:提供了服务发现,请求连接`server`端Acceptor,服务代理等 42 | 43 | ## 4 How to use 44 | 45 | ### 4.1 服务提供端 46 | 47 | - 添加 `zns-server` 依赖包 48 | 49 | ``` 50 | 51 | com.buildupchao 52 | zns-service-api 53 | 0.0.1-SNAPSHOT 54 | 55 | 56 | org.springframework 57 | spring-context 58 | 59 | 60 | 61 | ``` 62 | 63 | - 添加扫描包路径`ZnsServerPackage.class`以及启动调用方法`znsServerRunner.run()` 64 | 65 | ``` 66 | @ComponentScan( 67 | basePackages = "com.buildupchao.zns.service.provider", 68 | basePackageClasses = ZnsServerPackage.class 69 | ) 70 | @SpringBootApplication 71 | public class ZnsServiceProviderApplication implements ApplicationRunner { 72 | 73 | private static final Logger LOGGER = LoggerFactory.getLogger(ZnsServiceProviderApplication.class); 74 | 75 | @Autowired 76 | private ZnsServerRunner znsServerRunner; 77 | 78 | public static void main(String[] args) { 79 | SpringApplication.run(ZnsServiceProviderApplication.class, args); 80 | 81 | LOGGER.info("Zns service provider application startup successfully"); 82 | 83 | } 84 | 85 | @Override 86 | public void run(ApplicationArguments applicationArguments) throws Exception { 87 | znsServerRunner.run(); 88 | } 89 | } 90 | ``` 91 | 92 | - 在application.yml中配置属性 93 | 94 | ``` 95 | zns: 96 | server: 97 | zk: 98 | root: /zns 99 | addr: localhost:2181 100 | switch: true 101 | network: 102 | port: 8888 103 | ``` 104 | 105 | ### 4.2 服务消费端 106 | 107 | - 添加 `zns-client` 依赖包 108 | 109 | ``` 110 | 111 | com.buildupchao 112 | zns-client 113 | 1.0-SNAPSHOT 114 | 115 | 116 | org.springframework 117 | spring-context 118 | 119 | 120 | 121 | ``` 122 | 123 | - 添加扫描包路径`ZnsClientPackage.class`以及启动调用方法`znsClientRunner.run()` 124 | 125 | ``` 126 | @ComponentScan( 127 | basePackages = "com.buildupchao.zns.service.consumer", 128 | basePackageClasses = ZnsClientPackage.class 129 | ) 130 | @SpringBootApplication 131 | public class ZnsServiceConsumerApplication implements ApplicationRunner { 132 | 133 | @Autowired 134 | private ZnsClientRunner znsClientRunner; 135 | 136 | public static void main(String[] args) { 137 | SpringApplication.run(ZnsServiceConsumerApplication.class, args); 138 | } 139 | 140 | @Override 141 | public void run(ApplicationArguments applicationArguments) throws Exception { 142 | znsClientRunner.run(); 143 | } 144 | } 145 | ``` 146 | 147 | - 在application.yml中配置属性 148 | 149 | ``` 150 | zns: 151 | client: 152 | zk: 153 | root: /zns 154 | addr: localhost:2181 155 | switch: true 156 | api: 157 | package: com.buildupchao.zns.service.api 158 | ``` 159 | 160 | ### 4.3 公共API`zns-service-api`需要引入`zns-api`包依赖 161 | 162 | ``` 163 | 164 | com.buildupchao 165 | zns-api 166 | 1.0-SNAPSHOT 167 | 168 | ``` 169 | 170 | ## Contact Me 171 | 172 | - [buildupchao@gmail.com](mailto:buildupchao@gmail.com) 173 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.buildupchao 8 | zns 9 | 1.0-SNAPSHOT 10 | 11 | zns-common 12 | zns-server 13 | zns-client 14 | zns-api 15 | zns-monitor 16 | zns-zk 17 | 18 | pom 19 | 20 | 21 | UTF-8 22 | UTF-8 23 | 1.8 24 | 25 | 26 | 27 | 28 | 29 | com.github.sgroschupf 30 | zkclient 31 | 0.1 32 | 33 | 34 | io.netty 35 | netty-all 36 | 4.1.42.Final 37 | 38 | 39 | org.objenesis 40 | objenesis 41 | 2.6 42 | 43 | 44 | 45 | com.dyuproject.protostuff 46 | protostuff-core 47 | 1.0.8 48 | 49 | 50 | com.dyuproject.protostuff 51 | protostuff-runtime 52 | 1.0.8 53 | 54 | 55 | 56 | org.springframework 57 | spring-context 58 | 5.0.6.RELEASE 59 | 60 | 61 | 62 | org.projectlombok 63 | lombok 64 | 1.16.22 65 | 66 | 67 | org.slf4j 68 | slf4j-log4j12 69 | 1.7.25 70 | 71 | 72 | com.google.guava 73 | guava 74 | 19.0 75 | 76 | 77 | commons-collections 78 | commons-collections 79 | 3.2.2 80 | 81 | 82 | org.apache.commons 83 | commons-lang3 84 | 3.6 85 | 86 | 87 | commons-beanutils 88 | commons-beanutils 89 | 1.9.3 90 | 91 | 92 | cglib 93 | cglib 94 | 3.1 95 | 96 | 97 | org.reflections 98 | reflections 99 | 0.9.10 100 | 101 | 102 | 103 | junit 104 | junit 105 | 4.13.1 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | org.apache.maven.plugins 114 | maven-compiler-plugin 115 | 3.3 116 | 117 | ${java.version} 118 | ${java.version} 119 | UTF-8 120 | 121 | 122 | 123 | 124 | 125 | 126 | org.apache.maven.plugins 127 | maven-compiler-plugin 128 | 3.3 129 | 130 | ${java.version} 131 | ${java.version} 132 | UTF-8 133 | 134 | 135 | 136 | 137 | -------------------------------------------------------------------------------- /zns-api/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | zns 7 | com.buildupchao 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | com.buildupchao 13 | zns-api 14 | 1.0-SNAPSHOT 15 | 16 | 17 | 18 | org.springframework 19 | spring-context 20 | 21 | 22 | 23 | 24 | 25 | 26 | org.apache.maven.plugins 27 | maven-compiler-plugin 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /zns-api/src/main/java/com/buildupchao/zns/api/annotation/ZnsClient.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.api.annotation; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import java.lang.annotation.ElementType; 6 | import java.lang.annotation.Retention; 7 | import java.lang.annotation.RetentionPolicy; 8 | import java.lang.annotation.Target; 9 | 10 | /** 11 | * Interface to flag a class as being handled through the zns rpc. 12 | * 13 | * @author buildupchao 14 | * @date 2019/2/1 01:01 15 | * @since JDK 1.8 16 | */ 17 | @Component 18 | @Target(ElementType.TYPE) 19 | @Retention(RetentionPolicy.RUNTIME) 20 | public @interface ZnsClient { 21 | 22 | String version() default ""; 23 | } 24 | -------------------------------------------------------------------------------- /zns-api/src/main/java/com/buildupchao/zns/api/annotation/ZnsService.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.api.annotation; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import java.lang.annotation.ElementType; 6 | import java.lang.annotation.Retention; 7 | import java.lang.annotation.RetentionPolicy; 8 | import java.lang.annotation.Target; 9 | 10 | /** 11 | * Interface to flag a class as being handled through the zns rpc. 12 | * 13 | * @author buildupchao 14 | * @date 2019/1/31 23:39 15 | * @since JDK 1.8 16 | */ 17 | @Component 18 | @Target(ElementType.TYPE) 19 | @Retention(RetentionPolicy.RUNTIME) 20 | public @interface ZnsService { 21 | Class cls(); 22 | 23 | String version() default ""; 24 | } 25 | -------------------------------------------------------------------------------- /zns-api/target/maven-archiver/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven 2 | #Mon Feb 04 07:05:18 CST 2019 3 | version=1.0-SNAPSHOT 4 | groupId=com.buildupchao 5 | artifactId=zns-api 6 | -------------------------------------------------------------------------------- /zns-api/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/buildupchao/zns/3ea7b6ae0c90c7975d024d425e9cf6775e8c4e13/zns-api/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst -------------------------------------------------------------------------------- /zns-api/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | /Users/yachao/work/learn/git/zns/zns-api/src/main/java/com/buildupchao/zns/api/annotation/ZnsClient.java 2 | /Users/yachao/work/learn/git/zns/zns-api/src/main/java/com/buildupchao/zns/api/annotation/ZnsService.java 3 | -------------------------------------------------------------------------------- /zns-api/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/buildupchao/zns/3ea7b6ae0c90c7975d024d425e9cf6775e8c4e13/zns-api/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst -------------------------------------------------------------------------------- /zns-client/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | zns 7 | com.buildupchao 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 1.0-SNAPSHOT 12 | zns-client 13 | 14 | 15 | 16 | com.buildupchao 17 | zns-api 18 | 1.0-SNAPSHOT 19 | 20 | 21 | com.buildupchao 22 | zns-common 23 | 1.0-SNAPSHOT 24 | 25 | 26 | io.netty 27 | netty-all 28 | 29 | 30 | org.springframework 31 | spring-context 32 | 33 | 34 | com.github.sgroschupf 35 | zkclient 36 | 37 | 38 | cglib 39 | cglib 40 | 41 | 42 | org.reflections 43 | reflections 44 | 45 | 46 | 47 | 48 | 49 | org.apache.maven.plugins 50 | maven-compiler-plugin 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/ZnsClientPackage.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client; 2 | 3 | /** 4 | * @author buildupchao 5 | * @date 2019/2/1 14:01 6 | * @since JDK 1.8 7 | */ 8 | public class ZnsClientPackage { 9 | } 10 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/ZnsClientRunner.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client; 2 | 3 | import com.buildupchao.zns.client.cache.ServiceRouteCache; 4 | import com.buildupchao.zns.client.proxy.ServiceProxyManager; 5 | import com.buildupchao.zns.client.pull.ServicePullManager; 6 | import com.buildupchao.zns.client.runner.ZnsRequestManager; 7 | import com.buildupchao.zns.client.runner.ZnsRequestPool; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Component; 10 | 11 | /** 12 | * @author buildupchao 13 | * @date 2019/2/1 00:56 14 | * @since JDK 1.8 15 | */ 16 | @Component 17 | public class ZnsClientRunner { 18 | 19 | @Autowired 20 | private ServicePullManager servicePullManager; 21 | 22 | @Autowired 23 | private ServiceProxyManager serviceProxyManager; 24 | 25 | @Autowired 26 | private ZnsRequestPool znsRequestPool; 27 | 28 | @Autowired 29 | private ServiceRouteCache serviceRouteCache; 30 | 31 | public void run() { 32 | // Start request manager 33 | ZnsRequestManager.startZnsRequestManager(znsRequestPool, serviceRouteCache); 34 | 35 | // Pull service provider info from zookeeper 36 | servicePullManager.pullServiceFromZK(); 37 | 38 | // Create proxy for service which owns @ZnsClient annotation 39 | serviceProxyManager.initServiceProxyInstance(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/bean/ChannelHolder.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.bean; 2 | 3 | import io.netty.channel.Channel; 4 | import io.netty.channel.EventLoopGroup; 5 | import lombok.AllArgsConstructor; 6 | import lombok.Builder; 7 | import lombok.Data; 8 | import lombok.NoArgsConstructor; 9 | 10 | /** 11 | * @author buildupchao 12 | * @date 2019/2/1 13:13 13 | * @since JDK 1.8 14 | */ 15 | @Data 16 | @Builder 17 | @NoArgsConstructor 18 | @AllArgsConstructor 19 | public class ChannelHolder { 20 | private Channel channel; 21 | private EventLoopGroup eventLoopGroup; 22 | } 23 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/bean/ProviderService.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.bean; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Builder; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | import java.io.Serializable; 9 | 10 | /** 11 | * @author buildupchao 12 | * @date 2019/2/1 02:52 13 | * @since JDK 1.8 14 | */ 15 | @Data 16 | @Builder 17 | @NoArgsConstructor 18 | @AllArgsConstructor 19 | public class ProviderService implements Serializable { 20 | private String serverIp; 21 | private int serverPort; 22 | private int networkPort; 23 | 24 | private long timeout; 25 | // the weight of service provider 26 | private int weight; 27 | } 28 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/cache/ServiceRouteCache.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.cache; 2 | 3 | import com.buildupchao.zns.api.annotation.ZnsClient; 4 | import com.buildupchao.zns.client.bean.ProviderService; 5 | import com.buildupchao.zns.client.util.SpringBeanFactory; 6 | import com.buildupchao.zns.client.util.ZKit; 7 | import com.buildupchao.zns.common.exception.ZnsException; 8 | import com.google.common.cache.LoadingCache; 9 | import org.apache.commons.collections.MapUtils; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.stereotype.Component; 12 | 13 | import java.util.List; 14 | import java.util.Map; 15 | import java.util.concurrent.ExecutionException; 16 | 17 | /** 18 | * @author buildupchao 19 | * @date 2019/2/1 01:47 20 | * @since JDK 1.8 21 | */ 22 | @Component 23 | public class ServiceRouteCache { 24 | 25 | @Autowired 26 | private LoadingCache> cache; 27 | 28 | @Autowired 29 | private ZKit zKit; 30 | 31 | 32 | public void addCache(String serviceName, List serviceRoutes) { 33 | cache.put(serviceName, serviceRoutes); 34 | } 35 | 36 | public void updateCache(String serviceName, List serviceRoutes) { 37 | cache.put(serviceName, serviceRoutes); 38 | } 39 | 40 | public void updateCache(Map> newServiceRoutesMap) { 41 | cache.invalidateAll(); 42 | for (Map.Entry> entry : newServiceRoutesMap.entrySet()) { 43 | cache.put(entry.getKey(), entry.getValue()); 44 | } 45 | } 46 | 47 | public List getServiceRoutes(String serviceName) { 48 | if (cache.size() == 0) { 49 | reloadCache(); 50 | 51 | if (cache.size() == 0) { 52 | throw new ZnsException("Not any service which is available."); 53 | } 54 | } 55 | try { 56 | return cache.get(serviceName); 57 | } catch (ExecutionException e) { 58 | throw new ZnsException(e); 59 | } 60 | } 61 | 62 | private void reloadCache() { 63 | Map beans = SpringBeanFactory.getBeanListByAnnotationClass(ZnsClient.class); 64 | if (MapUtils.isEmpty(beans)) { 65 | return; 66 | } 67 | for (Object bean : beans.values()) { 68 | String serviceName = bean.getClass().getName(); 69 | List serviceRoutes = zKit.getServiceInfos(serviceName); 70 | addCache(serviceName, serviceRoutes); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/cluster/ClusterStrategy.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.cluster; 2 | 3 | import com.buildupchao.zns.client.bean.ProviderService; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author buildupchao 9 | * @date 2019/2/1 02:44 10 | * @since JDK 1.8 11 | */ 12 | public interface ClusterStrategy { 13 | 14 | ProviderService select(List serviceRoutes); 15 | } 16 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/cluster/ClusterStrategyEnum.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.cluster; 2 | 3 | import org.apache.commons.lang3.StringUtils; 4 | 5 | /** 6 | * @author buildupchao 7 | * @date 2019/2/1 02:46 8 | * @since JDK 1.8 9 | */ 10 | public enum ClusterStrategyEnum { 11 | RANDOM("Random"), 12 | WEIGHT_RANDOM("WeightRandom"), 13 | POLLING("Polling"), 14 | WEIGHT_POLLING("WeightPolling"), 15 | HASH("Hash"); 16 | 17 | private final String code; 18 | 19 | ClusterStrategyEnum(String code) { 20 | this.code = code; 21 | } 22 | 23 | public static ClusterStrategyEnum queryByCode(String code) { 24 | if (StringUtils.isBlank(code)) { 25 | return null; 26 | } 27 | 28 | ClusterStrategyEnum strategy = null; 29 | for (ClusterStrategyEnum strategyEnum : values()) { 30 | if (StringUtils.equals(code, strategyEnum.getCode())) { 31 | strategy = strategyEnum; 32 | break; 33 | } 34 | } 35 | return strategy; 36 | } 37 | 38 | public String getCode() { 39 | return code; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/cluster/engine/ClusterEngine.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.cluster.engine; 2 | 3 | import com.buildupchao.zns.client.cluster.ClusterStrategy; 4 | import com.buildupchao.zns.client.cluster.ClusterStrategyEnum; 5 | import com.buildupchao.zns.client.cluster.impl.*; 6 | import com.google.common.collect.Maps; 7 | 8 | import java.util.Map; 9 | 10 | /** 11 | * @author buildupchao 12 | * @date 2019/2/1 02:45 13 | * @since JDK 1.8 14 | */ 15 | public class ClusterEngine { 16 | 17 | private static final Map clusterStrategyMap = Maps.newConcurrentMap(); 18 | 19 | static { 20 | clusterStrategyMap.put(ClusterStrategyEnum.RANDOM, new RandomClusterStrategyImpl()); 21 | clusterStrategyMap.put(ClusterStrategyEnum.WEIGHT_RANDOM, new WeightRandomClusterStrategyImpl()); 22 | clusterStrategyMap.put(ClusterStrategyEnum.POLLING, new PollingClusterStrategyImpl()); 23 | clusterStrategyMap.put(ClusterStrategyEnum.WEIGHT_POLLING, new WeightPollingClusterStrategyImpl()); 24 | clusterStrategyMap.put(ClusterStrategyEnum.HASH, new HashClusterStrategyImpl()); 25 | } 26 | 27 | public static ClusterStrategy queryClusterStrategy(String clusterStrategy) { 28 | ClusterStrategyEnum clusterStrategyEnum = ClusterStrategyEnum.queryByCode(clusterStrategy); 29 | if (clusterStrategyEnum == null) { 30 | return new RandomClusterStrategyImpl(); 31 | } 32 | return clusterStrategyMap.get(clusterStrategyEnum); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/cluster/impl/HashClusterStrategyImpl.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.cluster.impl; 2 | 3 | import com.buildupchao.zns.client.bean.ProviderService; 4 | import com.buildupchao.zns.client.cluster.ClusterStrategy; 5 | import com.buildupchao.zns.common.util.IpUtil; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * @author buildupchao 11 | * @date 2019/2/4 22:45 12 | * @since JDK 1.8 13 | */ 14 | public class HashClusterStrategyImpl implements ClusterStrategy { 15 | 16 | @Override 17 | public ProviderService select(List serviceRoutes) { 18 | String realIp = IpUtil.getRealIp(); 19 | int hashCode = realIp.hashCode(); 20 | 21 | int size = serviceRoutes.size(); 22 | return serviceRoutes.get(hashCode % size); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/cluster/impl/PollingClusterStrategyImpl.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.cluster.impl; 2 | 3 | import com.buildupchao.zns.client.bean.ProviderService; 4 | import com.buildupchao.zns.client.cluster.ClusterStrategy; 5 | 6 | import java.util.List; 7 | import java.util.concurrent.TimeUnit; 8 | import java.util.concurrent.atomic.AtomicLong; 9 | import java.util.concurrent.locks.Lock; 10 | import java.util.concurrent.locks.ReentrantLock; 11 | 12 | /** 13 | * @author buildupchao 14 | * @date 2019/2/4 07:24 15 | * @since JDK 1.8 16 | */ 17 | public class PollingClusterStrategyImpl implements ClusterStrategy { 18 | 19 | private int counter = 0; 20 | private Lock lock = new ReentrantLock(); 21 | 22 | @Override 23 | public ProviderService select(List serviceRoutes) { 24 | ProviderService providerService = null; 25 | try { 26 | lock.tryLock(10, TimeUnit.SECONDS); 27 | int size = serviceRoutes.size(); 28 | if (counter >= size) { 29 | counter = 0; 30 | } 31 | 32 | providerService = serviceRoutes.get(counter); 33 | counter++; 34 | } catch (InterruptedException ex) { 35 | ex.printStackTrace(); 36 | } finally { 37 | lock.unlock(); 38 | } 39 | 40 | if (providerService == null) { 41 | providerService = serviceRoutes.get(0); 42 | } 43 | return providerService; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/cluster/impl/RandomClusterStrategyImpl.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.cluster.impl; 2 | 3 | import com.buildupchao.zns.client.bean.ProviderService; 4 | import com.buildupchao.zns.client.cluster.ClusterStrategy; 5 | import org.apache.commons.lang3.RandomUtils; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * @author buildupchao 11 | * @date 2019/2/1 02:50 12 | * @since JDK 1.8 13 | */ 14 | public class RandomClusterStrategyImpl implements ClusterStrategy { 15 | 16 | @Override 17 | public ProviderService select(List serviceRoutes) { 18 | int MAX_LEN = serviceRoutes.size(); 19 | int index = RandomUtils.nextInt(0, MAX_LEN - 1); 20 | return serviceRoutes.get(index); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/cluster/impl/WeightPollingClusterStrategyImpl.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.cluster.impl; 2 | 3 | import com.buildupchao.zns.client.bean.ProviderService; 4 | import com.buildupchao.zns.client.cluster.ClusterStrategy; 5 | import com.google.common.collect.Lists; 6 | 7 | import java.util.List; 8 | import java.util.concurrent.TimeUnit; 9 | import java.util.concurrent.locks.Lock; 10 | import java.util.concurrent.locks.ReentrantLock; 11 | 12 | /** 13 | * @author buildupchao 14 | * @date 2019/2/4 22:39 15 | * @since JDK 1.8 16 | */ 17 | public class WeightPollingClusterStrategyImpl implements ClusterStrategy { 18 | 19 | private int counter = 0; 20 | private Lock lock = new ReentrantLock(); 21 | 22 | @Override 23 | public ProviderService select(List serviceRoutes) { 24 | ProviderService providerService = null; 25 | 26 | try { 27 | lock.tryLock(10, TimeUnit.SECONDS); 28 | List providerServices = Lists.newArrayList(); 29 | for (ProviderService serviceRoute : serviceRoutes) { 30 | int weight = serviceRoute.getWeight(); 31 | for (int i = 0; i < weight; i++) { 32 | providerServices.add(serviceRoute); 33 | } 34 | } 35 | 36 | if (counter >= providerServices.size()) { 37 | counter = 0; 38 | } 39 | providerService = providerServices.get(counter); 40 | counter++; 41 | } catch (InterruptedException ex) { 42 | ex.printStackTrace(); 43 | } finally { 44 | lock.unlock(); 45 | } 46 | 47 | if (providerService == null) { 48 | providerService = serviceRoutes.get(0); 49 | } 50 | return providerService; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/cluster/impl/WeightRandomClusterStrategyImpl.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.cluster.impl; 2 | 3 | import com.buildupchao.zns.client.bean.ProviderService; 4 | import com.buildupchao.zns.client.cluster.ClusterStrategy; 5 | import com.google.common.collect.Lists; 6 | import org.apache.commons.lang3.RandomUtils; 7 | 8 | import java.util.List; 9 | 10 | /** 11 | * @author buildupchao 12 | * @date 2019/2/4 22:37 13 | * @since JDK 1.8 14 | */ 15 | public class WeightRandomClusterStrategyImpl implements ClusterStrategy { 16 | 17 | @Override 18 | public ProviderService select(List serviceRoutes) { 19 | List providerServices = Lists.newArrayList(); 20 | for (ProviderService providerService : serviceRoutes) { 21 | int weight = providerService.getWeight(); 22 | for (int i = 0; i < weight; i++) { 23 | providerServices.add(providerService); 24 | } 25 | } 26 | 27 | int MAX_LEN = providerServices.size(); 28 | int index = RandomUtils.nextInt(0, MAX_LEN - 1); 29 | return providerServices.get(index); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/config/BeanConfig.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.config; 2 | 3 | import com.buildupchao.zns.client.bean.ProviderService; 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 | import java.util.List; 13 | import java.util.concurrent.TimeUnit; 14 | 15 | /** 16 | * @author buildupchao 17 | * @date 2019/2/1 01:25 18 | * @since JDK 1.8 19 | */ 20 | @Configuration 21 | public class BeanConfig { 22 | 23 | private static final int EXPIRE_SECONDS = 86400; 24 | 25 | @Autowired 26 | private ZnsClientConfiguration configuration; 27 | 28 | @Bean 29 | public ZkClient zkClient() { 30 | return new ZkClient(configuration.getZkAddr(), 5000); 31 | } 32 | 33 | @Bean 34 | public LoadingCache> buildCache() { 35 | return CacheBuilder.newBuilder() 36 | .build(new CacheLoader>() { 37 | @Override 38 | public List load(String key) throws Exception { 39 | return null; 40 | } 41 | }); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/config/ZnsClientConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.config; 2 | 3 | import lombok.Data; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.stereotype.Component; 6 | 7 | /** 8 | * @author buildupchao 9 | * @date 2019/2/1 01:17 10 | * @since JDK 1.8 11 | */ 12 | @Data 13 | @Component 14 | public class ZnsClientConfiguration { 15 | 16 | @Value("${zns.client.zk.root}") 17 | private String zkRoot; 18 | 19 | @Value("${zns.client.zk.addr}") 20 | private String zkAddr; 21 | 22 | @Value("${server.port}") 23 | private String znsClientPort; 24 | 25 | @Value("${zns.client.api.package}") 26 | private String znsClientApiPackage; 27 | 28 | @Value("${zns.cluster.strategy}") 29 | private String znsClientClusterStrategy; 30 | } 31 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/connector/ZnsClientConnector.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.connector; 2 | 3 | import com.buildupchao.zns.client.bean.ChannelHolder; 4 | import com.buildupchao.zns.client.bean.ProviderService; 5 | import com.buildupchao.zns.client.connector.init.ZnsClientInitializer; 6 | import com.buildupchao.zns.client.runner.ZnsRequestManager; 7 | import com.buildupchao.zns.client.util.SpringBeanFactory; 8 | import io.netty.bootstrap.Bootstrap; 9 | import io.netty.channel.ChannelFuture; 10 | import io.netty.channel.EventLoopGroup; 11 | import io.netty.channel.nio.NioEventLoopGroup; 12 | import io.netty.channel.socket.nio.NioSocketChannel; 13 | import org.slf4j.Logger; 14 | import org.slf4j.LoggerFactory; 15 | 16 | import java.util.concurrent.CountDownLatch; 17 | 18 | /** 19 | * @author buildupchao 20 | * @date 2019/2/1 03:04 21 | * @since JDK 1.8 22 | */ 23 | public class ZnsClientConnector implements Runnable { 24 | 25 | private static final Logger LOGGER = LoggerFactory.getLogger(ZnsClientConnector.class); 26 | 27 | private String requestId; 28 | private ProviderService providerService; 29 | private CountDownLatch latch; 30 | private ZnsClientInitializer znsClientInitializer; 31 | 32 | public ZnsClientConnector(String requestId, ProviderService providerService, CountDownLatch latch) { 33 | this.requestId = requestId; 34 | this.providerService = providerService; 35 | this.latch = latch; 36 | this.znsClientInitializer = SpringBeanFactory.getBean(ZnsClientInitializer.class); 37 | } 38 | 39 | @Override 40 | public void run() { 41 | EventLoopGroup worker = new NioEventLoopGroup(); 42 | Bootstrap bootstrap = new Bootstrap(); 43 | bootstrap.group(worker) 44 | .channel(NioSocketChannel.class) 45 | .remoteAddress(providerService.getServerIp(), providerService.getNetworkPort()) 46 | .handler(znsClientInitializer); 47 | 48 | try { 49 | ChannelFuture future = bootstrap.connect().sync(); 50 | if (future.isSuccess()) { 51 | ChannelHolder channelHolder = ChannelHolder.builder() 52 | .channel(future.channel()) 53 | .eventLoopGroup(worker) 54 | .build(); 55 | ZnsRequestManager.registerChannelHolder(requestId, channelHolder); 56 | LOGGER.info("Construct a connector with service provider[{}:{}] successfully", 57 | providerService.getServerIp(), 58 | providerService.getNetworkPort() 59 | ); 60 | 61 | latch.countDown(); 62 | } 63 | } catch (InterruptedException ex) { 64 | ex.printStackTrace(); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/connector/handler/ZnsClientDecodeHandler.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.connector.handler; 2 | 3 | import com.buildupchao.zns.common.bean.ZnsResponse; 4 | import com.buildupchao.zns.common.util.SerializationUtil; 5 | import io.netty.buffer.ByteBuf; 6 | import io.netty.channel.ChannelHandlerContext; 7 | import io.netty.handler.codec.ByteToMessageDecoder; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * @author buildupchao 13 | * @date 2019/2/1 03:07 14 | * @since JDK 1.8 15 | */ 16 | public class ZnsClientDecodeHandler extends ByteToMessageDecoder { 17 | 18 | @Override 19 | protected void decode(ChannelHandlerContext ctx, ByteBuf in, List list) throws Exception { 20 | if (in.readableBytes() <= 4) { 21 | return; 22 | } 23 | 24 | int length = in.readInt(); 25 | in.markReaderIndex(); 26 | if (in.readableBytes() < length) { 27 | in.resetReaderIndex(); 28 | } else { 29 | byte[] bytes = new byte[in.readableBytes()]; 30 | in.readBytes(bytes); 31 | ZnsResponse znsResponse = SerializationUtil.deserialize(bytes, ZnsResponse.class); 32 | list.add(znsResponse); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/connector/handler/ZnsClientEncodeHandler.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.connector.handler; 2 | 3 | import com.buildupchao.zns.common.bean.ZnsRequest; 4 | import com.buildupchao.zns.common.util.SerializationUtil; 5 | import io.netty.buffer.ByteBuf; 6 | import io.netty.channel.ChannelHandlerContext; 7 | import io.netty.handler.codec.MessageToByteEncoder; 8 | 9 | /** 10 | * @author buildupchao 11 | * @date 2019/2/1 03:07 12 | * @since JDK 1.8 13 | */ 14 | public class ZnsClientEncodeHandler extends MessageToByteEncoder { 15 | 16 | @Override 17 | protected void encode(ChannelHandlerContext ctx, ZnsRequest znsRequest, ByteBuf in) throws Exception { 18 | byte[] bytes = SerializationUtil.serialize(znsRequest); 19 | in.writeInt(bytes.length); 20 | in.writeBytes(bytes); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/connector/handler/ZnsResponseHandler.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.connector.handler; 2 | 3 | import com.buildupchao.zns.client.runner.ZnsRequestPool; 4 | import com.buildupchao.zns.common.bean.ZnsResponse; 5 | import io.netty.channel.ChannelHandler; 6 | import io.netty.channel.ChannelHandlerContext; 7 | import io.netty.channel.SimpleChannelInboundHandler; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Component; 10 | 11 | /** 12 | * @author buildupchao 13 | * @date 2019/2/1 03:06 14 | * @since JDK 1.8 15 | */ 16 | @Component 17 | @ChannelHandler.Sharable 18 | public class ZnsResponseHandler extends SimpleChannelInboundHandler { 19 | 20 | @Autowired 21 | private ZnsRequestPool requestPool; 22 | 23 | @Override 24 | protected void channelRead0(ChannelHandlerContext ctx, ZnsResponse znsResponse) throws Exception { 25 | requestPool.notifyRequest(znsResponse.getRequestId(), znsResponse); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/connector/init/ZnsClientInitializer.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.connector.init; 2 | 3 | import com.buildupchao.zns.client.connector.handler.ZnsClientDecodeHandler; 4 | import com.buildupchao.zns.client.connector.handler.ZnsClientEncodeHandler; 5 | import com.buildupchao.zns.client.connector.handler.ZnsResponseHandler; 6 | import io.netty.channel.Channel; 7 | import io.netty.channel.ChannelHandler; 8 | import io.netty.channel.ChannelInitializer; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.stereotype.Component; 11 | 12 | /** 13 | * @author buildupchao 14 | * @date 2019/2/1 03:25 15 | * @since JDK 1.8 16 | */ 17 | @Component 18 | @ChannelHandler.Sharable 19 | public class ZnsClientInitializer extends ChannelInitializer { 20 | 21 | @Autowired 22 | private ZnsResponseHandler znsResponseHandler; 23 | 24 | @Override 25 | protected void initChannel(Channel channel) throws Exception { 26 | channel.pipeline() 27 | .addLast(new ZnsClientEncodeHandler()) 28 | .addLast(new ZnsClientDecodeHandler()) 29 | .addLast(znsResponseHandler); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/proxy/ProxyHelper.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.proxy; 2 | 3 | import com.buildupchao.zns.client.runner.ZnsRequestManager; 4 | import com.buildupchao.zns.client.runner.ZnsRequestPool; 5 | import com.buildupchao.zns.client.util.RequestIdUtil; 6 | import com.buildupchao.zns.common.bean.ZnsRequest; 7 | import com.buildupchao.zns.common.bean.ZnsResponse; 8 | import net.sf.cglib.proxy.Enhancer; 9 | import net.sf.cglib.proxy.MethodInterceptor; 10 | import net.sf.cglib.proxy.MethodProxy; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.stereotype.Component; 13 | 14 | import java.lang.reflect.Method; 15 | 16 | /** 17 | * @author buildupchao 18 | * @date 2019/2/1 12:44 19 | * @since JDK 1.8 20 | */ 21 | @Component 22 | public class ProxyHelper { 23 | 24 | @Autowired 25 | private ZnsRequestPool znsRequestPool; 26 | 27 | public T newProxyInstance(Class cls) { 28 | Enhancer enhancer = new Enhancer(); 29 | enhancer.setSuperclass(cls); 30 | enhancer.setCallback(new ProxyCallBackHandler()); 31 | return (T) enhancer.create(); 32 | } 33 | 34 | class ProxyCallBackHandler implements MethodInterceptor { 35 | 36 | @Override 37 | public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { 38 | return doIntercept(method, args); 39 | } 40 | 41 | private Object doIntercept(Method method, Object[] parameters) throws Throwable { 42 | String requestId = RequestIdUtil.requestId(); 43 | String className = method.getDeclaringClass().getName(); 44 | String methodName = method.getName(); 45 | Class[] parameterTypes = method.getParameterTypes(); 46 | 47 | ZnsRequest znsRequest = ZnsRequest.builder() 48 | .requestId(requestId) 49 | .className(className) 50 | .methodName(methodName) 51 | .parameterTypes(parameterTypes) 52 | .parameters(parameters) 53 | .build(); 54 | 55 | ZnsRequestManager.sendRequest(znsRequest); 56 | ZnsResponse znsResponse = znsRequestPool.fetchResponse(requestId); 57 | if (znsResponse == null) { 58 | return null; 59 | } 60 | 61 | if (znsResponse.isError()) { 62 | throw znsResponse.getCause(); 63 | } 64 | return znsResponse.getResult(); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/proxy/ServiceProxyManager.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.proxy; 2 | 3 | import com.buildupchao.zns.api.annotation.ZnsClient; 4 | import com.buildupchao.zns.client.config.ZnsClientConfiguration; 5 | import com.buildupchao.zns.client.util.SpringBeanFactory; 6 | import org.apache.commons.collections.CollectionUtils; 7 | import org.reflections.Reflections; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.beans.factory.support.DefaultListableBeanFactory; 12 | import org.springframework.stereotype.Component; 13 | 14 | import java.util.Set; 15 | 16 | /** 17 | * @author buildupchao 18 | * @date 2019/2/1 14:14 19 | * @since JDK 1.8 20 | */ 21 | @Component 22 | public class ServiceProxyManager { 23 | 24 | private static final Logger LOGGER = LoggerFactory.getLogger(ServiceProxyManager.class); 25 | 26 | @Autowired 27 | private ZnsClientConfiguration configuration; 28 | 29 | @Autowired 30 | private ProxyHelper proxyHelper; 31 | 32 | public void initServiceProxyInstance() { 33 | Reflections reflections = new Reflections(configuration.getZnsClientApiPackage()); 34 | Set> typesAnnotatedWith = reflections.getTypesAnnotatedWith(ZnsClient.class); 35 | if (CollectionUtils.isEmpty(typesAnnotatedWith)) { 36 | return; 37 | } 38 | 39 | DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) SpringBeanFactory.context() 40 | .getAutowireCapableBeanFactory(); 41 | for (Class cls : typesAnnotatedWith) { 42 | ZnsClient znsClient = cls.getAnnotation(ZnsClient.class); 43 | String serviceName = cls.getName(); 44 | beanFactory.registerSingleton(serviceName, proxyHelper.newProxyInstance(cls)); 45 | } 46 | 47 | LOGGER.info("Initialize proxy for service successfully"); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/pull/ServicePullManager.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.pull; 2 | 3 | import com.buildupchao.zns.api.annotation.ZnsClient; 4 | import com.buildupchao.zns.client.bean.ProviderService; 5 | import com.buildupchao.zns.client.cache.ServiceRouteCache; 6 | import com.buildupchao.zns.client.config.ZnsClientConfiguration; 7 | import com.buildupchao.zns.client.util.ZKit; 8 | import org.apache.commons.collections.CollectionUtils; 9 | import org.reflections.Reflections; 10 | import org.slf4j.Logger; 11 | import org.slf4j.LoggerFactory; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.stereotype.Component; 14 | 15 | import java.util.List; 16 | import java.util.Set; 17 | 18 | /** 19 | * Service pull manager: 20 | * Pull service provider list from zookeeper 21 | * 22 | * @author buildupchao 23 | * @date 2019/2/1 02:32 24 | * @since JDK 1.8 25 | */ 26 | @Component 27 | public class ServicePullManager { 28 | 29 | private static final Logger LOGGER = LoggerFactory.getLogger(ServicePullManager.class); 30 | 31 | @Autowired 32 | private ZKit zKit; 33 | 34 | @Autowired 35 | private ServiceRouteCache serviceRouteCache; 36 | 37 | @Autowired 38 | private ZnsClientConfiguration configuration; 39 | 40 | public void pullServiceFromZK() { 41 | Reflections reflections = new Reflections(configuration.getZnsClientApiPackage()); 42 | Set> typesAnnotatedWith = reflections.getTypesAnnotatedWith(ZnsClient.class); 43 | if (CollectionUtils.isEmpty(typesAnnotatedWith)) { 44 | return; 45 | } 46 | for (Class cls : typesAnnotatedWith) { 47 | String serviceName = cls.getName(); 48 | 49 | // Cache service provider list into local 50 | List providerServices = zKit.getServiceInfos(serviceName); 51 | serviceRouteCache.addCache(serviceName, providerServices); 52 | 53 | // Add listener for service node 54 | zKit.subscribeZKEvent(serviceName); 55 | } 56 | 57 | LOGGER.info("Pull service address list from zookeeper successfully"); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/runner/ZnsRequestManager.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.runner; 2 | 3 | import com.buildupchao.zns.client.bean.ChannelHolder; 4 | import com.buildupchao.zns.client.bean.ProviderService; 5 | import com.buildupchao.zns.client.cache.ServiceRouteCache; 6 | import com.buildupchao.zns.client.cluster.ClusterStrategy; 7 | import com.buildupchao.zns.client.cluster.engine.ClusterEngine; 8 | import com.buildupchao.zns.client.config.ZnsClientConfiguration; 9 | import com.buildupchao.zns.client.connector.ZnsClientConnector; 10 | import com.buildupchao.zns.client.util.SpringBeanFactory; 11 | import com.buildupchao.zns.common.bean.ZnsRequest; 12 | import com.buildupchao.zns.common.exception.StatusCode; 13 | import com.buildupchao.zns.common.exception.ZnsException; 14 | import org.apache.commons.lang3.StringUtils; 15 | import org.apache.commons.lang3.concurrent.BasicThreadFactory; 16 | import org.slf4j.Logger; 17 | import org.slf4j.LoggerFactory; 18 | 19 | import java.util.List; 20 | import java.util.concurrent.*; 21 | 22 | /** 23 | * @author buildupchao 24 | * @date 2019/2/1 12:53 25 | * @since JDK 1.8 26 | */ 27 | public class ZnsRequestManager { 28 | 29 | private static final Logger LOGGER = LoggerFactory.getLogger(ZnsRequestManager.class); 30 | 31 | private static final ConcurrentHashMap channelHolderMap = new ConcurrentHashMap<>(); 32 | 33 | private static final ExecutorService REQUEST_EXECUTOR = new ThreadPoolExecutor( 34 | 30, 35 | 100, 36 | 0, 37 | TimeUnit.SECONDS, 38 | new ArrayBlockingQueue<>(30), 39 | new BasicThreadFactory.Builder().namingPattern("request-service-connector-%d").build() 40 | ); 41 | 42 | private static ZnsRequestPool ZNS_REQUEST_POOL; 43 | private static ServiceRouteCache SERVICE_ROUTE_CACHE; 44 | private static String CLUSTER_STRATEGY; 45 | 46 | public static void startZnsRequestManager(ZnsRequestPool znsRequestPool, ServiceRouteCache serviceRouteCache) { 47 | ZNS_REQUEST_POOL = znsRequestPool; 48 | SERVICE_ROUTE_CACHE = serviceRouteCache; 49 | CLUSTER_STRATEGY = SpringBeanFactory.getBean(ZnsClientConfiguration.class).getZnsClientClusterStrategy(); 50 | } 51 | 52 | public static void sendRequest(ZnsRequest znsRequest) throws InterruptedException, ZnsException { 53 | ClusterStrategy strategy = ClusterEngine.queryClusterStrategy(CLUSTER_STRATEGY); 54 | List providerServices = SERVICE_ROUTE_CACHE.getServiceRoutes(znsRequest.getClassName()); 55 | ProviderService targetServiceProvider = strategy.select(providerServices); 56 | 57 | if (targetServiceProvider != null) { 58 | String requestId = znsRequest.getRequestId(); 59 | CountDownLatch latch = new CountDownLatch(1); 60 | REQUEST_EXECUTOR.execute(new ZnsClientConnector(requestId, targetServiceProvider, latch)); 61 | 62 | latch.await(); 63 | 64 | ChannelHolder channelHolder = channelHolderMap.get(requestId); 65 | channelHolder.getChannel().writeAndFlush(znsRequest); 66 | LOGGER.info("Send request[{}:{}] to service provider successfully", requestId, znsRequest.toString()); 67 | } else { 68 | throw new ZnsException(StatusCode.NO_AVAILABLE_SERVICE_PROVINDER); 69 | } 70 | } 71 | 72 | public static void registerChannelHolder(String requestId, ChannelHolder channelHolder) { 73 | if (StringUtils.isBlank(requestId) || channelHolder == null) { 74 | return; 75 | } 76 | channelHolderMap.put(requestId, channelHolder); 77 | LOGGER.info("Register ChannelHolder[{}:{}] successfully", requestId, channelHolder.toString()); 78 | 79 | ZNS_REQUEST_POOL.submitRequest(requestId, channelHolder.getChannel().eventLoop()); 80 | LOGGER.info("Submit request into ZnsRequestPool successfully"); 81 | } 82 | 83 | public static void destroyChannelHolder(String requestId) { 84 | if (StringUtils.isBlank(requestId)) { 85 | return; 86 | } 87 | ChannelHolder channelHolder = channelHolderMap.remove(requestId); 88 | try { 89 | channelHolder.getChannel().closeFuture(); 90 | channelHolder.getEventLoopGroup().shutdownGracefully(); 91 | } catch (Exception ex) { 92 | LOGGER.error("Close ChannelHolder[{}] error", requestId); 93 | } 94 | LOGGER.info("Destroy ChannelHolder[{}] successfully", requestId); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/runner/ZnsRequestPool.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.runner; 2 | 3 | import com.buildupchao.zns.common.bean.ZnsResponse; 4 | import io.netty.util.concurrent.DefaultPromise; 5 | import io.netty.util.concurrent.EventExecutor; 6 | import io.netty.util.concurrent.Promise; 7 | import org.springframework.stereotype.Component; 8 | 9 | import java.util.concurrent.ConcurrentHashMap; 10 | import java.util.concurrent.TimeUnit; 11 | 12 | /** 13 | * @author buildupchao 14 | * @date 2019/2/1 03:16 15 | * @since JDK 1.8 16 | */ 17 | @Component 18 | public class ZnsRequestPool { 19 | 20 | private final ConcurrentHashMap> requestPool = new ConcurrentHashMap<>(); 21 | 22 | public void submitRequest(String requestId, EventExecutor executor) { 23 | requestPool.put(requestId, new DefaultPromise<>(executor)); 24 | } 25 | 26 | public ZnsResponse fetchResponse(String requestId) throws Exception { 27 | Promise promise = requestPool.get(requestId); 28 | if (promise == null) { 29 | return null; 30 | } 31 | ZnsResponse znsResponse = promise.get(10, TimeUnit.SECONDS); 32 | requestPool.remove(requestId); 33 | 34 | ZnsRequestManager.destroyChannelHolder(requestId); 35 | return znsResponse; 36 | } 37 | 38 | public void notifyRequest(String requestId, ZnsResponse znsResponse) { 39 | Promise promise = requestPool.get(requestId); 40 | if (promise != null) { 41 | promise.setSuccess(znsResponse); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/util/RequestIdUtil.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.util; 2 | 3 | import java.util.UUID; 4 | 5 | /** 6 | * @author buildupchao 7 | * @date 2019/2/1 12:49 8 | * @since JDK 1.8 9 | */ 10 | public class RequestIdUtil { 11 | 12 | public static String requestId() { 13 | return UUID.randomUUID().toString(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/util/SpringBeanFactory.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.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 | import java.lang.annotation.Annotation; 9 | import java.util.Map; 10 | 11 | /** 12 | * @author buildupchao 13 | * @date 2019/1/31 21:52 14 | * @since JDK 1.8 15 | */ 16 | @Component 17 | public class SpringBeanFactory implements ApplicationContextAware { 18 | 19 | private static ApplicationContext context; 20 | 21 | public static T getBean(Class cls) { 22 | return context.getBean(cls); 23 | } 24 | 25 | public static Map getBeanListByAnnotationClass(Class annotationClass) { 26 | return context.getBeansWithAnnotation(annotationClass); 27 | } 28 | 29 | public static ApplicationContext context() { 30 | return context; 31 | } 32 | 33 | @Override 34 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 35 | context = applicationContext; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /zns-client/src/main/java/com/buildupchao/zns/client/util/ZKit.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.client.util; 2 | 3 | import com.buildupchao.zns.client.bean.ProviderService; 4 | import com.buildupchao.zns.client.cache.ServiceRouteCache; 5 | import com.buildupchao.zns.client.config.ZnsClientConfiguration; 6 | import com.google.common.collect.Lists; 7 | import org.I0Itec.zkclient.IZkChildListener; 8 | import org.I0Itec.zkclient.ZkClient; 9 | import org.apache.commons.collections.CollectionUtils; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.stereotype.Component; 12 | 13 | import java.util.List; 14 | import java.util.stream.Collectors; 15 | 16 | /** 17 | * @author buildupchao 18 | * @date 2019/2/1 01:24 19 | * @since JDK 1.8 20 | */ 21 | @Component 22 | public class ZKit { 23 | 24 | @Autowired 25 | private ZnsClientConfiguration configuration; 26 | 27 | @Autowired 28 | private ZkClient zkClient; 29 | 30 | @Autowired 31 | private ServiceRouteCache serviceRouteCache; 32 | 33 | public void subscribeZKEvent(String serviceName) { 34 | String path = configuration.getZkRoot() + "/" + serviceName; 35 | zkClient.subscribeChildChanges(path, new IZkChildListener() { 36 | @Override 37 | public void handleChildChange(String parentPath, List list) throws Exception { 38 | if (CollectionUtils.isNotEmpty(list)) { 39 | List providerServices = convertToProviderService(list); 40 | serviceRouteCache.updateCache(serviceName, providerServices); 41 | } 42 | } 43 | }); 44 | } 45 | 46 | public List getServiceInfos(String serviceName) { 47 | String path = configuration.getZkRoot() + "/" + serviceName; 48 | List children = zkClient.getChildren(path); 49 | 50 | List providerServices = convertToProviderService(children); 51 | return providerServices; 52 | } 53 | 54 | private List convertToProviderService(List list) { 55 | if (CollectionUtils.isEmpty(list)) { 56 | return Lists.newArrayListWithCapacity(0); 57 | } 58 | List providerServices = list.stream().map(v -> { 59 | String[] serviceInfos = v.split(":"); 60 | return ProviderService.builder() 61 | .serverIp(serviceInfos[0]) 62 | .serverPort(Integer.parseInt(serviceInfos[1])) 63 | .networkPort(Integer.parseInt(serviceInfos[2])) 64 | .build(); 65 | }).collect(Collectors.toList()); 66 | return providerServices; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /zns-client/target/maven-archiver/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven 2 | #Mon Feb 04 07:05:20 CST 2019 3 | version=1.0-SNAPSHOT 4 | groupId=com.buildupchao 5 | artifactId=zns-client 6 | -------------------------------------------------------------------------------- /zns-client/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst: -------------------------------------------------------------------------------- 1 | com/buildupchao/zns/client/cluster/impl/PollingClusterStrategyImpl.class 2 | -------------------------------------------------------------------------------- /zns-client/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/runner/ZnsRequestManager.java 2 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/connector/handler/ZnsResponseHandler.java 3 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/config/BeanConfig.java 4 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/connector/init/ZnsClientInitializer.java 5 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/bean/ChannelHolder.java 6 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/cluster/impl/RandomClusterStrategyImpl.java 7 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/proxy/ServiceProxyManager.java 8 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/connector/handler/ZnsClientEncodeHandler.java 9 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/util/SpringBeanFactory.java 10 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/bean/ProviderService.java 11 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/runner/ZnsRequestPool.java 12 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/connector/ZnsClientConnector.java 13 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/pull/ServicePullManager.java 14 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/util/RequestIdUtil.java 15 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/cluster/engine/ClusterEngine.java 16 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/config/ZnsClientConfiguration.java 17 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/ZnsClientRunner.java 18 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/util/ZKit.java 19 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/ZnsClientPackage.java 20 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/cache/ServiceRouteCache.java 21 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/cluster/ClusterStrategy.java 22 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/cluster/ClusterStrategyEnum.java 23 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/connector/handler/ZnsClientDecodeHandler.java 24 | /Users/yachao/work/learn/git/zns/zns-client/src/main/java/com/buildupchao/zns/client/proxy/ProxyHelper.java 25 | -------------------------------------------------------------------------------- /zns-client/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/buildupchao/zns/3ea7b6ae0c90c7975d024d425e9cf6775e8c4e13/zns-client/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst -------------------------------------------------------------------------------- /zns-common/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | zns 7 | com.buildupchao 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 1.0-SNAPSHOT 12 | zns-common 13 | jar 14 | 15 | 16 | 17 | org.projectlombok 18 | lombok 19 | 20 | 21 | org.objenesis 22 | objenesis 23 | 24 | 25 | com.dyuproject.protostuff 26 | protostuff-core 27 | 28 | 29 | com.dyuproject.protostuff 30 | protostuff-runtime 31 | 32 | 33 | 34 | commons-collections 35 | commons-collections 36 | 37 | 38 | org.apache.commons 39 | commons-lang3 40 | 41 | 42 | commons-beanutils 43 | commons-beanutils 44 | 45 | 46 | com.google.guava 47 | guava 48 | 49 | 50 | 51 | org.slf4j 52 | slf4j-log4j12 53 | 54 | 55 | junit 56 | junit 57 | 58 | 59 | 60 | 61 | 62 | org.apache.maven.plugins 63 | maven-compiler-plugin 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /zns-common/src/main/java/com/buildupchao/zns/common/bean/ZnsRequest.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.common.bean; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Builder; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | /** 9 | * @author buildupchao 10 | * @date 2019/1/31 18:18 11 | * @since JDK 1.8 12 | */ 13 | @Data 14 | @Builder 15 | @NoArgsConstructor 16 | @AllArgsConstructor 17 | public class ZnsRequest { 18 | private String requestId; 19 | private String className; 20 | private String methodName; 21 | private Class[] parameterTypes; 22 | private Object[] parameters; 23 | } 24 | -------------------------------------------------------------------------------- /zns-common/src/main/java/com/buildupchao/zns/common/bean/ZnsResponse.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.common.bean; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Builder; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | /** 9 | * @author buildupchao 10 | * @date 2019/1/31 18:19 11 | * @since JDK 1.8 12 | */ 13 | @Data 14 | @Builder 15 | @NoArgsConstructor 16 | @AllArgsConstructor 17 | public class ZnsResponse { 18 | private String requestId; 19 | private Object result; 20 | private Throwable cause; 21 | 22 | public boolean isError() { 23 | return cause != null; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /zns-common/src/main/java/com/buildupchao/zns/common/exception/StatusCode.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.common.exception; 2 | 3 | import lombok.Getter; 4 | 5 | /** 6 | * @author buildupchao 7 | * @date 2019/09/19 00:52 8 | * @since JDK 1.8 9 | */ 10 | @Getter 11 | public enum StatusCode { 12 | SUCCESS(200, "OK"), 13 | 14 | NO_AVAILABLE_SERVICE_PROVINDER(100001, "no available service provider"); 15 | 16 | 17 | private Integer code; 18 | private String description; 19 | 20 | StatusCode(Integer code, String description) { 21 | this.code = code; 22 | this.description = description; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /zns-common/src/main/java/com/buildupchao/zns/common/exception/ZnsException.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.common.exception; 2 | 3 | /** 4 | * @author buildupchao 5 | * @date 2019/2/1 02:27 6 | * @since JDK 1.8 7 | */ 8 | public class ZnsException extends RuntimeException { 9 | 10 | public ZnsException() { 11 | } 12 | 13 | public ZnsException(String message) { 14 | super(message); 15 | } 16 | 17 | public ZnsException(String message, Throwable cause) { 18 | super(message, cause); 19 | } 20 | 21 | public ZnsException(Throwable cause) { 22 | super(cause); 23 | } 24 | 25 | public ZnsException(StatusCode statusCode) { 26 | super(statusCode.getDescription()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /zns-common/src/main/java/com/buildupchao/zns/common/serialize/ISerializer.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.common.serialize; 2 | 3 | /** 4 | * @author buildupchao 5 | * @date 2019/1/31 18:21 6 | * @since JDK 1.8 7 | */ 8 | public interface ISerializer { 9 | 10 | byte[] serialize(T t); 11 | 12 | T deserialize(byte[] bytes, Class cls); 13 | } 14 | -------------------------------------------------------------------------------- /zns-common/src/main/java/com/buildupchao/zns/common/serialize/impl/JsonSerializer.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.common.serialize.impl; 2 | 3 | import com.buildupchao.zns.common.serialize.ISerializer; 4 | import com.dyuproject.protostuff.LinkedBuffer; 5 | import com.dyuproject.protostuff.ProtobufIOUtil; 6 | import com.dyuproject.protostuff.Schema; 7 | import com.dyuproject.protostuff.runtime.RuntimeSchema; 8 | import org.objenesis.Objenesis; 9 | import org.objenesis.ObjenesisStd; 10 | 11 | import java.util.Map; 12 | import java.util.concurrent.ConcurrentHashMap; 13 | 14 | /** 15 | * @author buildupchao 16 | * @date 2019/1/31 18:23 17 | * @since JDK 1.8 18 | */ 19 | public class JsonSerializer implements ISerializer { 20 | 21 | private static Map, Schema> classSchemaMap = new ConcurrentHashMap<>(); 22 | private static Objenesis objenesis = new ObjenesisStd(true); 23 | 24 | @Override 25 | public byte[] serialize(T t) { 26 | Class cls = (Class) t.getClass(); 27 | LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); 28 | 29 | try { 30 | Schema schema = getClassSchema(cls); 31 | return ProtobufIOUtil.toByteArray(t, schema, buffer); 32 | } catch (Exception e) { 33 | throw new IllegalStateException(e.getMessage(), e); 34 | }finally { 35 | buffer.clear(); 36 | } 37 | } 38 | 39 | @Override 40 | public T deserialize(byte[] bytes, Class cls) { 41 | try { 42 | Schema schema = getClassSchema(cls); 43 | T message = objenesis.newInstance(cls); 44 | ProtobufIOUtil.mergeFrom(bytes, message, schema); 45 | return message; 46 | } catch (Exception e) { 47 | throw new IllegalStateException(e.getMessage(), e); 48 | } 49 | } 50 | 51 | private Schema getClassSchema(Class cls) { 52 | Schema classSchema = null; 53 | if (classSchemaMap.containsKey(cls)) { 54 | classSchema = (Schema) classSchemaMap.get(cls); 55 | } else { 56 | classSchema = RuntimeSchema.getSchema(cls); 57 | if (classSchema != null) { 58 | classSchemaMap.put(cls, classSchema); 59 | } 60 | } 61 | return classSchema; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /zns-common/src/main/java/com/buildupchao/zns/common/util/IpUtil.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.common.util; 2 | 3 | import com.buildupchao.zns.common.exception.ZnsException; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | import java.net.InetAddress; 8 | import java.net.NetworkInterface; 9 | import java.net.SocketException; 10 | import java.net.UnknownHostException; 11 | import java.util.Enumeration; 12 | 13 | /** 14 | * @author buildupchao 15 | * @date 2019/1/31 21:05 16 | * @since JDK 1.8 17 | */ 18 | public class IpUtil { 19 | 20 | private static final Logger LOGGER = LoggerFactory.getLogger(IpUtil.class); 21 | 22 | public static String getHostAddress() { 23 | String host = null; 24 | try { 25 | host = InetAddress.getLocalHost().getHostAddress(); 26 | return host; 27 | } catch (UnknownHostException e) { 28 | LOGGER.error("Cannot get server host.", e); 29 | } 30 | return host; 31 | } 32 | 33 | public static String getRealIp() { 34 | String localIp = null; 35 | String netIp = null; 36 | 37 | try { 38 | Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); 39 | boolean finded = false; 40 | InetAddress ip = null; 41 | while (networkInterfaces.hasMoreElements() && !finded) { 42 | NetworkInterface networkInterface = networkInterfaces.nextElement(); 43 | Enumeration addresses = networkInterface.getInetAddresses(); 44 | while (addresses.hasMoreElements()) { 45 | ip = addresses.nextElement(); 46 | if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) { 47 | netIp = ip.getHostAddress(); 48 | finded = true; 49 | break; 50 | } else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) { 51 | 52 | localIp = ip.getHostAddress(); 53 | } 54 | } 55 | } 56 | 57 | if (netIp != null && !"".equals(netIp)) { 58 | return netIp; 59 | } else { 60 | return localIp; 61 | } 62 | } catch (SocketException ex) { 63 | throw new ZnsException(ex); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /zns-common/src/main/java/com/buildupchao/zns/common/util/SerializationUtil.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.common.util; 2 | 3 | import com.buildupchao.zns.common.serialize.ISerializer; 4 | import com.buildupchao.zns.common.serialize.impl.JsonSerializer; 5 | 6 | /** 7 | * @author buildupchao 8 | * @date 2019/1/31 22:03 9 | * @since JDK 1.8 10 | */ 11 | public class SerializationUtil { 12 | 13 | private static final ISerializer serializer = new JsonSerializer(); 14 | 15 | public static byte[] serialize(T t) { 16 | return serializer.serialize(t); 17 | } 18 | 19 | public static T deserialize(byte[] bytes, Class cls) { 20 | return serializer.deserialize(bytes, cls); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /zns-common/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=info, systemOut 2 | 3 | log4j.appender.systemOut=org.apache.log4j.ConsoleAppender 4 | log4j.appender.systemOut.layout=org.apache.log4j.PatternLayout 5 | 6 | # Pattern to output the caller's file name and line number. 7 | log4j.appender.systemOut.layout.ConversionPattern= %d %5p [%t] (%F:%L) - %m%n -------------------------------------------------------------------------------- /zns-common/src/test/java/com/buildupchao/zns/common/util/IpUtilTests.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.common.util; 2 | 3 | import org.junit.Test; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | /** 8 | * @author buildupchao 9 | * Date: 2019/1/31 21:11 10 | * @since JDK 1.8 11 | */ 12 | public class IpUtilTests { 13 | 14 | private static final Logger LOGGER = LoggerFactory.getLogger(IpUtilTests.class); 15 | 16 | @Test 17 | public void testGetHost() { 18 | LOGGER.info("Test getHost: {}", IpUtil.getRealIp()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /zns-common/target/classes/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=info, systemOut 2 | 3 | log4j.appender.systemOut=org.apache.log4j.ConsoleAppender 4 | log4j.appender.systemOut.layout=org.apache.log4j.PatternLayout 5 | 6 | # Pattern to output the caller's file name and line number. 7 | log4j.appender.systemOut.layout.ConversionPattern= %d %5p [%t] (%F:%L) - %m%n -------------------------------------------------------------------------------- /zns-common/target/maven-archiver/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven 2 | #Mon Feb 04 07:05:18 CST 2019 3 | version=1.0-SNAPSHOT 4 | groupId=com.buildupchao 5 | artifactId=zns-common 6 | -------------------------------------------------------------------------------- /zns-common/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/buildupchao/zns/3ea7b6ae0c90c7975d024d425e9cf6775e8c4e13/zns-common/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst -------------------------------------------------------------------------------- /zns-common/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | /Users/yachao/work/learn/git/zns/zns-common/src/main/java/com/buildupchao/zns/common/bean/ZnsResponse.java 2 | /Users/yachao/work/learn/git/zns/zns-common/src/main/java/com/buildupchao/zns/common/util/IpUtil.java 3 | /Users/yachao/work/learn/git/zns/zns-common/src/main/java/com/buildupchao/zns/common/util/SerializationUtil.java 4 | /Users/yachao/work/learn/git/zns/zns-common/src/main/java/com/buildupchao/zns/common/bean/ZnsRequest.java 5 | /Users/yachao/work/learn/git/zns/zns-common/src/main/java/com/buildupchao/zns/common/serialize/impl/JsonSerializer.java 6 | /Users/yachao/work/learn/git/zns/zns-common/src/main/java/com/buildupchao/zns/common/exception/ZnsException.java 7 | /Users/yachao/work/learn/git/zns/zns-common/src/main/java/com/buildupchao/zns/common/serialize/ISerializer.java 8 | -------------------------------------------------------------------------------- /zns-common/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/buildupchao/zns/3ea7b6ae0c90c7975d024d425e9cf6775e8c4e13/zns-common/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst -------------------------------------------------------------------------------- /zns-common/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | /Users/yachao/work/learn/git/zns/zns-common/src/test/java/com/buildupchao/zns/common/util/IpUtilTests.java 2 | -------------------------------------------------------------------------------- /zns-monitor/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /nbbuild/ 22 | /dist/ 23 | /nbdist/ 24 | /.nb-gradle/ 25 | /build/ 26 | -------------------------------------------------------------------------------- /zns-monitor/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/buildupchao/zns/3ea7b6ae0c90c7975d024d425e9cf6775e8c4e13/zns-monitor/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /zns-monitor/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /zns-monitor/mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Mingw, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | # TODO classpath? 118 | fi 119 | 120 | if [ -z "$JAVA_HOME" ]; then 121 | javaExecutable="`which javac`" 122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 123 | # readlink(1) is not available as standard on Solaris 10. 124 | readLink=`which readlink` 125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 126 | if $darwin ; then 127 | javaHome="`dirname \"$javaExecutable\"`" 128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 129 | else 130 | javaExecutable="`readlink -f \"$javaExecutable\"`" 131 | fi 132 | javaHome="`dirname \"$javaExecutable\"`" 133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 134 | JAVA_HOME="$javaHome" 135 | export JAVA_HOME 136 | fi 137 | fi 138 | fi 139 | 140 | if [ -z "$JAVACMD" ] ; then 141 | if [ -n "$JAVA_HOME" ] ; then 142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 143 | # IBM's JDK on AIX uses strange locations for the executables 144 | JAVACMD="$JAVA_HOME/jre/sh/java" 145 | else 146 | JAVACMD="$JAVA_HOME/bin/java" 147 | fi 148 | else 149 | JAVACMD="`which java`" 150 | fi 151 | fi 152 | 153 | if [ ! -x "$JAVACMD" ] ; then 154 | echo "Error: JAVA_HOME is not defined correctly." >&2 155 | echo " We cannot execute $JAVACMD" >&2 156 | exit 1 157 | fi 158 | 159 | if [ -z "$JAVA_HOME" ] ; then 160 | echo "Warning: JAVA_HOME environment variable is not set." 161 | fi 162 | 163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 164 | 165 | # traverses directory structure from process work directory to filesystem root 166 | # first directory with .mvn subdirectory is considered project base directory 167 | find_maven_basedir() { 168 | 169 | if [ -z "$1" ] 170 | then 171 | echo "Path not specified to find_maven_basedir" 172 | return 1 173 | fi 174 | 175 | basedir="$1" 176 | wdir="$1" 177 | while [ "$wdir" != '/' ] ; do 178 | if [ -d "$wdir"/.mvn ] ; then 179 | basedir=$wdir 180 | break 181 | fi 182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 183 | if [ -d "${wdir}" ]; then 184 | wdir=`cd "$wdir/.."; pwd` 185 | fi 186 | # end of workaround 187 | done 188 | echo "${basedir}" 189 | } 190 | 191 | # concatenates all lines of a file 192 | concat_lines() { 193 | if [ -f "$1" ]; then 194 | echo "$(tr -s '\n' ' ' < "$1")" 195 | fi 196 | } 197 | 198 | BASE_DIR=`find_maven_basedir "$(pwd)"` 199 | if [ -z "$BASE_DIR" ]; then 200 | exit 1; 201 | fi 202 | 203 | ########################################################################################## 204 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 205 | # This allows using the maven wrapper in projects that prohibit checking in binary data. 206 | ########################################################################################## 207 | if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then 208 | if [ "$MVNW_VERBOSE" = true ]; then 209 | echo "Found .mvn/wrapper/maven-wrapper.jar" 210 | fi 211 | else 212 | if [ "$MVNW_VERBOSE" = true ]; then 213 | echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." 214 | fi 215 | jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar" 216 | while IFS="=" read key value; do 217 | case "$key" in (wrapperUrl) jarUrl="$value"; break ;; 218 | esac 219 | done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" 220 | if [ "$MVNW_VERBOSE" = true ]; then 221 | echo "Downloading from: $jarUrl" 222 | fi 223 | wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" 224 | 225 | if command -v wget > /dev/null; then 226 | if [ "$MVNW_VERBOSE" = true ]; then 227 | echo "Found wget ... using wget" 228 | fi 229 | wget "$jarUrl" -O "$wrapperJarPath" 230 | elif command -v curl > /dev/null; then 231 | if [ "$MVNW_VERBOSE" = true ]; then 232 | echo "Found curl ... using curl" 233 | fi 234 | curl -o "$wrapperJarPath" "$jarUrl" 235 | else 236 | if [ "$MVNW_VERBOSE" = true ]; then 237 | echo "Falling back to using Java to download" 238 | fi 239 | javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" 240 | if [ -e "$javaClass" ]; then 241 | if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 242 | if [ "$MVNW_VERBOSE" = true ]; then 243 | echo " - Compiling MavenWrapperDownloader.java ..." 244 | fi 245 | # Compiling the Java class 246 | ("$JAVA_HOME/bin/javac" "$javaClass") 247 | fi 248 | if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 249 | # Running the downloader 250 | if [ "$MVNW_VERBOSE" = true ]; then 251 | echo " - Running MavenWrapperDownloader.java ..." 252 | fi 253 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") 254 | fi 255 | fi 256 | fi 257 | fi 258 | ########################################################################################## 259 | # End of extension 260 | ########################################################################################## 261 | 262 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 263 | if [ "$MVNW_VERBOSE" = true ]; then 264 | echo $MAVEN_PROJECTBASEDIR 265 | fi 266 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 267 | 268 | # For Cygwin, switch paths to Windows format before running java 269 | if $cygwin; then 270 | [ -n "$M2_HOME" ] && 271 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 272 | [ -n "$JAVA_HOME" ] && 273 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 274 | [ -n "$CLASSPATH" ] && 275 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 276 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 277 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 278 | fi 279 | 280 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 281 | 282 | exec "$JAVACMD" \ 283 | $MAVEN_OPTS \ 284 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 285 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 286 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 287 | -------------------------------------------------------------------------------- /zns-monitor/mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM set title of command window 39 | title %0 40 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 42 | 43 | @REM set %HOME% to equivalent of $HOME 44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 45 | 46 | @REM Execute a user defined script before this one 47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 49 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 50 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 51 | :skipRcPre 52 | 53 | @setlocal 54 | 55 | set ERROR_CODE=0 56 | 57 | @REM To isolate internal variables from possible post scripts, we use another setlocal 58 | @setlocal 59 | 60 | @REM ==== START VALIDATION ==== 61 | if not "%JAVA_HOME%" == "" goto OkJHome 62 | 63 | echo. 64 | echo Error: JAVA_HOME not found in your environment. >&2 65 | echo Please set the JAVA_HOME variable in your environment to match the >&2 66 | echo location of your Java installation. >&2 67 | echo. 68 | goto error 69 | 70 | :OkJHome 71 | if exist "%JAVA_HOME%\bin\java.exe" goto init 72 | 73 | echo. 74 | echo Error: JAVA_HOME is set to an invalid directory. >&2 75 | echo JAVA_HOME = "%JAVA_HOME%" >&2 76 | echo Please set the JAVA_HOME variable in your environment to match the >&2 77 | echo location of your Java installation. >&2 78 | echo. 79 | goto error 80 | 81 | @REM ==== END VALIDATION ==== 82 | 83 | :init 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 121 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 122 | 123 | set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar" 124 | FOR /F "tokens=1,2 delims==" %%A IN (%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties) DO ( 125 | IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B 126 | ) 127 | 128 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 129 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data. 130 | if exist %WRAPPER_JAR% ( 131 | echo Found %WRAPPER_JAR% 132 | ) else ( 133 | echo Couldn't find %WRAPPER_JAR%, downloading it ... 134 | echo Downloading from: %DOWNLOAD_URL% 135 | powershell -Command "(New-Object Net.WebClient).DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')" 136 | echo Finished downloading %WRAPPER_JAR% 137 | ) 138 | @REM End of extension 139 | 140 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 141 | if ERRORLEVEL 1 goto error 142 | goto end 143 | 144 | :error 145 | set ERROR_CODE=1 146 | 147 | :end 148 | @endlocal & set ERROR_CODE=%ERROR_CODE% 149 | 150 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 151 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 152 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 153 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 154 | :skipRcPost 155 | 156 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 157 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 158 | 159 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 160 | 161 | exit /B %ERROR_CODE% 162 | -------------------------------------------------------------------------------- /zns-monitor/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | zns 8 | com.buildupchao 9 | 1.0-SNAPSHOT 10 | 11 | com.buildupchao 12 | zns-monitor 13 | 0.0.1-SNAPSHOT 14 | zns-monitor 15 | ZNS monitor for server and client. 16 | 17 | 18 | UTF-8 19 | UTF-8 20 | 1.8 21 | 22 | 23 | 24 | 25 | com.buildupchao 26 | zns-common 27 | 1.0-SNAPSHOT 28 | 29 | 30 | io.springfox 31 | springfox-swagger2 32 | compile 33 | 2.5.0 34 | 35 | 36 | io.springfox 37 | springfox-swagger-ui 38 | 2.5.0 39 | 40 | 41 | de.codecentric 42 | spring-boot-admin-server 43 | 1.5.6 44 | 45 | 46 | de.codecentric 47 | spring-boot-admin-server-ui 48 | 1.5.6 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-configuration-processor 53 | true 54 | 1.5.6.RELEASE 55 | 56 | 57 | com.github.sgroschupf 58 | zkclient 59 | 60 | 61 | 62 | 63 | 64 | 65 | org.springframework.boot 66 | spring-boot-maven-plugin 67 | 68 | 69 | 70 | repackage 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /zns-monitor/src/main/java/com/buildupchao/zns/monitor/ZnsMonitorApplication.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.monitor; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ZnsMonitorApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ZnsMonitorApplication.class, args); 11 | } 12 | 13 | } 14 | 15 | -------------------------------------------------------------------------------- /zns-monitor/src/main/java/com/buildupchao/zns/monitor/config/SwaggerConfig.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.monitor.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 | * @author buildupchao 16 | * @date 2019/2/4 05:41 17 | * @since JDK 1.8 18 | */ 19 | @Configuration 20 | @EnableSwagger2 21 | @ConditionalOnExpression("'${swagger.enable}' == 'true'") 22 | public class SwaggerConfig { 23 | 24 | 25 | @Bean 26 | public Docket createRestApi() { 27 | return new Docket(DocumentationType.SWAGGER_2) 28 | .apiInfo(apiInfo()) 29 | .select() 30 | .apis(RequestHandlerSelectors.basePackage("com.buildupchao.zns.monitor.controller")) 31 | .paths(PathSelectors.any()) 32 | .build(); 33 | } 34 | 35 | private ApiInfo apiInfo() { 36 | return new ApiInfoBuilder() 37 | .title("zns-monitor api") 38 | .description("zns-monitor zk api") 39 | .termsOfServiceUrl("https://buildupchao.cn") 40 | .contact("buildupchao") 41 | .version("1.0.0") 42 | .build(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /zns-monitor/src/main/java/com/buildupchao/zns/monitor/config/ZnsMonitorConfig.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.monitor.config; 2 | 3 | import com.google.common.cache.CacheBuilder; 4 | import com.google.common.cache.CacheLoader; 5 | import com.google.common.cache.LoadingCache; 6 | import org.I0Itec.zkclient.ZkClient; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | 11 | import java.util.List; 12 | 13 | /** 14 | * @author buildupchao 15 | * @date 2019/2/4 05:48 16 | * @since JDK 1.8 17 | */ 18 | @Configuration 19 | public class ZnsMonitorConfig { 20 | 21 | @Autowired 22 | private ZnsMonitorConfigProperties properties; 23 | 24 | @Bean 25 | public ZkClient zkClient() { 26 | return new ZkClient(properties.getZkAddr(), 5000); 27 | } 28 | 29 | 30 | @Bean 31 | public LoadingCache> buildCache() { 32 | return CacheBuilder.newBuilder() 33 | .build(new CacheLoader>() { 34 | @Override 35 | public List load(String s) throws Exception { 36 | return null; 37 | } 38 | }); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /zns-monitor/src/main/java/com/buildupchao/zns/monitor/config/ZnsMonitorConfigProperties.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.monitor.config; 2 | 3 | import lombok.Data; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.stereotype.Component; 6 | 7 | /** 8 | * @author buildupchao 9 | * @date 2019/2/4 05:46 10 | * @since JDK 1.8 11 | */ 12 | @Data 13 | @Component 14 | public class ZnsMonitorConfigProperties { 15 | 16 | @Value("${zns.monitor.zk.root}") 17 | private String zkRoot; 18 | 19 | @Value("${zns.monitor.zk.addr}") 20 | private String zkAddr; 21 | 22 | @Value("${zns.monitor.zk.switch}") 23 | private boolean zkSwitch; 24 | 25 | @Value("${server.port}") 26 | private int port; 27 | } 28 | -------------------------------------------------------------------------------- /zns-monitor/src/main/java/com/buildupchao/zns/monitor/controller/MonitorController.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.monitor.controller; 2 | 3 | import org.springframework.web.bind.annotation.RequestMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | /** 7 | * @author buildupchao 8 | * @date 2019/2/4 05:29 9 | * @since JDK 1.8 10 | */ 11 | @RestController 12 | @RequestMapping("/monitor") 13 | public class MonitorController { 14 | 15 | } 16 | -------------------------------------------------------------------------------- /zns-monitor/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: zns-monitor 4 | server: 5 | port: 9002 6 | 7 | swagger: 8 | enable: true 9 | logging: 10 | level: 11 | root: info 12 | 13 | zns: 14 | monitor: 15 | zk: 16 | root: /zns 17 | addr: localhost:2181 18 | switch: true 19 | -------------------------------------------------------------------------------- /zns-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | zns 7 | com.buildupchao 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 1.0-SNAPSHOT 12 | zns-server 13 | 14 | 15 | 16 | com.buildupchao 17 | zns-api 18 | 1.0-SNAPSHOT 19 | 20 | 21 | com.buildupchao 22 | zns-common 23 | 1.0-SNAPSHOT 24 | 25 | 26 | io.netty 27 | netty-all 28 | 29 | 30 | org.springframework 31 | spring-context 32 | 33 | 34 | com.github.sgroschupf 35 | zkclient 36 | 37 | 38 | 39 | 40 | 41 | org.apache.maven.plugins 42 | maven-compiler-plugin 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /zns-server/src/main/java/com/buildupchao/zns/server/ZnsServerPackage.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.server; 2 | 3 | /** 4 | * @author buildupchao 5 | * @date 2019/2/1 12:08 6 | * @since JDK 1.8 7 | */ 8 | public class ZnsServerPackage { 9 | } 10 | -------------------------------------------------------------------------------- /zns-server/src/main/java/com/buildupchao/zns/server/ZnsServerRunner.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.server; 2 | 3 | import com.buildupchao.zns.server.acceptor.ZnsServerAcceptor; 4 | import com.buildupchao.zns.server.push.ServicePushManager; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Component; 9 | 10 | import javax.annotation.PreDestroy; 11 | import java.util.concurrent.ExecutorService; 12 | import java.util.concurrent.Executors; 13 | 14 | /** 15 | * Manage Center: 16 | * Start netty acceptor which provides network service. 17 | * Register services that owns @ZnsService annotation into zookeeper. 18 | * Start HeartBeatChecker, keep alive with zookeeper. 19 | * 20 | * @author buildupchao 21 | * @date 2019/1/31 20:34 22 | * @since JDK 1.8 23 | */ 24 | @Component 25 | public class ZnsServerRunner { 26 | 27 | private static final Logger LOGGER = LoggerFactory.getLogger(ZnsServerRunner.class); 28 | 29 | private static ExecutorService executor = null; 30 | 31 | @Autowired 32 | private ServicePushManager servicePushManager; 33 | 34 | public void run() { 35 | executor = Executors.newFixedThreadPool(3); 36 | 37 | // Start Acceptor,waiting for the service caller to fire the request call 38 | executor.execute(new ZnsServerAcceptor()); 39 | 40 | // Register service providers into Zookeeper 41 | servicePushManager.registerIntoZK(); 42 | } 43 | 44 | 45 | @PreDestroy 46 | public void destroy() { 47 | if (executor != null) { 48 | executor.shutdown(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /zns-server/src/main/java/com/buildupchao/zns/server/acceptor/ZnsServerAcceptor.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.server.acceptor; 2 | 3 | import com.buildupchao.zns.server.acceptor.init.ZnsServerInitializer; 4 | import com.buildupchao.zns.server.config.ZnsServerConfiguration; 5 | import com.buildupchao.zns.server.util.SpringBeanFactory; 6 | import io.netty.bootstrap.ServerBootstrap; 7 | import io.netty.channel.ChannelFuture; 8 | import io.netty.channel.ChannelOption; 9 | import io.netty.channel.EventLoopGroup; 10 | import io.netty.channel.nio.NioEventLoopGroup; 11 | import io.netty.channel.socket.nio.NioServerSocketChannel; 12 | import io.netty.handler.logging.LogLevel; 13 | import io.netty.handler.logging.LoggingHandler; 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | 17 | /** 18 | * ZnsServer Acceptor: 19 | * Netty acceptor which provides network service. 20 | * 21 | * @author buildupchao 22 | * @date 2019/1/31 21:23 23 | * @since JDK 1.8 24 | */ 25 | public class ZnsServerAcceptor implements Runnable { 26 | 27 | private static final Logger LOGGER = LoggerFactory.getLogger(ZnsServerAcceptor.class); 28 | 29 | private EventLoopGroup boss = new NioEventLoopGroup(); 30 | private EventLoopGroup worker = new NioEventLoopGroup(); 31 | 32 | private ZnsServerConfiguration znsServerConfiguration; 33 | private ZnsServerInitializer znsServerInitializer; 34 | 35 | public ZnsServerAcceptor() { 36 | this.znsServerConfiguration = SpringBeanFactory.getBean(ZnsServerConfiguration.class); 37 | this.znsServerInitializer = SpringBeanFactory.getBean(ZnsServerInitializer.class); 38 | } 39 | 40 | @Override 41 | public void run() { 42 | ServerBootstrap bootstrap = new ServerBootstrap(); 43 | bootstrap.group(boss, worker) 44 | .channel(NioServerSocketChannel.class) 45 | .handler(new LoggingHandler(LogLevel.DEBUG)) 46 | .option(ChannelOption.SO_BACKLOG, 1024) 47 | .childOption(ChannelOption.SO_KEEPALIVE, true) 48 | .childHandler(znsServerInitializer); 49 | 50 | try { 51 | LOGGER.info("ZnsServer acceptor startup at port[{}] successfully", znsServerConfiguration.getNetworkPort()); 52 | 53 | ChannelFuture future = bootstrap.bind(znsServerConfiguration.getNetworkPort()).sync(); 54 | future.channel().closeFuture().sync(); 55 | } catch (InterruptedException e) { 56 | LOGGER.error("ZnsServer acceptor startup failure!", e); 57 | e.printStackTrace(); 58 | } finally { 59 | boss.shutdownGracefully().syncUninterruptibly(); 60 | worker.shutdownGracefully().syncUninterruptibly(); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /zns-server/src/main/java/com/buildupchao/zns/server/acceptor/handler/ZnsRequestHandler.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.server.acceptor.handler; 2 | 3 | import com.buildupchao.zns.common.bean.ZnsRequest; 4 | import com.buildupchao.zns.common.bean.ZnsResponse; 5 | import com.buildupchao.zns.server.util.SpringBeanFactory; 6 | import io.netty.channel.ChannelHandler; 7 | import io.netty.channel.ChannelHandlerContext; 8 | import io.netty.channel.SimpleChannelInboundHandler; 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | import org.springframework.stereotype.Component; 12 | 13 | import java.lang.reflect.Method; 14 | 15 | /** 16 | * @author buildupchao 17 | * @date 2019/1/31 21:45 18 | * @since JDK 1.8 19 | */ 20 | @Component 21 | @ChannelHandler.Sharable 22 | public class ZnsRequestHandler extends SimpleChannelInboundHandler { 23 | 24 | private static final Logger LOGGER = LoggerFactory.getLogger(ZnsRequestHandler.class); 25 | 26 | @Override 27 | protected void channelRead0(ChannelHandlerContext ctx, ZnsRequest znsRequest) throws Exception { 28 | ZnsResponse znsResponse = new ZnsResponse(); 29 | znsResponse.setRequestId(znsRequest.getRequestId()); 30 | 31 | String className = znsRequest.getClassName(); 32 | String methodName = znsRequest.getMethodName(); 33 | Class[] parameterTypes = znsRequest.getParameterTypes(); 34 | Object[] parameterValues = znsRequest.getParameters(); 35 | 36 | try { 37 | Object targetClass = SpringBeanFactory.getBean(Class.forName(className)); 38 | Method targetMethod = targetClass.getClass().getMethod(methodName, parameterTypes); 39 | Object result = targetMethod.invoke(targetClass, parameterValues); 40 | znsResponse.setResult(result); 41 | } catch (Throwable cause) { 42 | znsResponse.setCause(cause); 43 | } 44 | ctx.writeAndFlush(znsResponse); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /zns-server/src/main/java/com/buildupchao/zns/server/acceptor/handler/ZnsServerDecodeHandler.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.server.acceptor.handler; 2 | 3 | import com.buildupchao.zns.common.bean.ZnsRequest; 4 | import com.buildupchao.zns.common.util.SerializationUtil; 5 | import io.netty.buffer.ByteBuf; 6 | import io.netty.channel.ChannelHandlerContext; 7 | import io.netty.handler.codec.ByteToMessageDecoder; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * @author buildupchao 13 | * @date 2019/1/31 21:39 14 | * @since JDK 1.8 15 | */ 16 | public class ZnsServerDecodeHandler extends ByteToMessageDecoder { 17 | 18 | @Override 19 | protected void decode(ChannelHandlerContext ctx, ByteBuf in, List list) throws 20 | Exception { 21 | if (in.readableBytes() <= 4) { 22 | return; 23 | } 24 | 25 | int length = in.readInt(); 26 | in.markReaderIndex(); 27 | if (in.readableBytes() < length) { 28 | in.resetReaderIndex(); 29 | } else { 30 | byte[] bytes = new byte[in.readableBytes()]; 31 | in.readBytes(bytes); 32 | ZnsRequest znsRequest = SerializationUtil.deserialize(bytes, ZnsRequest.class); 33 | list.add(znsRequest); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /zns-server/src/main/java/com/buildupchao/zns/server/acceptor/handler/ZnsServerEncodeHandler.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.server.acceptor.handler; 2 | 3 | import com.buildupchao.zns.common.bean.ZnsResponse; 4 | import com.buildupchao.zns.common.util.SerializationUtil; 5 | import io.netty.buffer.ByteBuf; 6 | import io.netty.channel.ChannelHandlerContext; 7 | import io.netty.handler.codec.MessageToByteEncoder; 8 | 9 | /** 10 | * @author buildupchao 11 | * @date 2019/1/31 21:42 12 | * @since JDK 1.8 13 | */ 14 | public class ZnsServerEncodeHandler extends MessageToByteEncoder { 15 | 16 | @Override 17 | protected void encode(ChannelHandlerContext ctx, ZnsResponse znsResponse, ByteBuf byteBuf) 18 | throws Exception { 19 | byte[] bytes = SerializationUtil.serialize(znsResponse); 20 | byteBuf.writeInt(bytes.length); 21 | byteBuf.writeBytes(bytes); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /zns-server/src/main/java/com/buildupchao/zns/server/acceptor/init/ZnsServerInitializer.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.server.acceptor.init; 2 | 3 | import com.buildupchao.zns.server.acceptor.handler.ZnsRequestHandler; 4 | import com.buildupchao.zns.server.acceptor.handler.ZnsServerDecodeHandler; 5 | import com.buildupchao.zns.server.acceptor.handler.ZnsServerEncodeHandler; 6 | import io.netty.channel.Channel; 7 | import io.netty.channel.ChannelHandler; 8 | import io.netty.channel.ChannelInitializer; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.stereotype.Component; 11 | 12 | /** 13 | * @author buildupchao 14 | * @date 2019/1/31 21:35 15 | * @since JDK 1.8 16 | */ 17 | @Component 18 | @ChannelHandler.Sharable 19 | public class ZnsServerInitializer extends ChannelInitializer { 20 | 21 | @Autowired 22 | private ZnsRequestHandler znsRequestHandler; 23 | 24 | @Override 25 | protected void initChannel(Channel channel) throws Exception { 26 | channel.pipeline() 27 | .addLast(new ZnsServerDecodeHandler()) 28 | .addLast(new ZnsServerEncodeHandler()) 29 | .addLast(znsRequestHandler); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /zns-server/src/main/java/com/buildupchao/zns/server/config/BeanConfig.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.server.config; 2 | 3 | import org.I0Itec.zkclient.ZkClient; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | /** 9 | * @author buildupchao 10 | * @date 2019/1/31 20:52 11 | * @since JDK 1.8 12 | */ 13 | @Configuration 14 | public class BeanConfig { 15 | 16 | @Autowired 17 | private ZnsServerConfiguration znsServerConfiguration; 18 | 19 | @Bean 20 | public ZkClient zkClient() { 21 | return new ZkClient(znsServerConfiguration.getZkAddr(), 5000); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /zns-server/src/main/java/com/buildupchao/zns/server/config/ZnsServerConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.server.config; 2 | 3 | import lombok.Data; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.stereotype.Component; 6 | 7 | /** 8 | * @author buildupchao 9 | * @date 2019/1/31 20:36 10 | * @since JDK 1.8 11 | */ 12 | @Data 13 | @Component 14 | public class ZnsServerConfiguration { 15 | 16 | @Value("${zns.server.zk.root}") 17 | private String zkRoot; 18 | 19 | @Value("${zns.server.zk.addr}") 20 | private String zkAddr; 21 | 22 | @Value("${zns.server.zk.switch}") 23 | private boolean zkSwitch; 24 | 25 | @Value("${zns.network.port}") 26 | private int networkPort; 27 | 28 | @Value("${server.port}") 29 | private int serverPort; 30 | } 31 | -------------------------------------------------------------------------------- /zns-server/src/main/java/com/buildupchao/zns/server/push/ServicePushManager.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.server.push; 2 | 3 | import com.buildupchao.zns.api.annotation.ZnsService; 4 | import com.buildupchao.zns.common.util.IpUtil; 5 | import com.buildupchao.zns.server.config.ZnsServerConfiguration; 6 | import com.buildupchao.zns.server.util.SpringBeanFactory; 7 | import com.buildupchao.zns.server.util.ZKit; 8 | import org.apache.commons.collections.MapUtils; 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.stereotype.Component; 13 | 14 | import java.io.File; 15 | import java.util.Map; 16 | 17 | /** 18 | * Service push manager: 19 | * Provide a service that register service info into zookeeper. 20 | * 21 | * @author buildupchao 22 | * @date 2019/1/31 23:49 23 | * @since JDK 1.8 24 | */ 25 | @Component 26 | public class ServicePushManager { 27 | 28 | private static final Logger LOGGER = LoggerFactory.getLogger(ServicePushManager.class); 29 | 30 | @Autowired 31 | private ZKit zKit; 32 | 33 | @Autowired 34 | private ZnsServerConfiguration configuration; 35 | 36 | public void registerIntoZK() { 37 | Map beanWithAnnotations = 38 | SpringBeanFactory.getBeanListByAnnotationClass(ZnsService.class); 39 | if (MapUtils.isEmpty(beanWithAnnotations)) { 40 | return; 41 | } 42 | 43 | zKit.createRootNode(); 44 | for (Object bean : beanWithAnnotations.values()) { 45 | ZnsService znsService = bean.getClass().getAnnotation(ZnsService.class); 46 | String serviceName = znsService.cls().getName(); 47 | pushServiceInfoIntoZK(serviceName); 48 | } 49 | LOGGER.info("Register service into zookeeper successfully"); 50 | } 51 | 52 | private void pushServiceInfoIntoZK(String serviceName) { 53 | // Create persistent service node 54 | zKit.createPersistentNode(serviceName); 55 | 56 | String serviceAddress = IpUtil.getRealIp() 57 | + ":" + configuration.getServerPort() 58 | + ":" + configuration.getNetworkPort(); 59 | String serviceAddressPath = serviceName + "/" + serviceAddress; 60 | zKit.createNode(serviceAddressPath); 61 | 62 | LOGGER.info("Register service[{}] into zookeeper successfully", serviceAddressPath); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /zns-server/src/main/java/com/buildupchao/zns/server/runner/HeartBeatChecker.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.server.runner; 2 | 3 | /** 4 | * HeartBeat: 5 | * Keep alive with zookeeper. 6 | * 7 | * @author buildupchao 8 | * @date 2019/2/1 00:11 9 | * @since JDK 1.8 10 | */ 11 | public class HeartBeatChecker { 12 | } 13 | -------------------------------------------------------------------------------- /zns-server/src/main/java/com/buildupchao/zns/server/util/ClassUtils.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.server.util; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | /** 7 | * @author buildupchao 8 | * @date 2019/1/31 22:14 9 | * @since JDK 1.8 10 | */ 11 | public class ClassUtils { 12 | 13 | private static final Logger LOGGER = LoggerFactory.getLogger(ClassUtils.class); 14 | 15 | public static ClassLoader getClassLoader() { 16 | return Thread.currentThread().getContextClassLoader(); 17 | } 18 | 19 | public static Class loadClass(String className) { 20 | return loadClass(className, true); 21 | } 22 | 23 | public static Class loadClass(String className, boolean isInitialized) { 24 | Class cls = null; 25 | try { 26 | cls = Class.forName(className, isInitialized, getClassLoader()); 27 | } catch (ClassNotFoundException e) { 28 | LOGGER.error("Load class {} error!", className, e); 29 | throw new RuntimeException(e); 30 | } 31 | return cls; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /zns-server/src/main/java/com/buildupchao/zns/server/util/SpringBeanFactory.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.server.util; 2 | 3 | import com.google.common.collect.Lists; 4 | import org.apache.commons.collections.MapUtils; 5 | import org.springframework.beans.BeansException; 6 | import org.springframework.context.ApplicationContext; 7 | import org.springframework.context.ApplicationContextAware; 8 | import org.springframework.stereotype.Component; 9 | 10 | import java.lang.annotation.Annotation; 11 | import java.util.Map; 12 | 13 | /** 14 | * @author buildupchao 15 | * @date 2019/1/31 21:52 16 | * @since JDK 1.8 17 | */ 18 | @Component 19 | public class SpringBeanFactory implements ApplicationContextAware { 20 | 21 | private static ApplicationContext context; 22 | 23 | public static T getBean(Class cls) { 24 | return context.getBean(cls); 25 | } 26 | 27 | public static Map getBeanListByAnnotationClass(Class annotationClass) { 28 | return context.getBeansWithAnnotation(annotationClass); 29 | } 30 | 31 | @Override 32 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 33 | context = applicationContext; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /zns-server/src/main/java/com/buildupchao/zns/server/util/ZKit.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.server.util; 2 | 3 | import com.buildupchao.zns.server.config.ZnsServerConfiguration; 4 | import org.I0Itec.zkclient.ZkClient; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Component; 7 | 8 | import java.io.File; 9 | 10 | /** 11 | * @author buildupchao 12 | * @date 2019/2/1 00:33 13 | * @since JDK 1.8 14 | */ 15 | @Component 16 | public class ZKit { 17 | 18 | @Autowired 19 | private ZkClient zkClient; 20 | 21 | @Autowired 22 | private ZnsServerConfiguration znsServerConfiguration; 23 | 24 | public void createRootNode() { 25 | boolean exists = zkClient.exists(znsServerConfiguration.getZkRoot()); 26 | if (!exists) { 27 | zkClient.createPersistent(znsServerConfiguration.getZkRoot()); 28 | } 29 | } 30 | 31 | public void createPersistentNode(String path) { 32 | String pathName = znsServerConfiguration.getZkRoot() + "/" + path; 33 | boolean exists = zkClient.exists(pathName); 34 | if (!exists) { 35 | zkClient.createPersistent(pathName); 36 | } 37 | } 38 | 39 | public void createNode(String path) { 40 | String pathName = znsServerConfiguration.getZkRoot() + "/" + path; 41 | boolean exists = zkClient.exists(pathName); 42 | if (!exists) { 43 | zkClient.createEphemeral(pathName); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /zns-server/target/maven-archiver/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven 2 | #Mon Feb 04 07:05:19 CST 2019 3 | version=1.0-SNAPSHOT 4 | groupId=com.buildupchao 5 | artifactId=zns-server 6 | -------------------------------------------------------------------------------- /zns-server/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/buildupchao/zns/3ea7b6ae0c90c7975d024d425e9cf6775e8c4e13/zns-server/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst -------------------------------------------------------------------------------- /zns-server/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/push/ServicePushManager.java 2 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/config/ZnsServerConfiguration.java 3 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/util/ZKit.java 4 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/acceptor/handler/ZnsRequestHandler.java 5 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/acceptor/ZnsServerAcceptor.java 6 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/core/impl/supports/ClassScannerSupport.java 7 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/acceptor/handler/ZnsServerDecodeHandler.java 8 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/ZnsServerRunner.java 9 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/core/impl/supports/AnnotationClassScannerSupport.java 10 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/acceptor/init/ZnsServerInitializer.java 11 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/config/BeanConfig.java 12 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/core/impl/DefaultClassScanner.java 13 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/util/ClassUtils.java 14 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/core/ClassScanner.java 15 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/runner/HeartBeatChecker.java 16 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/acceptor/handler/ZnsServerEncodeHandler.java 17 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/ZnsServerPackage.java 18 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/core/ClassHelper.java 19 | /Users/yachao/work/learn/git/zns/zns-server/src/main/java/com/buildupchao/zns/server/util/SpringBeanFactory.java 20 | -------------------------------------------------------------------------------- /zns-server/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/buildupchao/zns/3ea7b6ae0c90c7975d024d425e9cf6775e8c4e13/zns-server/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst -------------------------------------------------------------------------------- /zns-service-api/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /nbbuild/ 22 | /dist/ 23 | /nbdist/ 24 | /.nb-gradle/ 25 | /build/ 26 | -------------------------------------------------------------------------------- /zns-service-api/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/buildupchao/zns/3ea7b6ae0c90c7975d024d425e9cf6775e8c4e13/zns-service-api/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /zns-service-api/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /zns-service-api/mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Mingw, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | # TODO classpath? 118 | fi 119 | 120 | if [ -z "$JAVA_HOME" ]; then 121 | javaExecutable="`which javac`" 122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 123 | # readlink(1) is not available as standard on Solaris 10. 124 | readLink=`which readlink` 125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 126 | if $darwin ; then 127 | javaHome="`dirname \"$javaExecutable\"`" 128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 129 | else 130 | javaExecutable="`readlink -f \"$javaExecutable\"`" 131 | fi 132 | javaHome="`dirname \"$javaExecutable\"`" 133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 134 | JAVA_HOME="$javaHome" 135 | export JAVA_HOME 136 | fi 137 | fi 138 | fi 139 | 140 | if [ -z "$JAVACMD" ] ; then 141 | if [ -n "$JAVA_HOME" ] ; then 142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 143 | # IBM's JDK on AIX uses strange locations for the executables 144 | JAVACMD="$JAVA_HOME/jre/sh/java" 145 | else 146 | JAVACMD="$JAVA_HOME/bin/java" 147 | fi 148 | else 149 | JAVACMD="`which java`" 150 | fi 151 | fi 152 | 153 | if [ ! -x "$JAVACMD" ] ; then 154 | echo "Error: JAVA_HOME is not defined correctly." >&2 155 | echo " We cannot execute $JAVACMD" >&2 156 | exit 1 157 | fi 158 | 159 | if [ -z "$JAVA_HOME" ] ; then 160 | echo "Warning: JAVA_HOME environment variable is not set." 161 | fi 162 | 163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 164 | 165 | # traverses directory structure from process work directory to filesystem root 166 | # first directory with .mvn subdirectory is considered project base directory 167 | find_maven_basedir() { 168 | 169 | if [ -z "$1" ] 170 | then 171 | echo "Path not specified to find_maven_basedir" 172 | return 1 173 | fi 174 | 175 | basedir="$1" 176 | wdir="$1" 177 | while [ "$wdir" != '/' ] ; do 178 | if [ -d "$wdir"/.mvn ] ; then 179 | basedir=$wdir 180 | break 181 | fi 182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 183 | if [ -d "${wdir}" ]; then 184 | wdir=`cd "$wdir/.."; pwd` 185 | fi 186 | # end of workaround 187 | done 188 | echo "${basedir}" 189 | } 190 | 191 | # concatenates all lines of a file 192 | concat_lines() { 193 | if [ -f "$1" ]; then 194 | echo "$(tr -s '\n' ' ' < "$1")" 195 | fi 196 | } 197 | 198 | BASE_DIR=`find_maven_basedir "$(pwd)"` 199 | if [ -z "$BASE_DIR" ]; then 200 | exit 1; 201 | fi 202 | 203 | ########################################################################################## 204 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 205 | # This allows using the maven wrapper in projects that prohibit checking in binary data. 206 | ########################################################################################## 207 | if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then 208 | if [ "$MVNW_VERBOSE" = true ]; then 209 | echo "Found .mvn/wrapper/maven-wrapper.jar" 210 | fi 211 | else 212 | if [ "$MVNW_VERBOSE" = true ]; then 213 | echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." 214 | fi 215 | jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar" 216 | while IFS="=" read key value; do 217 | case "$key" in (wrapperUrl) jarUrl="$value"; break ;; 218 | esac 219 | done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" 220 | if [ "$MVNW_VERBOSE" = true ]; then 221 | echo "Downloading from: $jarUrl" 222 | fi 223 | wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" 224 | 225 | if command -v wget > /dev/null; then 226 | if [ "$MVNW_VERBOSE" = true ]; then 227 | echo "Found wget ... using wget" 228 | fi 229 | wget "$jarUrl" -O "$wrapperJarPath" 230 | elif command -v curl > /dev/null; then 231 | if [ "$MVNW_VERBOSE" = true ]; then 232 | echo "Found curl ... using curl" 233 | fi 234 | curl -o "$wrapperJarPath" "$jarUrl" 235 | else 236 | if [ "$MVNW_VERBOSE" = true ]; then 237 | echo "Falling back to using Java to download" 238 | fi 239 | javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" 240 | if [ -e "$javaClass" ]; then 241 | if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 242 | if [ "$MVNW_VERBOSE" = true ]; then 243 | echo " - Compiling MavenWrapperDownloader.java ..." 244 | fi 245 | # Compiling the Java class 246 | ("$JAVA_HOME/bin/javac" "$javaClass") 247 | fi 248 | if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 249 | # Running the downloader 250 | if [ "$MVNW_VERBOSE" = true ]; then 251 | echo " - Running MavenWrapperDownloader.java ..." 252 | fi 253 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") 254 | fi 255 | fi 256 | fi 257 | fi 258 | ########################################################################################## 259 | # End of extension 260 | ########################################################################################## 261 | 262 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 263 | if [ "$MVNW_VERBOSE" = true ]; then 264 | echo $MAVEN_PROJECTBASEDIR 265 | fi 266 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 267 | 268 | # For Cygwin, switch paths to Windows format before running java 269 | if $cygwin; then 270 | [ -n "$M2_HOME" ] && 271 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 272 | [ -n "$JAVA_HOME" ] && 273 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 274 | [ -n "$CLASSPATH" ] && 275 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 276 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 277 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 278 | fi 279 | 280 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 281 | 282 | exec "$JAVACMD" \ 283 | $MAVEN_OPTS \ 284 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 285 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 286 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 287 | -------------------------------------------------------------------------------- /zns-service-api/mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM set title of command window 39 | title %0 40 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 42 | 43 | @REM set %HOME% to equivalent of $HOME 44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 45 | 46 | @REM Execute a user defined script before this one 47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 49 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 50 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 51 | :skipRcPre 52 | 53 | @setlocal 54 | 55 | set ERROR_CODE=0 56 | 57 | @REM To isolate internal variables from possible post scripts, we use another setlocal 58 | @setlocal 59 | 60 | @REM ==== START VALIDATION ==== 61 | if not "%JAVA_HOME%" == "" goto OkJHome 62 | 63 | echo. 64 | echo Error: JAVA_HOME not found in your environment. >&2 65 | echo Please set the JAVA_HOME variable in your environment to match the >&2 66 | echo location of your Java installation. >&2 67 | echo. 68 | goto error 69 | 70 | :OkJHome 71 | if exist "%JAVA_HOME%\bin\java.exe" goto init 72 | 73 | echo. 74 | echo Error: JAVA_HOME is set to an invalid directory. >&2 75 | echo JAVA_HOME = "%JAVA_HOME%" >&2 76 | echo Please set the JAVA_HOME variable in your environment to match the >&2 77 | echo location of your Java installation. >&2 78 | echo. 79 | goto error 80 | 81 | @REM ==== END VALIDATION ==== 82 | 83 | :init 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 121 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 122 | 123 | set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar" 124 | FOR /F "tokens=1,2 delims==" %%A IN (%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties) DO ( 125 | IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B 126 | ) 127 | 128 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 129 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data. 130 | if exist %WRAPPER_JAR% ( 131 | echo Found %WRAPPER_JAR% 132 | ) else ( 133 | echo Couldn't find %WRAPPER_JAR%, downloading it ... 134 | echo Downloading from: %DOWNLOAD_URL% 135 | powershell -Command "(New-Object Net.WebClient).DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')" 136 | echo Finished downloading %WRAPPER_JAR% 137 | ) 138 | @REM End of extension 139 | 140 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 141 | if ERRORLEVEL 1 goto error 142 | goto end 143 | 144 | :error 145 | set ERROR_CODE=1 146 | 147 | :end 148 | @endlocal & set ERROR_CODE=%ERROR_CODE% 149 | 150 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 151 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 152 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 153 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 154 | :skipRcPost 155 | 156 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 157 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 158 | 159 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 160 | 161 | exit /B %ERROR_CODE% 162 | -------------------------------------------------------------------------------- /zns-service-api/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | com.buildupchao 6 | zns-service-api 7 | 0.0.1-SNAPSHOT 8 | zns-service-api 9 | Zns service api 10 | 11 | 12 | 1.8 13 | 14 | 15 | 16 | 17 | com.buildupchao 18 | zns-api 19 | 1.0-SNAPSHOT 20 | 21 | 22 | 23 | 24 | 25 | 26 | org.apache.maven.plugins 27 | maven-compiler-plugin 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /zns-service-api/src/main/java/com/buildupchao/zns/service/api/ChatService.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.service.api; 2 | 3 | import com.buildupchao.zns.api.annotation.ZnsClient; 4 | 5 | /** 6 | * @author buildupchao 7 | * @date 2019/2/1 15:59 8 | * @since JDK 1.8 9 | */ 10 | 11 | @ZnsClient 12 | public interface ChatService { 13 | 14 | String send(); 15 | 16 | String send(String userName, String message); 17 | 18 | String sendWithError(String message); 19 | } 20 | -------------------------------------------------------------------------------- /zns-service-api/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/buildupchao/zns/3ea7b6ae0c90c7975d024d425e9cf6775e8c4e13/zns-service-api/src/main/resources/application.properties -------------------------------------------------------------------------------- /zns-service-consumer/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /nbbuild/ 22 | /dist/ 23 | /nbdist/ 24 | /.nb-gradle/ 25 | /build/ 26 | -------------------------------------------------------------------------------- /zns-service-consumer/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/buildupchao/zns/3ea7b6ae0c90c7975d024d425e9cf6775e8c4e13/zns-service-consumer/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /zns-service-consumer/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /zns-service-consumer/mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Mingw, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | # TODO classpath? 118 | fi 119 | 120 | if [ -z "$JAVA_HOME" ]; then 121 | javaExecutable="`which javac`" 122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 123 | # readlink(1) is not available as standard on Solaris 10. 124 | readLink=`which readlink` 125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 126 | if $darwin ; then 127 | javaHome="`dirname \"$javaExecutable\"`" 128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 129 | else 130 | javaExecutable="`readlink -f \"$javaExecutable\"`" 131 | fi 132 | javaHome="`dirname \"$javaExecutable\"`" 133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 134 | JAVA_HOME="$javaHome" 135 | export JAVA_HOME 136 | fi 137 | fi 138 | fi 139 | 140 | if [ -z "$JAVACMD" ] ; then 141 | if [ -n "$JAVA_HOME" ] ; then 142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 143 | # IBM's JDK on AIX uses strange locations for the executables 144 | JAVACMD="$JAVA_HOME/jre/sh/java" 145 | else 146 | JAVACMD="$JAVA_HOME/bin/java" 147 | fi 148 | else 149 | JAVACMD="`which java`" 150 | fi 151 | fi 152 | 153 | if [ ! -x "$JAVACMD" ] ; then 154 | echo "Error: JAVA_HOME is not defined correctly." >&2 155 | echo " We cannot execute $JAVACMD" >&2 156 | exit 1 157 | fi 158 | 159 | if [ -z "$JAVA_HOME" ] ; then 160 | echo "Warning: JAVA_HOME environment variable is not set." 161 | fi 162 | 163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 164 | 165 | # traverses directory structure from process work directory to filesystem root 166 | # first directory with .mvn subdirectory is considered project base directory 167 | find_maven_basedir() { 168 | 169 | if [ -z "$1" ] 170 | then 171 | echo "Path not specified to find_maven_basedir" 172 | return 1 173 | fi 174 | 175 | basedir="$1" 176 | wdir="$1" 177 | while [ "$wdir" != '/' ] ; do 178 | if [ -d "$wdir"/.mvn ] ; then 179 | basedir=$wdir 180 | break 181 | fi 182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 183 | if [ -d "${wdir}" ]; then 184 | wdir=`cd "$wdir/.."; pwd` 185 | fi 186 | # end of workaround 187 | done 188 | echo "${basedir}" 189 | } 190 | 191 | # concatenates all lines of a file 192 | concat_lines() { 193 | if [ -f "$1" ]; then 194 | echo "$(tr -s '\n' ' ' < "$1")" 195 | fi 196 | } 197 | 198 | BASE_DIR=`find_maven_basedir "$(pwd)"` 199 | if [ -z "$BASE_DIR" ]; then 200 | exit 1; 201 | fi 202 | 203 | ########################################################################################## 204 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 205 | # This allows using the maven wrapper in projects that prohibit checking in binary data. 206 | ########################################################################################## 207 | if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then 208 | if [ "$MVNW_VERBOSE" = true ]; then 209 | echo "Found .mvn/wrapper/maven-wrapper.jar" 210 | fi 211 | else 212 | if [ "$MVNW_VERBOSE" = true ]; then 213 | echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." 214 | fi 215 | jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar" 216 | while IFS="=" read key value; do 217 | case "$key" in (wrapperUrl) jarUrl="$value"; break ;; 218 | esac 219 | done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" 220 | if [ "$MVNW_VERBOSE" = true ]; then 221 | echo "Downloading from: $jarUrl" 222 | fi 223 | wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" 224 | 225 | if command -v wget > /dev/null; then 226 | if [ "$MVNW_VERBOSE" = true ]; then 227 | echo "Found wget ... using wget" 228 | fi 229 | wget "$jarUrl" -O "$wrapperJarPath" 230 | elif command -v curl > /dev/null; then 231 | if [ "$MVNW_VERBOSE" = true ]; then 232 | echo "Found curl ... using curl" 233 | fi 234 | curl -o "$wrapperJarPath" "$jarUrl" 235 | else 236 | if [ "$MVNW_VERBOSE" = true ]; then 237 | echo "Falling back to using Java to download" 238 | fi 239 | javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" 240 | if [ -e "$javaClass" ]; then 241 | if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 242 | if [ "$MVNW_VERBOSE" = true ]; then 243 | echo " - Compiling MavenWrapperDownloader.java ..." 244 | fi 245 | # Compiling the Java class 246 | ("$JAVA_HOME/bin/javac" "$javaClass") 247 | fi 248 | if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 249 | # Running the downloader 250 | if [ "$MVNW_VERBOSE" = true ]; then 251 | echo " - Running MavenWrapperDownloader.java ..." 252 | fi 253 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") 254 | fi 255 | fi 256 | fi 257 | fi 258 | ########################################################################################## 259 | # End of extension 260 | ########################################################################################## 261 | 262 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 263 | if [ "$MVNW_VERBOSE" = true ]; then 264 | echo $MAVEN_PROJECTBASEDIR 265 | fi 266 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 267 | 268 | # For Cygwin, switch paths to Windows format before running java 269 | if $cygwin; then 270 | [ -n "$M2_HOME" ] && 271 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 272 | [ -n "$JAVA_HOME" ] && 273 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 274 | [ -n "$CLASSPATH" ] && 275 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 276 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 277 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 278 | fi 279 | 280 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 281 | 282 | exec "$JAVACMD" \ 283 | $MAVEN_OPTS \ 284 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 285 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 286 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 287 | -------------------------------------------------------------------------------- /zns-service-consumer/mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM set title of command window 39 | title %0 40 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 42 | 43 | @REM set %HOME% to equivalent of $HOME 44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 45 | 46 | @REM Execute a user defined script before this one 47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 49 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 50 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 51 | :skipRcPre 52 | 53 | @setlocal 54 | 55 | set ERROR_CODE=0 56 | 57 | @REM To isolate internal variables from possible post scripts, we use another setlocal 58 | @setlocal 59 | 60 | @REM ==== START VALIDATION ==== 61 | if not "%JAVA_HOME%" == "" goto OkJHome 62 | 63 | echo. 64 | echo Error: JAVA_HOME not found in your environment. >&2 65 | echo Please set the JAVA_HOME variable in your environment to match the >&2 66 | echo location of your Java installation. >&2 67 | echo. 68 | goto error 69 | 70 | :OkJHome 71 | if exist "%JAVA_HOME%\bin\java.exe" goto init 72 | 73 | echo. 74 | echo Error: JAVA_HOME is set to an invalid directory. >&2 75 | echo JAVA_HOME = "%JAVA_HOME%" >&2 76 | echo Please set the JAVA_HOME variable in your environment to match the >&2 77 | echo location of your Java installation. >&2 78 | echo. 79 | goto error 80 | 81 | @REM ==== END VALIDATION ==== 82 | 83 | :init 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 121 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 122 | 123 | set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar" 124 | FOR /F "tokens=1,2 delims==" %%A IN (%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties) DO ( 125 | IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B 126 | ) 127 | 128 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 129 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data. 130 | if exist %WRAPPER_JAR% ( 131 | echo Found %WRAPPER_JAR% 132 | ) else ( 133 | echo Couldn't find %WRAPPER_JAR%, downloading it ... 134 | echo Downloading from: %DOWNLOAD_URL% 135 | powershell -Command "(New-Object Net.WebClient).DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')" 136 | echo Finished downloading %WRAPPER_JAR% 137 | ) 138 | @REM End of extension 139 | 140 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 141 | if ERRORLEVEL 1 goto error 142 | goto end 143 | 144 | :error 145 | set ERROR_CODE=1 146 | 147 | :end 148 | @endlocal & set ERROR_CODE=%ERROR_CODE% 149 | 150 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 151 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 152 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 153 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 154 | :skipRcPost 155 | 156 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 157 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 158 | 159 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 160 | 161 | exit /B %ERROR_CODE% 162 | -------------------------------------------------------------------------------- /zns-service-consumer/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.springframework.boot 8 | spring-boot-starter-parent 9 | 2.1.2.RELEASE 10 | 11 | 12 | com.buildupchao 13 | zns-service-consumer 14 | 0.0.1-SNAPSHOT 15 | zns-service-consumer 16 | zns client endpoint 17 | 18 | 19 | 1.8 20 | 21 | 22 | 23 | 24 | com.buildupchao 25 | zns-service-api 26 | 0.0.1-SNAPSHOT 27 | 28 | 29 | com.buildupchao 30 | zns-client 31 | 1.0-SNAPSHOT 32 | 33 | 34 | org.springframework 35 | spring-context 36 | 37 | 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-web 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-test 48 | test 49 | 50 | 51 | 52 | org.slf4j 53 | slf4j-log4j12 54 | 55 | 56 | 57 | 58 | 59 | 60 | org.springframework.boot 61 | spring-boot-maven-plugin 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /zns-service-consumer/src/main/java/com/buildupchao/zns/service/consumer/ZnsServiceConsumerApplication.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.service.consumer; 2 | 3 | import com.buildupchao.zns.client.ZnsClientPackage; 4 | import com.buildupchao.zns.client.ZnsClientRunner; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.ApplicationArguments; 7 | import org.springframework.boot.ApplicationRunner; 8 | import org.springframework.boot.SpringApplication; 9 | import org.springframework.boot.autoconfigure.SpringBootApplication; 10 | import org.springframework.context.annotation.ComponentScan; 11 | 12 | @ComponentScan( 13 | basePackages = "com.buildupchao.zns.service.consumer", 14 | basePackageClasses = ZnsClientPackage.class 15 | ) 16 | @SpringBootApplication 17 | public class ZnsServiceConsumerApplication implements ApplicationRunner { 18 | 19 | @Autowired 20 | private ZnsClientRunner znsClientRunner; 21 | 22 | public static void main(String[] args) { 23 | SpringApplication.run(ZnsServiceConsumerApplication.class, args); 24 | } 25 | 26 | @Override 27 | public void run(ApplicationArguments applicationArguments) throws Exception { 28 | znsClientRunner.run(); 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /zns-service-consumer/src/main/java/com/buildupchao/zns/service/consumer/controller/TestController.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.service.consumer.controller; 2 | 3 | import com.buildupchao.zns.client.util.SpringBeanFactory; 4 | import com.buildupchao.zns.service.api.ChatService; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | * @author buildupchao 11 | * @date 2019/2/1 15:13 12 | * @since JDK 1.8 13 | */ 14 | @RestController 15 | @RequestMapping("/test") 16 | public class TestController { 17 | 18 | @GetMapping("/testservice") 19 | public void test() { 20 | ChatService chatService = SpringBeanFactory.getBean(ChatService.class); 21 | 22 | System.out.println(chatService.send()); 23 | 24 | System.out.println(chatService.send("buildupchao", "Happy Spring Festival!")); 25 | 26 | try { 27 | chatService.sendWithError("No message"); 28 | } catch (Exception ex) { 29 | ex.printStackTrace(); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /zns-service-consumer/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | 2 | logging: 3 | config: classpath:logback-spring.xml 4 | server: 5 | port: 8081 6 | spring: 7 | application: 8 | name: zns-service-consumer 9 | 10 | zns: 11 | client: 12 | zk: 13 | root: /zns 14 | addr: localhost:2181 15 | switch: true 16 | api: 17 | package: com.buildupchao.zns.service.api 18 | cluster: 19 | strategy: Polling -------------------------------------------------------------------------------- /zns-service-consumer/src/main/resources/logback-spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | zns-service-consumer 8 | 9 | 10 | 11 | ${LOG_LEVEL} 12 | 13 | 14 | %d{yyyy-MM-dd HH:mm:ss} %contextName [%thread] %-5level %logger{35} - %msg %n 15 | 16 | 17 | 18 | 19 | ${LOG_LEVEL} 20 | 21 | ${LOG_PATH}/sys-running.log 22 | 23 | ${LOG_PATH}/sys-running.log.%d{yyyy-MM-dd}.%i.gz 24 | 25 | 26 | 100MB 27 | 28 | 30 29 | 3GB 30 | 31 | 32 | %d{yyyy-MM-dd HH:mm:ss} %contextName [%thread] %-5level %logger{35} - %msg%n 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /zns-service-consumer/src/test/java/com/buildupchao/zns/service/consumer/ZnsServiceConsumerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.service.consumer; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class ZnsServiceConsumerApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | 18 | -------------------------------------------------------------------------------- /zns-service-provider/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /nbbuild/ 22 | /dist/ 23 | /nbdist/ 24 | /.nb-gradle/ 25 | /build/ 26 | -------------------------------------------------------------------------------- /zns-service-provider/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/buildupchao/zns/3ea7b6ae0c90c7975d024d425e9cf6775e8c4e13/zns-service-provider/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /zns-service-provider/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /zns-service-provider/mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Mingw, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | # TODO classpath? 118 | fi 119 | 120 | if [ -z "$JAVA_HOME" ]; then 121 | javaExecutable="`which javac`" 122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 123 | # readlink(1) is not available as standard on Solaris 10. 124 | readLink=`which readlink` 125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 126 | if $darwin ; then 127 | javaHome="`dirname \"$javaExecutable\"`" 128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 129 | else 130 | javaExecutable="`readlink -f \"$javaExecutable\"`" 131 | fi 132 | javaHome="`dirname \"$javaExecutable\"`" 133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 134 | JAVA_HOME="$javaHome" 135 | export JAVA_HOME 136 | fi 137 | fi 138 | fi 139 | 140 | if [ -z "$JAVACMD" ] ; then 141 | if [ -n "$JAVA_HOME" ] ; then 142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 143 | # IBM's JDK on AIX uses strange locations for the executables 144 | JAVACMD="$JAVA_HOME/jre/sh/java" 145 | else 146 | JAVACMD="$JAVA_HOME/bin/java" 147 | fi 148 | else 149 | JAVACMD="`which java`" 150 | fi 151 | fi 152 | 153 | if [ ! -x "$JAVACMD" ] ; then 154 | echo "Error: JAVA_HOME is not defined correctly." >&2 155 | echo " We cannot execute $JAVACMD" >&2 156 | exit 1 157 | fi 158 | 159 | if [ -z "$JAVA_HOME" ] ; then 160 | echo "Warning: JAVA_HOME environment variable is not set." 161 | fi 162 | 163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 164 | 165 | # traverses directory structure from process work directory to filesystem root 166 | # first directory with .mvn subdirectory is considered project base directory 167 | find_maven_basedir() { 168 | 169 | if [ -z "$1" ] 170 | then 171 | echo "Path not specified to find_maven_basedir" 172 | return 1 173 | fi 174 | 175 | basedir="$1" 176 | wdir="$1" 177 | while [ "$wdir" != '/' ] ; do 178 | if [ -d "$wdir"/.mvn ] ; then 179 | basedir=$wdir 180 | break 181 | fi 182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 183 | if [ -d "${wdir}" ]; then 184 | wdir=`cd "$wdir/.."; pwd` 185 | fi 186 | # end of workaround 187 | done 188 | echo "${basedir}" 189 | } 190 | 191 | # concatenates all lines of a file 192 | concat_lines() { 193 | if [ -f "$1" ]; then 194 | echo "$(tr -s '\n' ' ' < "$1")" 195 | fi 196 | } 197 | 198 | BASE_DIR=`find_maven_basedir "$(pwd)"` 199 | if [ -z "$BASE_DIR" ]; then 200 | exit 1; 201 | fi 202 | 203 | ########################################################################################## 204 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 205 | # This allows using the maven wrapper in projects that prohibit checking in binary data. 206 | ########################################################################################## 207 | if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then 208 | if [ "$MVNW_VERBOSE" = true ]; then 209 | echo "Found .mvn/wrapper/maven-wrapper.jar" 210 | fi 211 | else 212 | if [ "$MVNW_VERBOSE" = true ]; then 213 | echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." 214 | fi 215 | jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar" 216 | while IFS="=" read key value; do 217 | case "$key" in (wrapperUrl) jarUrl="$value"; break ;; 218 | esac 219 | done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" 220 | if [ "$MVNW_VERBOSE" = true ]; then 221 | echo "Downloading from: $jarUrl" 222 | fi 223 | wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" 224 | 225 | if command -v wget > /dev/null; then 226 | if [ "$MVNW_VERBOSE" = true ]; then 227 | echo "Found wget ... using wget" 228 | fi 229 | wget "$jarUrl" -O "$wrapperJarPath" 230 | elif command -v curl > /dev/null; then 231 | if [ "$MVNW_VERBOSE" = true ]; then 232 | echo "Found curl ... using curl" 233 | fi 234 | curl -o "$wrapperJarPath" "$jarUrl" 235 | else 236 | if [ "$MVNW_VERBOSE" = true ]; then 237 | echo "Falling back to using Java to download" 238 | fi 239 | javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" 240 | if [ -e "$javaClass" ]; then 241 | if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 242 | if [ "$MVNW_VERBOSE" = true ]; then 243 | echo " - Compiling MavenWrapperDownloader.java ..." 244 | fi 245 | # Compiling the Java class 246 | ("$JAVA_HOME/bin/javac" "$javaClass") 247 | fi 248 | if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 249 | # Running the downloader 250 | if [ "$MVNW_VERBOSE" = true ]; then 251 | echo " - Running MavenWrapperDownloader.java ..." 252 | fi 253 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") 254 | fi 255 | fi 256 | fi 257 | fi 258 | ########################################################################################## 259 | # End of extension 260 | ########################################################################################## 261 | 262 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 263 | if [ "$MVNW_VERBOSE" = true ]; then 264 | echo $MAVEN_PROJECTBASEDIR 265 | fi 266 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 267 | 268 | # For Cygwin, switch paths to Windows format before running java 269 | if $cygwin; then 270 | [ -n "$M2_HOME" ] && 271 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 272 | [ -n "$JAVA_HOME" ] && 273 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 274 | [ -n "$CLASSPATH" ] && 275 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 276 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 277 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 278 | fi 279 | 280 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 281 | 282 | exec "$JAVACMD" \ 283 | $MAVEN_OPTS \ 284 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 285 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 286 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 287 | -------------------------------------------------------------------------------- /zns-service-provider/mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM set title of command window 39 | title %0 40 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 42 | 43 | @REM set %HOME% to equivalent of $HOME 44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 45 | 46 | @REM Execute a user defined script before this one 47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 49 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 50 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 51 | :skipRcPre 52 | 53 | @setlocal 54 | 55 | set ERROR_CODE=0 56 | 57 | @REM To isolate internal variables from possible post scripts, we use another setlocal 58 | @setlocal 59 | 60 | @REM ==== START VALIDATION ==== 61 | if not "%JAVA_HOME%" == "" goto OkJHome 62 | 63 | echo. 64 | echo Error: JAVA_HOME not found in your environment. >&2 65 | echo Please set the JAVA_HOME variable in your environment to match the >&2 66 | echo location of your Java installation. >&2 67 | echo. 68 | goto error 69 | 70 | :OkJHome 71 | if exist "%JAVA_HOME%\bin\java.exe" goto init 72 | 73 | echo. 74 | echo Error: JAVA_HOME is set to an invalid directory. >&2 75 | echo JAVA_HOME = "%JAVA_HOME%" >&2 76 | echo Please set the JAVA_HOME variable in your environment to match the >&2 77 | echo location of your Java installation. >&2 78 | echo. 79 | goto error 80 | 81 | @REM ==== END VALIDATION ==== 82 | 83 | :init 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 121 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 122 | 123 | set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar" 124 | FOR /F "tokens=1,2 delims==" %%A IN (%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties) DO ( 125 | IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B 126 | ) 127 | 128 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 129 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data. 130 | if exist %WRAPPER_JAR% ( 131 | echo Found %WRAPPER_JAR% 132 | ) else ( 133 | echo Couldn't find %WRAPPER_JAR%, downloading it ... 134 | echo Downloading from: %DOWNLOAD_URL% 135 | powershell -Command "(New-Object Net.WebClient).DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')" 136 | echo Finished downloading %WRAPPER_JAR% 137 | ) 138 | @REM End of extension 139 | 140 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 141 | if ERRORLEVEL 1 goto error 142 | goto end 143 | 144 | :error 145 | set ERROR_CODE=1 146 | 147 | :end 148 | @endlocal & set ERROR_CODE=%ERROR_CODE% 149 | 150 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 151 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 152 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 153 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 154 | :skipRcPost 155 | 156 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 157 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 158 | 159 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 160 | 161 | exit /B %ERROR_CODE% 162 | -------------------------------------------------------------------------------- /zns-service-provider/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.springframework.boot 8 | spring-boot-starter-parent 9 | 2.1.2.RELEASE 10 | 11 | 12 | com.buildupchao 13 | zns-service-provider 14 | 0.0.1-SNAPSHOT 15 | zns-service-provider 16 | zns server endpoint 17 | 18 | 19 | 1.8 20 | 21 | 22 | 23 | 24 | com.buildupchao 25 | zns-service-api 26 | 0.0.1-SNAPSHOT 27 | 28 | 29 | com.buildupchao 30 | zns-server 31 | 1.0-SNAPSHOT 32 | 33 | 34 | org.springframework 35 | spring-context 36 | 37 | 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-web 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-test 48 | test 49 | 50 | 51 | 52 | org.slf4j 53 | slf4j-log4j12 54 | 55 | 56 | 57 | 58 | 59 | 60 | org.springframework.boot 61 | spring-boot-maven-plugin 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /zns-service-provider/src/main/java/com/buildupchao/zns/service/provider/ZnsServiceProviderApplication.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.service.provider; 2 | 3 | import com.buildupchao.zns.server.ZnsServerPackage; 4 | import com.buildupchao.zns.server.ZnsServerRunner; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.ApplicationArguments; 9 | import org.springframework.boot.ApplicationRunner; 10 | import org.springframework.boot.SpringApplication; 11 | import org.springframework.boot.autoconfigure.SpringBootApplication; 12 | import org.springframework.context.annotation.ComponentScan; 13 | 14 | @ComponentScan( 15 | basePackages = "com.buildupchao.zns.service.provider", 16 | basePackageClasses = ZnsServerPackage.class 17 | ) 18 | @SpringBootApplication 19 | public class ZnsServiceProviderApplication implements ApplicationRunner { 20 | 21 | private static final Logger LOGGER = LoggerFactory.getLogger(ZnsServiceProviderApplication.class); 22 | 23 | @Autowired 24 | private ZnsServerRunner znsServerRunner; 25 | 26 | public static void main(String[] args) { 27 | SpringApplication.run(ZnsServiceProviderApplication.class, args); 28 | 29 | LOGGER.info("Zns service provider application startup successfully"); 30 | 31 | } 32 | 33 | @Override 34 | public void run(ApplicationArguments applicationArguments) throws Exception { 35 | znsServerRunner.run(); 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /zns-service-provider/src/main/java/com/buildupchao/zns/service/provider/service/ChatServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.service.provider.service; 2 | 3 | import com.buildupchao.zns.api.annotation.ZnsService; 4 | import com.buildupchao.zns.common.exception.ZnsException; 5 | import com.buildupchao.zns.service.api.ChatService; 6 | 7 | /** 8 | * @author buildupchao 9 | * @date 2019/2/1 11:33 10 | * @since JDK 1.8 11 | */ 12 | @ZnsService(cls = ChatService.class) 13 | public class ChatServiceImpl implements ChatService { 14 | 15 | @Override 16 | public String send() { 17 | return "Nobody send message!"; 18 | } 19 | 20 | @Override 21 | public String send(String userName, String message) { 22 | return String.format("【%s】:%s", userName, message); 23 | } 24 | 25 | @Override 26 | public String sendWithError(String message) { 27 | throw new ZnsException("test error!" + message); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /zns-service-provider/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | 2 | logging: 3 | config: classpath:logback-spring.xml 4 | server: 5 | port: 8082 6 | spring: 7 | application: 8 | name: zns-service-provider 9 | 10 | zns: 11 | server: 12 | zk: 13 | root: /zns 14 | addr: localhost:2181 15 | switch: true 16 | network: 17 | port: 8888 -------------------------------------------------------------------------------- /zns-service-provider/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=info, systemOut 2 | 3 | log4j.appender.systemOut=org.apache.log4j.ConsoleAppender 4 | log4j.appender.systemOut.layout=org.apache.log4j.PatternLayout 5 | 6 | # Pattern to output the caller's file name and line number. 7 | log4j.appender.systemOut.layout.ConversionPattern= %d %5p [%t] (%F:%L) - %m%n -------------------------------------------------------------------------------- /zns-service-provider/src/main/resources/logback-spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | zns-service-provider 8 | 9 | 10 | 11 | ${LOG_LEVEL} 12 | 13 | 14 | %d{yyyy-MM-dd HH:mm:ss} %contextName [%thread] %-5level %logger{35} - %msg %n 15 | 16 | 17 | 18 | 19 | ${LOG_LEVEL} 20 | 21 | ${LOG_PATH}/sys-running.log 22 | 23 | ${LOG_PATH}/sys-running.log.%d{yyyy-MM-dd}.%i.gz 24 | 25 | 26 | 100MB 27 | 28 | 30 29 | 3GB 30 | 31 | 32 | %d{yyyy-MM-dd HH:mm:ss} %contextName [%thread] %-5level %logger{35} - %msg%n 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /zns-service-provider/src/test/java/com/buildupchao/zns/service/provider/ZnsServiceProviderApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.service.provider; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class ZnsServiceProviderApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | 18 | -------------------------------------------------------------------------------- /zns-zk/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | zns 7 | com.buildupchao 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | com.buildupchao 12 | zns-zk 13 | zns registry center 14 | 15 | 16 | 17 | com.buildupchao 18 | zns-common 19 | 1.0-SNAPSHOT 20 | 21 | 22 | com.github.sgroschupf 23 | zkclient 24 | 25 | 26 | org.springframework 27 | spring-context 28 | 29 | 30 | -------------------------------------------------------------------------------- /zns-zk/src/main/java/com/buildupchao/zns/zk/ZnsZKApplication.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.zk; 2 | 3 | /** 4 | * @author buildupchao 5 | * @date 2019/2/17 17:12 6 | * @since JDK 1.8 7 | */ 8 | public class ZnsZKApplication { 9 | } 10 | -------------------------------------------------------------------------------- /zns-zk/src/main/java/com/buildupchao/zns/zk/bean/ConsumerService.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.zk.bean; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Builder; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | import java.io.Serializable; 9 | 10 | /** 11 | * @author buildupchao 12 | * @date 2019/2/17 17:15 13 | * @since JDK 1.8 14 | */ 15 | @Data 16 | @Builder 17 | @NoArgsConstructor 18 | @AllArgsConstructor 19 | public class ConsumerService implements Serializable { 20 | 21 | private String consumerIp; 22 | private int consumerPort; 23 | private long timeout; 24 | 25 | private String appKey; 26 | private String groupName = "default"; 27 | } 28 | -------------------------------------------------------------------------------- /zns-zk/src/main/java/com/buildupchao/zns/zk/bean/ProviderService.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.zk.bean; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Builder; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | import org.apache.commons.beanutils.BeanUtils; 8 | 9 | import java.io.Serializable; 10 | 11 | /** 12 | * @author buildupchao 13 | * @date 2019/2/1 02:52 14 | * @since JDK 1.8 15 | */ 16 | @Data 17 | @Builder 18 | @NoArgsConstructor 19 | @AllArgsConstructor 20 | public class ProviderService implements Serializable { 21 | private String serverIp; 22 | private int serverPort; 23 | private int networkPort; 24 | 25 | private long timeout; 26 | // the weight of service provider 27 | private int weight; 28 | 29 | private int workerThreads; 30 | private String appKey; 31 | private String groupName; 32 | 33 | public ProviderService newBean() { 34 | ProviderService target = new ProviderService(); 35 | target.setServerIp(serverIp); 36 | target.setServerPort(serverPort); 37 | target.setNetworkPort(networkPort); 38 | target.setTimeout(timeout); 39 | target.setWeight(weight); 40 | target.setWorkerThreads(workerThreads); 41 | target.setAppKey(appKey); 42 | target.setGroupName(groupName); 43 | return target; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /zns-zk/src/main/java/com/buildupchao/zns/zk/config/BeanConfig.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.zk.config; 2 | 3 | import org.I0Itec.zkclient.ZkClient; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | /** 9 | * @author buildupchao 10 | * @date 2019/2/17 17:40 11 | * @since JDK 1.8 12 | */ 13 | @Configuration 14 | public class BeanConfig { 15 | 16 | @Autowired 17 | private ZnsZkConfiguration configuration; 18 | 19 | @Bean 20 | public ZkClient zkClient() { 21 | return new ZkClient(configuration.getZkAddr(), configuration.getZkConnectTimeout()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /zns-zk/src/main/java/com/buildupchao/zns/zk/config/ZnsZkConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.zk.config; 2 | 3 | import lombok.Data; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.stereotype.Component; 6 | 7 | /** 8 | * @author buildupchao 9 | * @date 2019/2/17 17:38 10 | * @since JDK 1.8 11 | */ 12 | @Data 13 | @Component 14 | public class ZnsZkConfiguration { 15 | 16 | @Value("${zns.zk.root}") 17 | private String zkRoot; 18 | 19 | @Value("${zns.zk.addr}") 20 | private String zkAddr; 21 | 22 | @Value("${zns.zk.switch}") 23 | private boolean zkSwitch; 24 | 25 | @Value("${zns.zk.connect.timeout:5000}") 26 | private int zkConnectTimeout; 27 | } 28 | -------------------------------------------------------------------------------- /zns-zk/src/main/java/com/buildupchao/zns/zk/registry/IRegisterCenter2Provider.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.zk.registry; 2 | 3 | import com.buildupchao.zns.zk.bean.ProviderService; 4 | 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | /** 9 | * @author buildupchao 10 | * @date 2019/2/17 17:13 11 | * @since JDK 1.8 12 | */ 13 | public interface IRegisterCenter2Provider { 14 | 15 | void registerProvider(final List providerServices); 16 | 17 | Map> getProviderServiceMap(); 18 | } 19 | -------------------------------------------------------------------------------- /zns-zk/src/main/java/com/buildupchao/zns/zk/registry/RegisterCenter.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.zk.registry; 2 | 3 | import com.buildupchao.zns.zk.bean.ProviderService; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | import java.util.List; 8 | import java.util.Map; 9 | import java.util.concurrent.ConcurrentHashMap; 10 | 11 | /** 12 | * @author buildupchao 13 | * @date 2019/2/17 17:32 14 | * @since JDK 1.8 15 | */ 16 | public class RegisterCenter implements IRegisterCenter2Provider { 17 | 18 | private static final Logger LOGGER = LoggerFactory.getLogger(RegisterCenter.class); 19 | 20 | private static RegisterCenter registerCenter = new RegisterCenter(); 21 | 22 | private static final String PROVIDER_TYPE = "provider"; 23 | private static final String CONSUMER_TYPE = "consumer"; 24 | private static final ConcurrentHashMap> providerServiceMap = new ConcurrentHashMap<>(); 25 | private static final ConcurrentHashMap> serviceMetaDataMap4Consumer = new ConcurrentHashMap<>(); 26 | 27 | private RegisterCenter() {} 28 | 29 | public void initRegisterCenter() { 30 | 31 | } 32 | 33 | @Override 34 | public void registerProvider(List providerServices) { 35 | 36 | } 37 | 38 | @Override 39 | public Map> getProviderServiceMap() { 40 | return null; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /zns-zk/src/main/java/com/buildupchao/zns/zk/util/SpringBeanFactory.java: -------------------------------------------------------------------------------- 1 | package com.buildupchao.zns.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 | /** 9 | * @author buildupchao 10 | * @date 2019/2/17 17:42 11 | * @since JDK 1.8 12 | */ 13 | @Component 14 | public class SpringBeanFactory implements ApplicationContextAware { 15 | 16 | private static ApplicationContext context; 17 | 18 | public static T getBean(Class cls) { 19 | return context.getBean(cls); 20 | } 21 | 22 | @Override 23 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 24 | context = applicationContext; 25 | } 26 | } 27 | --------------------------------------------------------------------------------