提交 ad1038f9 编写于 作者: L lvxuhong

代码优化

上级 4a0c75b2
package github.javaguide; package github.javaguide;
import github.javaguide.api.Hello;
import github.javaguide.api.HelloService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
/** /**
......
package github.javaguide; package github.javaguide;
import github.javaguide.api.HelloService;
import github.javaguide.remoting.transport.netty.server.NettyServer; import github.javaguide.remoting.transport.netty.server.NettyServer;
/** /**
...@@ -8,6 +9,7 @@ import github.javaguide.remoting.transport.netty.server.NettyServer; ...@@ -8,6 +9,7 @@ import github.javaguide.remoting.transport.netty.server.NettyServer;
*/ */
public class NettyServerMain { public class NettyServerMain {
public static void main(String[] args) { public static void main(String[] args) {
HelloService helloService = new HelloServiceImpl(); HelloService helloService = new HelloServiceImpl();
NettyServer nettyServer = new NettyServer("127.0.0.1", 9999); NettyServer nettyServer = new NettyServer("127.0.0.1", 9999);
nettyServer.publishService(helloService, HelloService.class); nettyServer.publishService(helloService, HelloService.class);
......
package github.javaguide; package github.javaguide;
import github.javaguide.api.HelloService;
import github.javaguide.remoting.transport.socket.SocketRpcServer; import github.javaguide.remoting.transport.socket.SocketRpcServer;
/** /**
......
package github.javaguide; package github.javaguide.api;
import lombok.AllArgsConstructor; import lombok.*;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import java.io.Serializable; import java.io.Serializable;
......
package github.javaguide; package github.javaguide.api;
/** /**
* @author shuang.kou * @author shuang.kou
......
...@@ -23,7 +23,7 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -23,7 +23,7 @@ import java.util.concurrent.ConcurrentHashMap;
public final class CuratorUtils { public final class CuratorUtils {
private static final int BASE_SLEEP_TIME = 1000; private static final int BASE_SLEEP_TIME = 1000;
private static final int MAX_RETRIES = 5; private static final int MAX_RETRIES = 5;
private static final String CONNECT_STRING = "127.0.0.1:2181"; private static final String CONNECT_STRING = "20.21.1.145:2181";
public static final String ZK_REGISTER_ROOT_PATH = "/my-rpc"; public static final String ZK_REGISTER_ROOT_PATH = "/my-rpc";
private static Map<String, List<String>> serviceAddressMap = new ConcurrentHashMap<>(); private static Map<String, List<String>> serviceAddressMap = new ConcurrentHashMap<>();
private static Set<String> registeredPathSet = ConcurrentHashMap.newKeySet(); private static Set<String> registeredPathSet = ConcurrentHashMap.newKeySet();
......
...@@ -19,5 +19,6 @@ public class ZkServiceRegistry implements ServiceRegistry { ...@@ -19,5 +19,6 @@ public class ZkServiceRegistry implements ServiceRegistry {
//根节点下注册子节点:服务 //根节点下注册子节点:服务
String servicePath = CuratorUtils.ZK_REGISTER_ROOT_PATH + "/" + serviceName + inetSocketAddress.toString(); String servicePath = CuratorUtils.ZK_REGISTER_ROOT_PATH + "/" + serviceName + inetSocketAddress.toString();
CuratorUtils.createPersistentNode(servicePath); CuratorUtils.createPersistentNode(servicePath);
} }
} }
...@@ -2,4 +2,4 @@ ...@@ -2,4 +2,4 @@
基于spring 5.2.6.RELEASE 基于spring 5.2.6.RELEASE
- 通过一个注解就可以实现远程接口的调用 - 通过一个注解就可以实现远程接口的调用
@RpcServiceScan("github.javaguide.spring.service") @RpcServiceScan("github.javaguide.api")
\ No newline at end of file \ No newline at end of file
...@@ -27,5 +27,12 @@ ...@@ -27,5 +27,12 @@
<version>${project.version}</version> <version>${project.version}</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>github.javaguide</groupId>
<artifactId>hello-service-api</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
...@@ -25,15 +25,7 @@ public class RpcServiceFactoryBean<T> implements FactoryBean<T> { ...@@ -25,15 +25,7 @@ public class RpcServiceFactoryBean<T> implements FactoryBean<T> {
if (rpcServiceInterface == null) { if (rpcServiceInterface == null) {
throw new IllegalStateException(""); throw new IllegalStateException("");
} }
return ClientProxy.getServiceProxy(rpcServiceInterface); return ClientProxy.getServiceProxy(rpcServiceInterface);
// return (T) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]{rpcServiceInterface}, new InvocationHandler() {
// @Override
// public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
// if(method.equals()){}
// return null;
// }
// });
} }
@Override @Override
......
package github.javaguide.spring; package github.javaguide.spring;
import github.javaguide.api.Hello;
import github.javaguide.api.HelloService;
import github.javaguide.spring.annotation.RpcServiceScan; import github.javaguide.spring.annotation.RpcServiceScan;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -19,12 +22,16 @@ public class ClientTest { ...@@ -19,12 +22,16 @@ public class ClientTest {
applicationContext.refresh(); applicationContext.refresh();
applicationContext.start(); applicationContext.start();
HelloService helloService = applicationContext.getBean(HelloService.class);
Hello hello = Hello.builder().message("test message").description("test description").build();
String res = helloService.hello(hello);
String expectedResult = "Hello description is " + hello.getDescription();
Assert.assertEquals(expectedResult, res);
} }
@Configuration //@Configuration
@RpcServiceScan("github.javaguide.spring.service") @RpcServiceScan("github.javaguide.api")
public static class TestConfig { public static class TestConfig {
} }
......
package github.javaguide.spring.service;
public interface Service1 {
}
package github.javaguide.spring.service;
/**
* @description:
* @author:lvxuhong
* @date:2020/6/18
*/
public interface Service2 {
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册