├── .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 | 
32 |
33 | 
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