Skip to content

Commit 26d68c7

Browse files
committed
[refractor&fix]完善rpc传参&修复失败测试
1 parent 336316c commit 26d68c7

35 files changed

+163
-172
lines changed

README-EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ public class NettyServerMain {
150150
NettyServer nettyServer = new NettyServer();
151151
// Register service manually
152152
HelloService helloService2 = new HelloServiceImpl2();
153-
RpcServiceProperties rpcServiceProperties = RpcServiceProperties.builder()
153+
RpcServiceProperties rpcServiceConfig = RpcServiceProperties.builder()
154154
.group("test2").version("version2").build();
155-
nettyServer.registerService(helloService2, rpcServiceProperties);
155+
nettyServer.registerService(helloService2, rpcServiceConfig);
156156
nettyServer.start();
157157
}
158158
}
@@ -182,9 +182,9 @@ public class HelloController {
182182

183183
```java
184184
ClientTransport rpcRequestTransport = new SocketRpcClient();
185-
RpcServiceProperties rpcServiceProperties = RpcServiceProperties.builder()
185+
RpcServiceProperties rpcServiceConfig = RpcServiceProperties.builder()
186186
.group("test2").version("version2").build();
187-
RpcClientProxy rpcClientProxy = new RpcClientProxy(rpcRequestTransport, rpcServiceProperties);
187+
RpcClientProxy rpcClientProxy = new RpcClientProxy(rpcRequestTransport, rpcServiceConfig);
188188
HelloService helloService = rpcClientProxy.getProxy(HelloService.class);
189189
String hello = helloService.hello(new Hello("111", "222"));
190190
System.out.println(hello);

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ public class NettyServerMain {
196196
NettyServer nettyServer = new NettyServer();
197197
// Register service manually
198198
HelloService helloService2 = new HelloServiceImpl2();
199-
RpcServiceProperties rpcServiceProperties = RpcServiceProperties.builder()
199+
RpcServiceProperties rpcServiceConfig = RpcServiceProperties.builder()
200200
.group("test2").version("version2").build();
201-
nettyServer.registerService(helloService2, rpcServiceProperties);
201+
nettyServer.registerService(helloService2, rpcServiceConfig);
202202
nettyServer.start();
203203
}
204204
}
@@ -227,9 +227,9 @@ public class HelloController {
227227

228228
```java
229229
ClientTransport rpcRequestTransport = new SocketRpcClient();
230-
RpcServiceProperties rpcServiceProperties = RpcServiceProperties.builder()
230+
RpcServiceProperties rpcServiceConfig = RpcServiceProperties.builder()
231231
.group("test2").version("version2").build();
232-
RpcClientProxy rpcClientProxy = new RpcClientProxy(rpcRequestTransport, rpcServiceProperties);
232+
RpcClientProxy rpcClientProxy = new RpcClientProxy(rpcRequestTransport, rpcServiceConfig);
233233
HelloService helloService = rpcClientProxy.getProxy(HelloService.class);
234234
String hello = helloService.hello(new Hello("111", "222"));
235235
System.out.println(hello);

config/checkstyle.xml

-2
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,6 @@
178178
<property name="basicOffset" value="4"/>
179179
<property name="braceAdjustment" value="0"/>
180180
<property name="caseIndent" value="4"/>
181-
<property name="throwsIndent" value="8"/>
182-
<property name="lineWrappingIndentation" value="8"/>
183181
<property name="arrayInitIndent" value="4"/>
184182
</module>
185183

docs/造个轮子:自定义注解实现服务注册和消费(RPC框架).md

+4-4
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ public @interface RpcService {
111111

112112
```java
113113
import github.javaguide.annotation.RpcService;
114-
import github.javaguide.entity.RpcServiceProperties;
114+
import github.javaguide.config.RpcServiceConfig;
115115
import github.javaguide.factory.SingletonFactory;
116116
import github.javaguide.provider.ServiceProvider;
117-
import github.javaguide.provider.ServiceProviderImpl;
117+
import github.javaguide.provider.impl.ZkServiceProviderImpl;
118118
import lombok.SneakyThrows;
119119
import lombok.extern.slf4j.Slf4j;
120120
import org.springframework.beans.BeansException;
@@ -145,10 +145,10 @@ public class SpringBeanPostProcessor implements BeanPostProcessor {
145145
log.info("[{}] is annotated with [{}]", bean.getClass().getName(), RpcService.class.getCanonicalName());
146146
// 获取注解
147147
RpcService rpcService = bean.getClass().getAnnotation(RpcService.class);
148-
RpcServiceProperties rpcServiceProperties = RpcServiceProperties.builder()
148+
RpcServiceProperties rpcServiceConfig = RpcServiceProperties.builder()
149149
.group(rpcService.group()).version(rpcService.version()).build();
150150
// 发布服务
151-
serviceProvider.publishService(bean, rpcServiceProperties);
151+
serviceProvider.publishService(bean, rpcServiceConfig);
152152
}
153153
return bean;
154154
}

example-client/src/main/java/github/javaguide/SocketClientMain.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package github.javaguide;
22

3-
import github.javaguide.entity.RpcServiceProperties;
3+
import github.javaguide.config.RpcServiceConfig;
44
import github.javaguide.proxy.RpcClientProxy;
55
import github.javaguide.remoting.transport.RpcRequestTransport;
66
import github.javaguide.remoting.transport.socket.SocketRpcClient;
@@ -12,9 +12,8 @@
1212
public class SocketClientMain {
1313
public static void main(String[] args) {
1414
RpcRequestTransport rpcRequestTransport = new SocketRpcClient();
15-
RpcServiceProperties rpcServiceProperties = RpcServiceProperties.builder()
16-
.group("test2").version("version2").build();
17-
RpcClientProxy rpcClientProxy = new RpcClientProxy(rpcRequestTransport, rpcServiceProperties);
15+
RpcServiceConfig rpcServiceConfig = new RpcServiceConfig();
16+
RpcClientProxy rpcClientProxy = new RpcClientProxy(rpcRequestTransport, rpcServiceConfig);
1817
HelloService helloService = rpcClientProxy.getProxy(HelloService.class);
1918
String hello = helloService.hello(new Hello("111", "222"));
2019
System.out.println(hello);

example-server/src/main/java/NettyServerMain.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import github.javaguide.HelloService;
22
import github.javaguide.annotation.RpcScan;
3-
import github.javaguide.entity.RpcServiceProperties;
3+
import github.javaguide.config.RpcServiceConfig;
44
import github.javaguide.remoting.transport.netty.server.NettyRpcServer;
55
import github.javaguide.serviceimpl.HelloServiceImpl2;
66
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -19,9 +19,9 @@ public static void main(String[] args) {
1919
NettyRpcServer nettyRpcServer = (NettyRpcServer) applicationContext.getBean("nettyRpcServer");
2020
// Register service manually
2121
HelloService helloService2 = new HelloServiceImpl2();
22-
RpcServiceProperties rpcServiceProperties = RpcServiceProperties.builder()
23-
.group("test2").version("version2").build();
24-
nettyRpcServer.registerService(helloService2, rpcServiceProperties);
22+
RpcServiceConfig rpcServiceConfig = RpcServiceConfig.builder()
23+
.group("test2").version("version2").service(helloService2).build();
24+
nettyRpcServer.registerService(rpcServiceConfig);
2525
nettyRpcServer.start();
2626
}
2727
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import github.javaguide.HelloService;
2-
import github.javaguide.entity.RpcServiceProperties;
2+
import github.javaguide.config.RpcServiceConfig;
33
import github.javaguide.remoting.transport.socket.SocketRpcServer;
44
import github.javaguide.serviceimpl.HelloServiceImpl;
55

@@ -11,9 +11,9 @@ public class SocketServerMain {
1111
public static void main(String[] args) {
1212
HelloService helloService = new HelloServiceImpl();
1313
SocketRpcServer socketRpcServer = new SocketRpcServer();
14-
RpcServiceProperties rpcServiceProperties = RpcServiceProperties.builder()
15-
.group("test2").version("version2").build();
16-
socketRpcServer.registerService(helloService, rpcServiceProperties);
14+
RpcServiceConfig rpcServiceConfig = new RpcServiceConfig();
15+
rpcServiceConfig.setService(helloService);
16+
socketRpcServer.registerService(rpcServiceConfig);
1717
socketRpcServer.start();
1818
}
1919
}

rpc-framework-common/src/main/java/github/javaguide/factory/SingletonFactory.java

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package github.javaguide.factory;
22

33
import java.lang.reflect.InvocationTargetException;
4-
import java.util.HashMap;
54
import java.util.Map;
65
import java.util.concurrent.ConcurrentHashMap;
76

rpc-framework-common/src/main/java/github/javaguide/utils/RuntimeUtil.java

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
public class RuntimeUtil {
88
/**
99
* 获取CPU的核心数
10+
*
1011
* @return cpu的核心数
1112
*/
1213
public static int cpus() {

rpc-framework-simple/src/main/java/github/javaguide/compress/gzip/GzipCompress.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public byte[] compress(byte[] bytes) {
2424
throw new NullPointerException("bytes is null");
2525
}
2626
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
27-
GZIPOutputStream gzip = new GZIPOutputStream(out)) {
27+
GZIPOutputStream gzip = new GZIPOutputStream(out)) {
2828
gzip.write(bytes);
2929
gzip.flush();
3030
gzip.finish();
@@ -40,7 +40,7 @@ public byte[] decompress(byte[] bytes) {
4040
throw new NullPointerException("bytes is null");
4141
}
4242
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
43-
GZIPInputStream gunzip = new GZIPInputStream(new ByteArrayInputStream(bytes))) {
43+
GZIPInputStream gunzip = new GZIPInputStream(new ByteArrayInputStream(bytes))) {
4444
byte[] buffer = new byte[BUFFER_SIZE];
4545
int n;
4646
while ((n = gunzip.read(buffer)) > -1) {

rpc-framework-common/src/main/java/github/javaguide/entity/RpcServiceProperties.java renamed to rpc-framework-simple/src/main/java/github/javaguide/config/RpcServiceConfig.java

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package github.javaguide.entity;
1+
package github.javaguide.config;
22

33
import lombok.AllArgsConstructor;
44
import lombok.Builder;
@@ -17,18 +17,26 @@
1717
@Setter
1818
@Builder
1919
@ToString
20-
public class RpcServiceProperties {
20+
public class RpcServiceConfig {
2121
/**
2222
* service version
2323
*/
24-
private String version;
24+
private String version = "";
2525
/**
2626
* when the interface has multiple implementation classes, distinguish by group
2727
*/
28-
private String group;
29-
private String serviceName;
28+
private String group = "";
3029

31-
public String toRpcServiceName() {
30+
/**
31+
* target service
32+
*/
33+
private Object service;
34+
35+
public String getRpcServiceName() {
3236
return this.getServiceName() + this.getGroup() + this.getVersion();
3337
}
38+
39+
public String getServiceName() {
40+
return this.service.getClass().getInterfaces()[0].getCanonicalName();
41+
}
3442
}

rpc-framework-simple/src/main/java/github/javaguide/loadbalance/loadbalancer/ConsistentHashLoadBalance.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,14 @@ public class ConsistentHashLoadBalance extends AbstractLoadBalance {
2727
protected String doSelect(List<String> serviceAddresses, RpcRequest rpcRequest) {
2828
int identityHashCode = System.identityHashCode(serviceAddresses);
2929
// build rpc service name by rpcRequest
30-
String rpcServiceName = rpcRequest.toRpcProperties().toRpcServiceName();
31-
30+
String rpcServiceName = rpcRequest.getRpcServiceName();
3231
ConsistentHashSelector selector = selectors.get(rpcServiceName);
33-
3432
// check for updates
3533
if (selector == null || selector.identityHashCode != identityHashCode) {
3634
selectors.put(rpcServiceName, new ConsistentHashSelector(serviceAddresses, 160, identityHashCode));
3735
selector = selectors.get(rpcServiceName);
3836
}
39-
40-
return selector.select(rpcServiceName+ Arrays.stream(rpcRequest.getParameters()));
37+
return selector.select(rpcServiceName + Arrays.stream(rpcRequest.getParameters()));
4138
}
4239

4340
static class ConsistentHashSelector {
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package github.javaguide.provider;
22

3-
import github.javaguide.entity.RpcServiceProperties;
3+
import github.javaguide.config.RpcServiceConfig;
44

55
/**
66
* store and provide service object.
@@ -11,26 +11,19 @@
1111
public interface ServiceProvider {
1212

1313
/**
14-
* @param service service object
15-
* @param serviceClass the interface class implemented by the service instance object
16-
* @param rpcServiceProperties service related attributes
14+
* @param rpcServiceConfig rpc service related attributes
1715
*/
18-
void addService(Object service, Class<?> serviceClass, RpcServiceProperties rpcServiceProperties);
16+
void addService(RpcServiceConfig rpcServiceConfig);
1917

2018
/**
21-
* @param rpcServiceProperties service related attributes
19+
* @param rpcServiceName rpc service name
2220
* @return service object
2321
*/
24-
Object getService(RpcServiceProperties rpcServiceProperties);
22+
Object getService(String rpcServiceName);
2523

2624
/**
27-
* @param service service object
28-
* @param rpcServiceProperties service related attributes
25+
* @param rpcServiceConfig rpc service related attributes
2926
*/
30-
void publishService(Object service, RpcServiceProperties rpcServiceProperties);
27+
void publishService(RpcServiceConfig rpcServiceConfig);
3128

32-
/**
33-
* @param service service object
34-
*/
35-
void publishService(Object service);
3629
}

rpc-framework-simple/src/main/java/github/javaguide/provider/ServiceProviderImpl.java renamed to rpc-framework-simple/src/main/java/github/javaguide/provider/impl/ZkServiceProviderImpl.java

+14-22
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package github.javaguide.provider;
1+
package github.javaguide.provider.impl;
22

3-
import github.javaguide.entity.RpcServiceProperties;
3+
import github.javaguide.config.RpcServiceConfig;
44
import github.javaguide.enums.RpcErrorMessageEnum;
55
import github.javaguide.exception.RpcException;
66
import github.javaguide.extension.ExtensionLoader;
7+
import github.javaguide.provider.ServiceProvider;
78
import github.javaguide.registry.ServiceRegistry;
89
import github.javaguide.remoting.transport.netty.server.NettyRpcServer;
910
import lombok.extern.slf4j.Slf4j;
@@ -20,7 +21,7 @@
2021
* @createTime 2020年05月13日 11:23:00
2122
*/
2223
@Slf4j
23-
public class ServiceProviderImpl implements ServiceProvider {
24+
public class ZkServiceProviderImpl implements ServiceProvider {
2425

2526
/**
2627
* key: rpc service name(interface name + version + group)
@@ -30,47 +31,38 @@ public class ServiceProviderImpl implements ServiceProvider {
3031
private final Set<String> registeredService;
3132
private final ServiceRegistry serviceRegistry;
3233

33-
34-
public ServiceProviderImpl() {
34+
public ZkServiceProviderImpl() {
3535
serviceMap = new ConcurrentHashMap<>();
3636
registeredService = ConcurrentHashMap.newKeySet();
3737
serviceRegistry = ExtensionLoader.getExtensionLoader(ServiceRegistry.class).getExtension("zk");
3838
}
3939

4040
@Override
41-
public void addService(Object service, Class<?> serviceClass, RpcServiceProperties rpcServiceProperties) {
42-
String rpcServiceName = rpcServiceProperties.toRpcServiceName();
41+
public void addService(RpcServiceConfig rpcServiceConfig) {
42+
String rpcServiceName = rpcServiceConfig.getRpcServiceName();
4343
if (registeredService.contains(rpcServiceName)) {
4444
return;
4545
}
4646
registeredService.add(rpcServiceName);
47-
serviceMap.put(rpcServiceName, service);
48-
log.info("Add service: {} and interfaces:{}", rpcServiceName, service.getClass().getInterfaces());
47+
serviceMap.put(rpcServiceName, rpcServiceConfig.getService());
48+
log.info("Add service: {} and interfaces:{}", rpcServiceName, rpcServiceConfig.getService().getClass().getInterfaces());
4949
}
5050

5151
@Override
52-
public Object getService(RpcServiceProperties rpcServiceProperties) {
53-
Object service = serviceMap.get(rpcServiceProperties.toRpcServiceName());
52+
public Object getService(String rpcServiceName) {
53+
Object service = serviceMap.get(rpcServiceName);
5454
if (null == service) {
5555
throw new RpcException(RpcErrorMessageEnum.SERVICE_CAN_NOT_BE_FOUND);
5656
}
5757
return service;
5858
}
5959

6060
@Override
61-
public void publishService(Object service) {
62-
this.publishService(service, RpcServiceProperties.builder().group("").version("").build());
63-
}
64-
65-
@Override
66-
public void publishService(Object service, RpcServiceProperties rpcServiceProperties) {
61+
public void publishService(RpcServiceConfig rpcServiceConfig) {
6762
try {
6863
String host = InetAddress.getLocalHost().getHostAddress();
69-
Class<?> serviceRelatedInterface = service.getClass().getInterfaces()[0];
70-
String serviceName = serviceRelatedInterface.getCanonicalName();
71-
rpcServiceProperties.setServiceName(serviceName);
72-
this.addService(service, serviceRelatedInterface, rpcServiceProperties);
73-
serviceRegistry.registerService(rpcServiceProperties.toRpcServiceName(), new InetSocketAddress(host, NettyRpcServer.PORT));
64+
this.addService(rpcServiceConfig);
65+
serviceRegistry.registerService(rpcServiceConfig.getRpcServiceName(), new InetSocketAddress(host, NettyRpcServer.PORT));
7466
} catch (UnknownHostException e) {
7567
log.error("occur exception when getHostAddress", e);
7668
}

0 commit comments

Comments
 (0)