Skip to content

Commit

Permalink
优化代码 添加注释
Browse files Browse the repository at this point in the history
  • Loading branch information
tiny-x committed Mar 21, 2020
1 parent a5bc428 commit de8a95e
Show file tree
Hide file tree
Showing 66 changed files with 953 additions and 570 deletions.
4 changes: 2 additions & 2 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>leaf</artifactId>
<groupId>com.leaf-rpc</groupId>
<artifactId>leaf-rpc</artifactId>
<groupId>com.leaf.rpc</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
10 changes: 10 additions & 0 deletions common/src/main/java/com/leaf/common/model/ServiceMeta.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.leaf.common.model;

import com.leaf.common.constants.Constants;

/**
* 服务三元素 确定服务
*/
Expand All @@ -11,6 +13,14 @@ public class ServiceMeta extends Directory {

private String version;

public ServiceMeta(String serviceProviderName) {
this(Constants.DEFAULT_SERVICE_GROUP, serviceProviderName);
}

public ServiceMeta(String group, String serviceProviderName) {
this(group, serviceProviderName, Constants.DEFAULT_SERVICE_VERSION);
}

public ServiceMeta(String group, String serviceProviderName, String version) {
this.group = group;
this.serviceProviderName = serviceProviderName;
Expand Down
28 changes: 28 additions & 0 deletions common/src/main/java/com/leaf/common/utils/StackTraceUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.leaf.common.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public final class StackTraceUtil {

public static String stackTrace(Throwable t) {
if (t == null) {
return "null";
}

ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(out);
t.printStackTrace(ps);
ps.flush();
try {
return new String(out.toByteArray());
} finally {
try {
out.close();
} catch (IOException ignored) {}
}
}

private StackTraceUtil() {}
}
7 changes: 7 additions & 0 deletions doc/basic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐
2 │ 1 │ 1 │ 8 │ 4 │
├ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤
│ │ │ │ │
│ MAGIC Sign Status Invoke Id Body Length Body Content │
│ │ │ │ │
└ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘
24 changes: 10 additions & 14 deletions example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,53 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>leaf</artifactId>
<groupId>com.leaf-rpc</groupId>
<artifactId>leaf-rpc</artifactId>
<groupId>com.leaf.rpc</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>example</artifactId>

<dependencies>
<dependency>
<groupId>com.leaf-rpc</groupId>
<artifactId>common</artifactId>
</dependency>

<dependency>
<groupId>com.leaf-rpc</groupId>
<groupId>com.leaf.rpc</groupId>
<artifactId>rpc</artifactId>
</dependency>

<dependency>
<groupId>com.leaf-rpc</groupId>
<groupId>com.leaf.rpc</groupId>
<artifactId>register-default</artifactId>
</dependency>

<dependency>
<groupId>com.leaf-rpc</groupId>
<groupId>com.leaf.rpc</groupId>
<artifactId>register-zookeeper</artifactId>
</dependency>

<dependency>
<groupId>com.leaf-rpc</groupId>
<groupId>com.leaf.rpc</groupId>
<artifactId>serialization-protostuff</artifactId>
</dependency>

<dependency>
<groupId>com.leaf-rpc</groupId>
<groupId>com.leaf.rpc</groupId>
<artifactId>serialization-java</artifactId>
</dependency>

<dependency>
<groupId>com.leaf-rpc</groupId>
<groupId>com.leaf.rpc</groupId>
<artifactId>serialization-hessian</artifactId>
</dependency>

<dependency>
<groupId>com.leaf-rpc</groupId>
<groupId>com.leaf.rpc</groupId>
<artifactId>serialization-json</artifactId>
</dependency>

<dependency>
<groupId>com.leaf-rpc</groupId>
<groupId>com.leaf.rpc</groupId>
<artifactId>spring-support</artifactId>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.leaf.common.UnresolvedAddress;
import com.leaf.example.cluster.api.ClusterService;
import com.leaf.rpc.ProxyFactory;
import com.leaf.rpc.DefaultProxyFactory;
import com.leaf.rpc.consumer.Consumer;
import com.leaf.rpc.consumer.DefaultConsumer;
import com.leaf.rpc.consumer.InvokeType;
Expand Down Expand Up @@ -36,7 +36,7 @@ public static void main(String[] args) {
}

private static void sync(Consumer consumer, UnresolvedAddress[] addresses) {
ClusterService clusterService = ProxyFactory.factory(ClusterService.class)
ClusterService clusterService = DefaultProxyFactory.factory(ClusterService.class)
.consumer(consumer)
.providers(addresses)
.strategy(ClusterInvoker.Strategy.FAIL_FAST)
Expand Down Expand Up @@ -69,7 +69,7 @@ private static void sync(Consumer consumer, UnresolvedAddress[] addresses) {
}

private static void async(Consumer consumer, UnresolvedAddress[] addresses) {
ClusterService clusterService = ProxyFactory.factory(ClusterService.class)
ClusterService clusterService = DefaultProxyFactory.factory(ClusterService.class)
.consumer(consumer)
.providers(addresses)
.invokeType(InvokeType.ASYNC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.leaf.common.UnresolvedAddress;
import com.leaf.example.cluster.api.ClusterService;
import com.leaf.rpc.ProxyFactory;
import com.leaf.rpc.DefaultProxyFactory;
import com.leaf.rpc.consumer.Consumer;
import com.leaf.rpc.consumer.DefaultConsumer;
import com.leaf.rpc.consumer.InvokeType;
Expand Down Expand Up @@ -36,7 +36,7 @@ public static void main(String[] args) {
}

private static void sync(Consumer consumer, UnresolvedAddress[] addresses) {
ClusterService clusterService = ProxyFactory.factory(ClusterService.class)
ClusterService clusterService = DefaultProxyFactory.factory(ClusterService.class)
.consumer(consumer)
.providers(addresses)
.strategy(ClusterInvoker.Strategy.FAIL_OVER)
Expand Down Expand Up @@ -67,7 +67,7 @@ private static void sync(Consumer consumer, UnresolvedAddress[] addresses) {
}

private static void async(Consumer consumer, UnresolvedAddress[] addresses) {
ClusterService clusterService = ProxyFactory.factory(ClusterService.class)
ClusterService clusterService = DefaultProxyFactory.factory(ClusterService.class)
.consumer(consumer)
.providers(addresses)
.invokeType(InvokeType.ASYNC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.leaf.common.UnresolvedAddress;
import com.leaf.example.cluster.api.ClusterService;
import com.leaf.rpc.ProxyFactory;
import com.leaf.rpc.DefaultProxyFactory;
import com.leaf.rpc.consumer.Consumer;
import com.leaf.rpc.consumer.DefaultConsumer;
import com.leaf.rpc.consumer.InvokeType;
Expand Down Expand Up @@ -38,7 +38,7 @@ public static void main(String[] args) {
}

private static void sync(Consumer consumer, UnresolvedAddress[] addresses) {
ClusterService clusterService = ProxyFactory.factory(ClusterService.class)
ClusterService clusterService = DefaultProxyFactory.factory(ClusterService.class)
.consumer(consumer)
.providers(addresses)
.strategy(ClusterInvoker.Strategy.FAIL_SAFE)
Expand Down Expand Up @@ -71,7 +71,7 @@ private static void sync(Consumer consumer, UnresolvedAddress[] addresses) {
}

private static void async(Consumer consumer, UnresolvedAddress[] addresses) {
ClusterService clusterService = ProxyFactory.factory(ClusterService.class)
ClusterService clusterService = DefaultProxyFactory.factory(ClusterService.class)
.consumer(consumer)
.providers(addresses)
.invokeType(InvokeType.ASYNC)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.leaf.example.demo.annotation;

import com.leaf.common.UnresolvedAddress;
import com.leaf.rpc.ProxyFactory;
import com.leaf.rpc.DefaultProxyFactory;
import com.leaf.rpc.consumer.Consumer;
import com.leaf.rpc.consumer.DefaultConsumer;

Expand All @@ -17,7 +17,7 @@ public class ConsumerExample {
consumer.connect(address);
consumer.connect(address);

userService = ProxyFactory.factory(UserService.class)
userService = DefaultProxyFactory.factory(UserService.class)
.consumer(consumer)
.providers(address)
.timeMillis(3000L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import com.leaf.common.model.ServiceMeta;
import com.leaf.example.demo.HelloService;
import com.leaf.remoting.netty.NettyClientConfig;
import com.leaf.rpc.ProxyFactory;
import com.leaf.rpc.DefaultProxyFactory;
import com.leaf.rpc.consumer.Consumer;
import com.leaf.rpc.consumer.DefaultConsumer;
import com.leaf.rpc.consumer.InvokeType;
import com.leaf.rpc.consumer.future.InvokeFuture;
import com.leaf.rpc.consumer.future.InvokeFutureContext;
import com.leaf.rpc.consumer.future.DefaultInvokeFuture;
import com.leaf.rpc.consumer.future.InvokeFutureListener;

public class ConsumerAsyncExample {
Expand All @@ -24,7 +23,7 @@ public static void main(String[] args) {
ServiceMeta serviceMeta = new ServiceMeta("test", "org.rpc.example.demo.HelloService", "1.0.0");
consumer.client().addChannelGroup(serviceMeta, address);

HelloService helloService = ProxyFactory.factory(HelloService.class)
HelloService helloService = DefaultProxyFactory.factory(HelloService.class)
.consumer(consumer)
.directory(serviceMeta)
.timeMillis(3000L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.leaf.common.model.ServiceMeta;
import com.leaf.example.demo.HelloService;
import com.leaf.remoting.netty.NettyClientConfig;
import com.leaf.rpc.ProxyFactory;
import com.leaf.rpc.DefaultProxyFactory;
import com.leaf.rpc.consumer.Consumer;
import com.leaf.rpc.consumer.DefaultConsumer;

Expand All @@ -24,7 +24,7 @@ public class ConsumerExample {
ServiceMeta serviceMeta = new ServiceMeta("test", "org.rpc.example.demo.HelloService", "1.0.0");
consumer.client().addChannelGroup(serviceMeta, address);

helloService = ProxyFactory.factory(HelloService.class)
helloService = DefaultProxyFactory.factory(HelloService.class)
.consumer(consumer)
.directory(serviceMeta)
.timeMillis(300000L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.leaf.common.model.ServiceMeta;
import com.leaf.example.demo.HelloService;
import com.leaf.remoting.netty.NettyClientConfig;
import com.leaf.rpc.ProxyFactory;
import com.leaf.rpc.DefaultProxyFactory;
import com.leaf.rpc.consumer.Consumer;
import com.leaf.rpc.consumer.DefaultConsumer;
import com.leaf.rpc.consumer.InvokeType;
Expand Down Expand Up @@ -35,7 +35,7 @@ public static void main(String[] args) {
consumer.client().addChannelGroup(serviceMeta, address);
}

HelloService helloService = ProxyFactory.factory(HelloService.class)
HelloService helloService = DefaultProxyFactory.factory(HelloService.class)
.consumer(consumer)
.directory(serviceMeta)
.timeMillis(3000L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.leaf.rpc.consumer.Consumer;
import com.leaf.rpc.consumer.DefaultConsumer;
import com.leaf.rpc.consumer.invoke.GenericInvoke;
import com.leaf.serialization.api.SerializerType;

public class ConsumerExample {

Expand All @@ -25,8 +26,19 @@ public static void main(String[] args) throws Throwable {
.timeMillis(3000L)
.newProxy();

String s = (String) genericInvoke.$invoke("sayHello"," biu biu biu!!!");
User user = new User();
user.setName("aaaa");
user.setAge("18");
String s = (String) genericInvoke.$invoke("sayHello", "yefei", "8");
System.out.printf("---------->: receive provider message %s \n", s);

GenericInvoke genericInvoke2 = GenericProxyFactory.factory()
.consumer(consumer)
.directory(serviceMeta)
.timeMillis(1L)
.newProxy();
String s2 = (String) genericInvoke2.$invoke("sayHello", "yefei", "18");
System.out.printf("---------->: receive provider message %s \n", s2);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
*/
public interface HelloService {

String sayHello(String name);
String sayHello(String name, String age);

String sayHello(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ public class HelloServiceImpl implements HelloService {
private final static Logger logger = LoggerFactory.getLogger(HelloServiceImpl.class);

@Override
public String sayHello(String name) {
logger.info("HelloServiceImpl param:{}", name);
public String sayHello(String name, String age) {
logger.info("HelloServiceImpl name:{}, age:{}", name, age);
return "hello" + name;
}

@Override
public String sayHello(User user) {
logger.info("HelloServiceImpl param:{}", user.getName());
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class ProviderExample {
public static void main(String[] args) {
NettyServerConfig config = new NettyServerConfig();
Provider provider = new DefaultProvider(config);

provider.start();
provider.registerGlobalFlowController();

Expand Down
24 changes: 24 additions & 0 deletions example/src/main/java/com/leaf/example/demo/generic/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.leaf.example.demo.generic;

public class User {

private String name;

private String age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}
}
Loading

0 comments on commit de8a95e

Please sign in to comment.