提交 1e936a02 编写于 作者: G guide

[docs]update readme

上级 4c4b38fa
......@@ -142,13 +142,13 @@ Publish services (transport using Netty) :
* @author shuang.kou
* @createTime 2020年05月10日 07:25:00
*/
@ComponentScan("github.javaguide")
@RpcScan(basePackage = {"github.javaguide.serviceimpl"})
public class NettyServerMain {
public static void main(String[] args) {
// Register service via annotation(通过注解注册服务 HelloServiceImpl )
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(NettyServerMain.class);
NettyServer nettyServer = applicationContext.getBean(NettyServer.class);
// Register service manually(手动注册服务 HelloServiceImpl2)
// Register service via annotation
new AnnotationConfigApplicationContext(NettyServerMain.class);
NettyServer nettyServer = new NettyServer();
// Register service manually
HelloService helloService2 = new HelloServiceImpl2();
RpcServiceProperties rpcServiceProperties = RpcServiceProperties.builder()
.group("test2").version("version2").build();
......
......@@ -71,7 +71,7 @@
- [x] **处理一个接口有多个类实现的情况** :对服务分组,发布服务的时候增加一个 group 参数即可。
- [x] **集成 Spring 通过注解注册服务**
- [x] **增加服务版本号** :建议使用两位数字版本,如:1.0,通常在接口不兼容时版本号才需要升级。为什么要增加服务版本号?为后续不兼容升级提供可能,比如服务接口增加方法,或服务模型增加字段,可向后兼容,删除方法或删除字段,将不兼容,枚举类型新增字段也不兼容,需通过变更版本号升级。
- [ ] **对 SPI 机制的运用**
- [x] **对 SPI 机制的运用**
- [ ] **增加可配置比如序列化方式、注册中心的实现方式,避免硬编码** :通过 API 配置,后续集成 Spring 的话建议使用配置文件的方式进行配置
- [ ] **使用注解进行服务消费**
- [ ] **客户端与服务端通信协议(数据包结构)重新设计** ,可以将原有的 `RpcRequest``RpcReuqest` 对象作为消息体,然后增加如下字段(可以参考:《Netty 入门实战小册》和 Dubbo 框架对这块的设计):
......@@ -181,20 +181,18 @@ public class HelloServiceImpl2 implements HelloService {
* @author shuang.kou
* @createTime 2020年05月10日 07:25:00
*/
@ComponentScan("github.javaguide")
@RpcScan(basePackage = {"github.javaguide.serviceimpl"})
public class NettyServerMain {
public static void main(String[] args) {
// Register service via annotation(通过注解注册服务 HelloServiceImpl )
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(NettyServerMain.class);
NettyServer nettyServer = applicationContext.getBean(NettyServer.class);
nettyServer.start();
// Register service manually(手动注册服务 HelloServiceImpl2)
// Register service via annotation
new AnnotationConfigApplicationContext(NettyServerMain.class);
NettyServer nettyServer = new NettyServer();
// Register service manually
HelloService helloService2 = new HelloServiceImpl2();
ServiceProvider serviceProvider = new ServiceProviderImpl();
RpcServiceProperties rpcServiceProperties = RpcServiceProperties.builder()
.group("test2").version("test2").build();
serviceProvider.publishService(helloService2, rpcServiceProperties);
.group("test2").version("version2").build();
nettyServer.registerService(helloService2, rpcServiceProperties);
nettyServer.start();
}
}
```
......
package github.javaguide;
import github.javaguide.entity.RpcServiceProperties;
import github.javaguide.proxy.RpcClientProxy;
import github.javaguide.remoting.transport.ClientTransport;
import github.javaguide.remoting.transport.netty.client.NettyClientTransport;
/**
* @author shuang.kou
* @createTime 2020年05月10日 07:25:00
*/
public class NettyClientMain2 {
public static void main(String[] args) throws InterruptedException {
ClientTransport rpcClient = new NettyClientTransport();
RpcServiceProperties rpcServiceProperties = RpcServiceProperties.builder()
.group("test2").version("version2").build();
RpcClientProxy rpcClientProxy = new RpcClientProxy(rpcClient, rpcServiceProperties);
HelloService helloService = rpcClientProxy.getProxy(HelloService.class);
String hello = helloService.hello(new Hello("111", "222"));
//如需使用 assert 断言,需要在 VM options 添加参数:-ea
assert "Hello description is 222".equals(hello);
Thread.sleep(12000);
for (int i = 0; i < 10; i++) {
helloService.hello(new Hello("111", "222"));
}
}
}
package github.javaguide.factory;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
......@@ -21,10 +22,12 @@ public final class SingletonFactory {
synchronized (c) {
if (instance == null) {
try {
instance = c.newInstance();
instance = c.getDeclaredConstructor().newInstance();
OBJECT_MAP.put(key, instance);
} catch (IllegalAccessException | InstantiationException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册