├── .idea
├── compiler.xml
├── libraries
│ ├── Maven__aopalliance_aopalliance_1_0.xml
│ ├── Maven__com_dyuproject_protostuff_protostuff_api_1_0_10.xml
│ ├── Maven__com_dyuproject_protostuff_protostuff_collectionschema_1_0_10.xml
│ ├── Maven__com_dyuproject_protostuff_protostuff_core_1_0_10.xml
│ ├── Maven__com_dyuproject_protostuff_protostuff_runtime_1_0_10.xml
│ ├── Maven__com_github_sgroschupf_zkclient_0_1.xml
│ ├── Maven__commons_logging_commons_logging_1_2.xml
│ ├── Maven__io_netty_netty_3_7_0_Final.xml
│ ├── Maven__io_netty_netty_all_4_1_14_Final.xml
│ ├── Maven__jline_jline_0_9_94.xml
│ ├── Maven__junit_junit_4_12.xml
│ ├── Maven__log4j_log4j_1_2_16.xml
│ ├── Maven__org_apache_zookeeper_zookeeper_3_4_6.xml
│ ├── Maven__org_hamcrest_hamcrest_core_1_3.xml
│ ├── Maven__org_slf4j_slf4j_api_1_6_1.xml
│ ├── Maven__org_slf4j_slf4j_log4j12_1_6_1.xml
│ ├── Maven__org_springframework_spring_aop_4_1_7_RELEASE.xml
│ ├── Maven__org_springframework_spring_beans_4_1_7_RELEASE.xml
│ ├── Maven__org_springframework_spring_context_4_1_7_RELEASE.xml
│ ├── Maven__org_springframework_spring_core_4_1_7_RELEASE.xml
│ ├── Maven__org_springframework_spring_expression_4_1_7_RELEASE.xml
│ └── Maven__org_springframework_spring_test_4_1_7_RELEASE.xml
├── misc.xml
├── modules.xml
├── uiDesigner.xml
└── vcs.xml
├── README.md
├── my-rpc.iml
├── pom.xml
├── rpc-consumer
├── pom.xml
├── rpc-consumer.iml
└── src
│ └── main
│ ├── java
│ └── top
│ │ └── yuyufeng
│ │ └── rpc
│ │ └── test
│ │ ├── ClientApp.java
│ │ └── ClientAppSpring.java
│ └── resources
│ └── my-rpc-consumer.xml
├── rpc-core
├── pom.xml
├── rpc-core.iml
└── src
│ └── main
│ └── java
│ └── top
│ └── yuyufeng
│ └── rpc
│ ├── MyDecoder.java
│ ├── MyEncoder.java
│ ├── RpcRequest.java
│ ├── RpcResponse.java
│ ├── client
│ ├── ClientCluster.java
│ ├── DiscoverService.java
│ ├── MyClientHandler.java
│ ├── MyNettyClient.java
│ ├── proxy
│ │ ├── ProxyHandler.java
│ │ └── RemoteServiceImpl.java
│ └── spring
│ │ └── MyRpcReference.java
│ ├── exception
│ └── RpcException.java
│ ├── server
│ ├── MyServer.java
│ ├── MyServerHandler.java
│ ├── RegisterServicesCenter.java
│ ├── RpcServer.java
│ ├── RpcServerImpl.java
│ └── spring
│ │ ├── MyRpcConfig.java
│ │ └── RpcServerFactory.java
│ └── utils
│ └── ProtostuffUtil.java
├── rpc-provider
├── pom.xml
├── rpc-provider.iml
└── src
│ └── main
│ ├── java
│ └── top
│ │ └── yuyufeng
│ │ └── rpc
│ │ ├── ServerApp.java
│ │ ├── ServerAppBySpring.java
│ │ └── service
│ │ └── impl
│ │ ├── CalServiceImpl.java
│ │ └── HelloServiceImpl.java
│ └── resources
│ └── my-rpc-provider.xml
└── rpc-service
├── pom.xml
├── rpc-service.iml
└── src
└── main
└── java
└── top
└── yuyufeng
└── rpc
└── service
├── CalService.java
├── HelloService.java
└── top
└── yuyufeng
└── dto
└── MyResult.java
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__aopalliance_aopalliance_1_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__com_dyuproject_protostuff_protostuff_api_1_0_10.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__com_dyuproject_protostuff_protostuff_collectionschema_1_0_10.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__com_dyuproject_protostuff_protostuff_core_1_0_10.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__com_dyuproject_protostuff_protostuff_runtime_1_0_10.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__com_github_sgroschupf_zkclient_0_1.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__commons_logging_commons_logging_1_2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__io_netty_netty_3_7_0_Final.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__io_netty_netty_all_4_1_14_Final.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__jline_jline_0_9_94.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__junit_junit_4_12.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__log4j_log4j_1_2_16.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_apache_zookeeper_zookeeper_3_4_6.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_slf4j_slf4j_api_1_6_1.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_slf4j_slf4j_log4j12_1_6_1.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_springframework_spring_aop_4_1_7_RELEASE.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_springframework_spring_beans_4_1_7_RELEASE.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_springframework_spring_context_4_1_7_RELEASE.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_springframework_spring_core_4_1_7_RELEASE.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_springframework_spring_expression_4_1_7_RELEASE.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__org_springframework_spring_test_4_1_7_RELEASE.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | false
5 |
6 | false
7 | false
8 |
9 |
10 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | Abstraction issuesJava
25 |
26 |
27 | Android
28 |
29 |
30 | Android > Lint > Accessibility
31 |
32 |
33 | Android > Lint > Correctness
34 |
35 |
36 | Android > Lint > Correctness > Messages
37 |
38 |
39 | Android > Lint > Internationalization
40 |
41 |
42 | Android > Lint > Internationalization > Bidirectional Text
43 |
44 |
45 | Android > Lint > Performance
46 |
47 |
48 | Android > Lint > Security
49 |
50 |
51 | Android > Lint > Usability
52 |
53 |
54 | Android > Lint > Usability > Icons
55 |
56 |
57 | Android Lint for Kotlin
58 |
59 |
60 | Assignment issuesGroovy
61 |
62 |
63 | Assignment issuesJava
64 |
65 |
66 | Bitwise operation issuesJava
67 |
68 |
69 | Bitwise operation issuesJavaScript
70 |
71 |
72 | CDI(Contexts and Dependency Injection) issues
73 |
74 |
75 | CSS
76 |
77 |
78 | Class metricsJava
79 |
80 |
81 | Class structureJava
82 |
83 |
84 | Cloning issuesJava
85 |
86 |
87 | Code maturity issuesJava
88 |
89 |
90 | Code style issuesJava
91 |
92 |
93 | Code style issuesJavaScript
94 |
95 |
96 | CodeSpring CoreSpring
97 |
98 |
99 | Compiler issuesJava
100 |
101 |
102 | Control flow issuesJava
103 |
104 |
105 | Control flow issuesJavaScript
106 |
107 |
108 | DOM issuesJavaScript
109 |
110 |
111 | Data flow issuesJava
112 |
113 |
114 | Data flow issuesJavaScript
115 |
116 |
117 | ECMAScript 6 migration aidsJavaScript
118 |
119 |
120 | Encapsulation issuesJava
121 |
122 |
123 | Error handlingGroovy
124 |
125 |
126 | Error handlingJava
127 |
128 |
129 | Error handlingJavaScript
130 |
131 |
132 | Finalization issuesJava
133 |
134 |
135 | General
136 |
137 |
138 | GeneralJavaScript
139 |
140 |
141 | Google Web Toolkit issues
142 |
143 |
144 | Groovy
145 |
146 |
147 | Guice Inspections
148 |
149 |
150 | HTML
151 |
152 |
153 | ImportsJava
154 |
155 |
156 | Inheritance issuesJava
157 |
158 |
159 | Initialization issuesJava
160 |
161 |
162 | Internationalization issuesJava
163 |
164 |
165 | J2ME issuesJava
166 |
167 |
168 | JBoss Seam issues
169 |
170 |
171 | JSON
172 |
173 |
174 | JSP Inspections
175 |
176 |
177 | JUnit issuesJava
178 |
179 |
180 | Java
181 |
182 |
183 | Java EE issues
184 |
185 |
186 | Java language level migration aidsJava
187 |
188 |
189 | JavaBeans issuesJava
190 |
191 |
192 | JavaFX
193 |
194 |
195 | JavaScript
196 |
197 |
198 | JavaScript function metricsJavaScript
199 |
200 |
201 | Javadoc issuesJava
202 |
203 |
204 | Kotlin
205 |
206 |
207 | Logging issuesJava
208 |
209 |
210 | Memory issuesJava
211 |
212 |
213 | Method MetricsGroovy
214 |
215 |
216 | Method metricsJava
217 |
218 |
219 | Naming conventionsJava
220 |
221 |
222 | Numeric issuesJava
223 |
224 |
225 | OtherGroovy
226 |
227 |
228 | Performance issuesJava
229 |
230 |
231 | Plugin DevKit
232 |
233 |
234 | Portability issuesJava
235 |
236 |
237 | Potentially confusing code constructsGroovy
238 |
239 |
240 | Potentially confusing code constructsJavaScript
241 |
242 |
243 | Probable bugsCSS
244 |
245 |
246 | Probable bugsGroovy
247 |
248 |
249 | Probable bugsJava
250 |
251 |
252 | Probable bugsJavaScript
253 |
254 |
255 | Properties Files
256 |
257 |
258 | RELAX NG
259 |
260 |
261 | SQL
262 |
263 |
264 | Security issuesJava
265 |
266 |
267 | Serialization issuesJava
268 |
269 |
270 | Spelling
271 |
272 |
273 | Spring
274 |
275 |
276 | Spring AOPSpring
277 |
278 |
279 | Spring CoreSpring
280 |
281 |
282 | Spring OSGiSpring
283 |
284 |
285 | TestNGJava
286 |
287 |
288 | Threading issuesGroovy
289 |
290 |
291 | Threading issuesJava
292 |
293 |
294 | UI Form Problems
295 |
296 |
297 | Verbose or redundant code constructsJava
298 |
299 |
300 | Visibility issuesJava
301 |
302 |
303 | WSDL issues
304 |
305 |
306 | Web Services
307 |
308 |
309 | XML
310 |
311 |
312 | XMLSpring CoreSpring
313 |
314 |
315 | XPath
316 |
317 |
318 | XSLT
319 |
320 |
321 |
322 |
323 | Android
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/uiDesigner.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | -
6 |
7 |
8 | -
9 |
10 |
11 | -
12 |
13 |
14 | -
15 |
16 |
17 | -
18 |
19 |
20 |
21 |
22 |
23 | -
24 |
25 |
26 |
27 |
28 |
29 | -
30 |
31 |
32 |
33 |
34 |
35 | -
36 |
37 |
38 |
39 |
40 |
41 | -
42 |
43 |
44 |
45 |
46 | -
47 |
48 |
49 |
50 |
51 | -
52 |
53 |
54 |
55 |
56 | -
57 |
58 |
59 |
60 |
61 | -
62 |
63 |
64 |
65 |
66 | -
67 |
68 |
69 |
70 |
71 | -
72 |
73 |
74 | -
75 |
76 |
77 |
78 |
79 | -
80 |
81 |
82 |
83 |
84 | -
85 |
86 |
87 |
88 |
89 | -
90 |
91 |
92 |
93 |
94 | -
95 |
96 |
97 |
98 |
99 | -
100 |
101 |
102 | -
103 |
104 |
105 | -
106 |
107 |
108 | -
109 |
110 |
111 | -
112 |
113 |
114 |
115 |
116 | -
117 |
118 |
119 | -
120 |
121 |
122 |
123 |
124 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # my-rpc
2 | >了解RPC的一些原理后,尝试自己造个轮子,加深了解。
3 | 如果你对RPC的原理和运行机制还不够了解,我更希望你从最简单的版本开始了解。
4 | [https://github.com/yuyufeng1994/my-rpc/tree/v1](https://github.com/yuyufeng1994/my-rpc/tree/v1)
5 |
6 | 个人对rpc原理的见解
7 | ---
8 | >rpc是一种远程过程调用协议。rpc主要功能:异构分布式项目之间的通信,使消费者只需要知道接口,远程调用方法就像调用本地方法一样。
9 | 要使得消费层只通过接口调用远程实现方法,那么其之间的传输数据肯定是:类、方法、参数、返回值,以及一些其它传输的信息。
10 | 之间涉及到通信,肯定要发布服务供客户端请求。客户端要执行未知实现的方法,是通过动态代理实现的。
11 | 在了解动态代理的使用后,就会发现,在动态代理的方法执行过程中,可以不用去执行真实方法(invoke方法中),你可以获取到上面所需要的类、方法、参数、返回值等执行方法的参数。
12 | 那么把这些参数传输到远程去。在提供层接收到消费层方法的传参后,通过反射执行已经注册的类方法。(提供层要把暴露接口的实现类的方法注册到容器中,供查找)
13 | 自己编写rpc之前的所需的准备
14 | --
15 | >了解了常用rpc框架dubbo的流程。复习了动态代理。复习了java io通信。
16 |
17 | 模块说明
18 | ------
19 | #### rpc-core
20 | >这个模块是rpc的核心部分包括网络通信,客户端动态代理,通信数据结构,Spring结合模块
21 | #### rpc-service
22 | >这个模块rpc提供层暴露的接口
23 | #### rpc-provider
24 | >这个模块是rpc的提供层
25 | #### rpc-consumer
26 | >这个模块是rpc的消费层
27 |
28 | 依赖关系
29 | ---
30 | >rpc-consumer、rpc-provider为rpc使用方,所以他们两个都依赖 rpc-core 模块。
31 | 因为提供层要为消费层暴露接口,所以提取了公共接口,因此,它们两个都依赖rpc-service
32 |
33 |
34 | 启动调试
35 | --
36 | * 使用HelloService接口进行调试。首先运行zookeeper服务,默认本地ip。
37 | * 启动提供层:在rpc-provider模块中运行top.yuyufeng.rpc.ServerApp.Main
38 | * 启动消费层:在rpc-consumer模块中运行top.yuyufeng.rpc.test.ClientApp.Main
39 |
40 | 启动调试(使用Spring)
41 | --
42 | * 使用HelloService接口进行调试。首先运行zookeeper服务,默认本地ip。
43 | * 启动提供层:在rpc-provider模块中运行top.yuyufeng.rpc.ServerAppBySpring
44 | * 启动消费层:在rpc-consumer模块中运行top.yuyufeng.rpc.test.ClientAppSpring
45 |
46 | 缺陷
47 | --
48 | * 消费降级等功能的有待实现
49 | >* ...(*细节有待实现*)
50 |
51 | 版本说明
52 | --
53 | > 项目随着主干进行下去
54 | * [master](https://github.com/yuyufeng1994/my-rpc/)
55 | * [v4](https://github.com/yuyufeng1994/my-rpc/) 增加Cluster(负载)层,结合Spring启动
56 | * [v3](https://github.com/yuyufeng1994/my-rpc/tree/v3) 使用Netty4来操作NIO通信,不再是原来的多线程BIO。改变了通信方式,结构变化较大
57 | * [v2](https://github.com/yuyufeng1994/my-rpc/tree/v2) 在上个版本基础上,增加zookeeper注册发现,客户端服务端无需互相知道对方。增加Protostuff序列化(序列化效率高)
58 | * [v1](https://github.com/yuyufeng1994/my-rpc/tree/v1) 原始版本(bio多线程通信 客户端服务端直连)
59 |
60 |
61 | 更新日志
62 | ----
63 | >* 2019年4月11日增加结合Spring启动,服务端服务实例单例化
64 | >* 2017年8月29日 增加Cluster(负载)层
65 | 之前初始版本已经建立分支[https://github.com/yuyufeng1994/my-rpc/tree/v3](https://github.com/yuyufeng1994/my-rpc/tree/v3)
66 | >* 2017年8月28日 将原来的BIO通信改成了NIO,加入*Netty4*进行通信
67 | 之前初始版本已经建立分支[https://github.com/yuyufeng1994/my-rpc/tree/v2](https://github.com/yuyufeng1994/my-rpc/tree/v2)
68 | >* 2017年8月24日 使用*zookeeper*来作协调服务注册与发现,使用*Protostuff*序列化(取代jdk序列化)
69 | 之前初始版本已经建立分支[https://github.com/yuyufeng1994/my-rpc/tree/v1](https://github.com/yuyufeng1994/my-rpc/tree/v1)
70 |
--------------------------------------------------------------------------------
/my-rpc.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | top.yuyufeng
8 | my-rpc
9 | pom
10 | 1.0-SNAPSHOT
11 |
12 |
13 | 1.8
14 | 1.8
15 |
16 |
17 |
18 |
19 | rpc-consumer
20 | rpc-service
21 | rpc-provider
22 | rpc-core
23 |
24 |
25 |
26 |
27 |
28 | junit
29 | junit
30 | 4.12
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 | org.apache.maven.plugins
39 | maven-surefire-plugin
40 | 2.19
41 |
42 |
43 | true
44 | -Dfile.encoding=UTF-8
45 |
46 |
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/rpc-consumer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | my-rpc
7 | top.yuyufeng
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | rpc-consumer
13 |
14 |
15 |
16 | top.yuyufeng
17 | rpc-service
18 | 1.0-SNAPSHOT
19 |
20 |
21 | top.yuyufeng
22 | rpc-core
23 | 1.0-SNAPSHOT
24 |
25 |
26 |
27 | org.springframework
28 | spring-test
29 | 4.1.7.RELEASE
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/rpc-consumer/rpc-consumer.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/rpc-consumer/src/main/java/top/yuyufeng/rpc/test/ClientApp.java:
--------------------------------------------------------------------------------
1 | package top.yuyufeng.rpc.test;
2 |
3 | import top.yuyufeng.rpc.client.proxy.RemoteServiceImpl;
4 | import top.yuyufeng.rpc.service.CalService;
5 | import top.yuyufeng.rpc.service.HelloService;
6 | import top.yuyufeng.rpc.service.top.yuyufeng.dto.MyResult;
7 |
8 | /**
9 | * 服务调用 (客户端)
10 | * @date 2017/8/19.
11 | * @author yuyufeng
12 | */
13 | public class ClientApp {
14 | public static void main(String[] args) {
15 | //设置Zookeeper的地址
16 | RemoteServiceImpl.setZookeeperAddress("127.0.0.1:2181");
17 |
18 | //获取动态代理的HelloService的“真实对象(其实内部不是真实的,被换成了调用远程方法)”
19 | HelloService helloService = RemoteServiceImpl.newRemoteProxyObject(HelloService.class);
20 | String result = helloService.sayHello("yyf");
21 | System.out.println(result);
22 |
23 | CalService calService = RemoteServiceImpl.newRemoteProxyObject(CalService.class);
24 | MyResult myResult = calService.getResult(1, 2);
25 | System.out.println(myResult);
26 |
27 | myResult = calService.getResult(3, 2);
28 | System.out.println(myResult);
29 |
30 | /*//启动10个线程去请求
31 | for (int i = 0; i < 10; i++) {
32 | new Thread(){
33 | @Override
34 | public void run() {
35 | for (int j = 0; j < 10; j++) {
36 | String result = helloService.sayHello("yyf");
37 | System.out.println(result);
38 | MyResult myResult = calService.getResult(1,2);
39 | System.out.println(myResult);
40 | }
41 | }
42 | }.start();
43 | }*/
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/rpc-consumer/src/main/java/top/yuyufeng/rpc/test/ClientAppSpring.java:
--------------------------------------------------------------------------------
1 | package top.yuyufeng.rpc.test;
2 |
3 | import org.springframework.context.support.ClassPathXmlApplicationContext;
4 | import top.yuyufeng.rpc.client.proxy.RemoteServiceImpl;
5 | import top.yuyufeng.rpc.service.CalService;
6 | import top.yuyufeng.rpc.service.HelloService;
7 | import top.yuyufeng.rpc.service.top.yuyufeng.dto.MyResult;
8 |
9 | /**
10 | * 服务调用 (客户端)
11 | *
12 | * @author yuyufeng
13 | * @date 2017/8/19.
14 | */
15 | public class ClientAppSpring {
16 | public static void main(String[] args) {
17 | ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"my-rpc-consumer.xml"});
18 |
19 | HelloService helloService = context.getBean(HelloService.class);
20 | String result = helloService.sayHello("yyf");
21 | System.out.println(result);
22 |
23 | CalService calService = context.getBean(CalService.class);
24 | MyResult myResult = calService.getResult(1, 2);
25 | System.out.println(myResult);
26 |
27 | myResult = calService.getResult(3, 2);
28 | System.out.println(myResult);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/rpc-consumer/src/main/resources/my-rpc-consumer.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/rpc-core/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | my-rpc
7 | top.yuyufeng
8 | 1.0-SNAPSHOT
9 |
10 | jar
11 | 4.0.0
12 |
13 | rpc-core
14 |
15 |
16 | 4.1.7.RELEASE
17 |
18 |
19 |
20 |
21 | org.apache.zookeeper
22 | zookeeper
23 | 3.4.6
24 |
25 |
26 |
27 | com.github.sgroschupf
28 | zkclient
29 | 0.1
30 |
31 |
32 |
33 | com.dyuproject.protostuff
34 | protostuff-api
35 | 1.0.10
36 |
37 |
38 | com.dyuproject.protostuff
39 | protostuff-core
40 | 1.0.10
41 |
42 |
43 | com.dyuproject.protostuff
44 | protostuff-runtime
45 | 1.0.10
46 |
47 |
48 |
49 | io.netty
50 | netty-all
51 | 4.1.14.Final
52 |
53 |
54 |
55 |
56 | org.springframework
57 | spring-core
58 | ${spring-version}
59 |
60 |
61 |
62 | org.springframework
63 | spring-beans
64 | ${spring-version}
65 |
66 |
67 |
68 | org.springframework
69 | spring-context
70 | ${spring-version}
71 |
72 |
73 |
74 |
75 |
76 |
77 |
--------------------------------------------------------------------------------
/rpc-core/rpc-core.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/rpc-core/src/main/java/top/yuyufeng/rpc/MyDecoder.java:
--------------------------------------------------------------------------------
1 | package top.yuyufeng.rpc;
2 |
3 | import io.netty.buffer.ByteBuf;
4 | import io.netty.channel.ChannelHandlerContext;
5 | import io.netty.handler.codec.ByteToMessageDecoder;
6 | import top.yuyufeng.rpc.utils.ProtostuffUtil;
7 |
8 | import java.util.List;
9 |
10 | /**
11 | * Created by yuyufeng on 2017/8/28.
12 | */
13 | public class MyDecoder extends ByteToMessageDecoder {
14 | private Class> genericClass;
15 |
16 | public MyDecoder(Class> genericClass) {
17 | this.genericClass = genericClass;
18 | }
19 |
20 | @Override
21 | public final void decode(ChannelHandlerContext ctx, ByteBuf in, List