Skip to content

Commit

Permalink
修复重载问题
Browse files Browse the repository at this point in the history
  • Loading branch information
yefei committed May 25, 2020
1 parent 0c3ed53 commit 3be6488
Show file tree
Hide file tree
Showing 39 changed files with 319 additions and 135 deletions.
67 changes: 66 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,73 @@
+ 负载均衡算法:加权轮询、加权随机
+ 注解配置


#### 示例:
##### 一个最简单的例子
##### 1、provider
```` java
public interface HelloService {

String sayHello(String name);

String sayHello(String name, String age);

String sayHello(User user);
}

public class HelloServiceImpl implements HelloService {

/**
* logger
*/
private final static Logger logger = LoggerFactory.getLogger(HelloServiceImpl.class);

@Override
public String sayHello(String name) {
return "hello" + name;
}

@Override
public String sayHello(String name, String age) {
return "hello:" + name + ",age:" + age;
}

@Override
public String sayHello(User user) {
logger.info("HelloServiceImpl param:{}", user.getName());
return "HelloServiceImpl param: " + user.getName();
}
}

public class ProviderExample {

public static void main(String[] args) {
LeafServer leafServer = new DefaultLeafServer(9180);
leafServer.start();

leafServer.serviceRegistry()
.provider(new HelloServiceImpl())
.interfaceClass(HelloService.class)
.register();
}
}
````

#### 2、consumer
```` java
public class ConsumerExample {

public static void main(String[] args) {
LeafClient leafClient = new DefaultLeafClient("consumer");

HelloService helloService = DefaultProxyFactory.factory(HelloService.class)
.consumer(leafClient)
.providers(new UnresolvedAddress("127.0.0.1", 9180))
.newProxy();
System.out.println(helloService.sayHello("i'm king", "119"));
System.out.println(helloService.sayHello(new User("达维安爵士", "108")));
}
}
````
##### 具体可参考example模块
##### 集成spring示例

Expand Down
10 changes: 9 additions & 1 deletion common/src/main/java/com/leaf/common/utils/Reflects.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ public static Object Invoke(Object obj, String methodName, Object[] args) {
METHODACCESS_CACHE.remove(aClass);
invoker = MethodAccess.get(aClass);
}
return invoker.invoke(obj, methodName, args);
if (Collections.isNotEmpty(args)) {
Class[] paramTypes = new Class[args.length];
for (int i = 0; i < args.length; i++) {
paramTypes[i] = args[i].getClass();
}
return invoker.invoke(obj, methodName, paramTypes, args);
} else {
return invoker.invoke(obj, methodName, args);
}
}

public static Object getTypeDefaultValue(Class<?> clazz) {
Expand Down
5 changes: 2 additions & 3 deletions console/src/main/java/com/leaf/console/config/Config.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.leaf.console.config;

import com.leaf.common.concurrent.ConcurrentSet;
import com.leaf.common.model.ServiceMeta;
import com.leaf.register.api.*;
import com.leaf.register.api.model.RegisterMeta;
Expand Down Expand Up @@ -41,7 +40,7 @@ public void notify(SubscribeMeta subscribeMeta, NotifyEvent event) {
}
}
if (event == NotifyEvent.ADD) {
subscribeMetas.add(subscribeMeta);
subscribeMetas.addIfAbsent(subscribeMeta);
} else {
subscribeMetas.remove(subscribeMeta);
}
Expand All @@ -61,7 +60,7 @@ public void notify(RegisterMeta registerMeta, NotifyEvent event) {
}
}
if (event == NotifyEvent.ADD) {
registerMetas.add(registerMeta);
registerMetas.addIfAbsent(registerMeta);
} else {
registerMetas.remove(registerMeta);
}
Expand Down
2 changes: 1 addition & 1 deletion console/src/main/resources/static/echarts8.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html class="x-admin-sm">
<head>
<meta charset="UTF-8">
<title>后台登录-X-admin2.2</title>
<title>console</title>
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
Expand Down
9 changes: 1 addition & 8 deletions console/src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<!-- 顶部开始 -->
<div class="container">
<div class="logo">
<a href="./index.html">X-admin v2.2</a></div>
<a href="./index.html">console</a></div>
<div class="left_open">
<a><i title="展开左侧栏" class="iconfont">&#xe699;</i></a>
</div>
Expand Down Expand Up @@ -127,13 +127,6 @@
<style id="theme_style"></style>
<!-- 右侧主体结束 -->
<!-- 中部结束 -->
<script>//百度统计可去掉
var _hmt = _hmt || []; (function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();</script>
</body>

</html>
5 changes: 5 additions & 0 deletions example/src/main/java/com/leaf/example/demo/HelloService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
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
@@ -1,6 +1,5 @@
package com.leaf.example.demo;

import com.leaf.common.context.RpcContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -19,4 +18,15 @@ public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
return "hello" + name;
}

@Override
public String sayHello(String name, String age) {
return "hello:" + name + ",age:" + age;
}

@Override
public String sayHello(User user) {
logger.info("HelloServiceImpl param:{}", user.getName());
return "HelloServiceImpl param: " + user.getName();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package com.leaf.example.demo.generic;
package com.leaf.example.demo;

public class User {

private String name;

private String age;

public User() {
}

public User(String name, String age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}
Expand All @@ -21,4 +29,4 @@ public String getAge() {
public void setAge(String age) {
this.age = age;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,24 @@

import com.leaf.common.UnresolvedAddress;
import com.leaf.common.context.RpcContext;
import com.leaf.common.model.ServiceMeta;
import com.leaf.example.demo.HelloService;
import com.leaf.remoting.netty.NettyClientConfig;
import com.leaf.rpc.DefaultProxyFactory;
import com.leaf.rpc.consumer.LeafClient;
import com.leaf.rpc.consumer.DefaultLeafClient;
import com.leaf.rpc.consumer.InvokeType;
import com.leaf.rpc.consumer.LeafClient;
import com.leaf.rpc.consumer.future.InvokeFuture;
import com.leaf.rpc.consumer.future.InvokeFutureContext;
import com.leaf.rpc.consumer.future.InvokeFutureListener;

public class ConsumerAsyncExample {

public static void main(String[] args) {
NettyClientConfig config = new NettyClientConfig();
LeafClient leafClient = new DefaultLeafClient("consumer", config);
UnresolvedAddress address = new UnresolvedAddress("127.0.0.1", 9180);
leafClient.connect(address);

ServiceMeta serviceMeta = new ServiceMeta("test", "org.rpc.example.demo.HelloService", "1.0.0");
leafClient.client().addChannelGroup(serviceMeta, address);
LeafClient leafClient = new DefaultLeafClient("consumer");

HelloService helloService = DefaultProxyFactory.factory(HelloService.class)
.consumer(leafClient)
.directory(serviceMeta)
.providers(new UnresolvedAddress("127.0.0.1", 9180))
.group("test1")
.timeMillis(3000L)
.invokeType(InvokeType.ASYNC)
.newProxy();
Expand All @@ -52,6 +45,6 @@ public void failure(Throwable cause) {
} catch (Throwable throwable) {
throwable.printStackTrace();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ConsumerExample {
leafClient.connect(address);

ServiceMeta serviceMeta = new ServiceMeta("test", "org.rpc.example.demo.HelloService", "1.0.0");
leafClient.client().addChannelGroup(serviceMeta, address);
leafClient.remotingClient().addChannelGroup(serviceMeta, address);

helloService = DefaultProxyFactory.factory(HelloService.class)
.consumer(leafClient)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
package com.leaf.example.demo.basic;

import com.leaf.common.UnresolvedAddress;
import com.leaf.common.model.ServiceMeta;
import com.leaf.example.demo.HelloService;
import com.leaf.rpc.DefaultProxyFactory;
import com.leaf.rpc.consumer.LeafClient;
import com.leaf.rpc.consumer.DefaultLeafClient;
import com.leaf.rpc.consumer.LeafClient;

public class ConsumerExample {

private static HelloService helloService;

static {
public static void main(String[] args) {
LeafClient leafClient = new DefaultLeafClient("consumer");
UnresolvedAddress address = new UnresolvedAddress("127.0.0.1", 9180);
leafClient.connect(address);
leafClient.connect(address);
leafClient.connect(address);
leafClient.connect(address);

ServiceMeta serviceMeta = new ServiceMeta("test", "org.rpc.example.demo.HelloService", "1.0.0");
leafClient.client().addChannelGroup(serviceMeta, address);

helloService = DefaultProxyFactory.factory(HelloService.class)
.consumer(leafClient)
.directory(serviceMeta)
.timeMillis(300000L)
.providers(new UnresolvedAddress("127.0.0.1", 9180))
.group("test")
.version("1.0.0")
.timeMillis(3000L)
.newProxy();
}

public static void main(String[] args) {
new ConsumerExample().invoke();
}

public void invoke() {
helloService.sayHello(" biu biu biu!!!");
String s = helloService.sayHello(" biu biu biu!!!");
System.out.println(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public static void main(String[] args) {
ServiceWrapper serviceWrapper = leafServer.serviceRegistry()
.provider(helloService)
.interfaceClass(HelloService.class)
.providerName("org.rpc.example.demo.HelloService")
.group("test")
.version("1.0.0")
.register();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static void main(String[] args) {

for (UnresolvedAddress address : addresses) {
leafClient.connect(address);
leafClient.client().addChannelGroup(serviceMeta, address);
leafClient.remotingClient().addChannelGroup(serviceMeta, address);
}

HelloService helloService = DefaultProxyFactory.factory(HelloService.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@

import com.leaf.common.UnresolvedAddress;
import com.leaf.common.model.ServiceMeta;
import com.leaf.remoting.netty.NettyClientConfig;
import com.leaf.example.demo.User;
import com.leaf.rpc.GenericProxyFactory;
import com.leaf.rpc.consumer.LeafClient;
import com.leaf.rpc.consumer.DefaultLeafClient;
import com.leaf.rpc.consumer.LeafClient;
import com.leaf.rpc.consumer.invoke.GenericInvoke;

public class ConsumerExample {

public static void main(String[] args) throws Throwable {
NettyClientConfig config = new NettyClientConfig();
LeafClient leafClient = new DefaultLeafClient("consumer", config);
UnresolvedAddress address = new UnresolvedAddress("127.0.0.1", 9180);
leafClient.connect(address);
LeafClient leafClient = new DefaultLeafClient("consumer");

ServiceMeta serviceMeta = new ServiceMeta("test", "org.rpc.example.demo.HelloService", "1.0.0");
leafClient.client().addChannelGroup(serviceMeta, address);
ServiceMeta serviceMeta = new ServiceMeta("leaf", "com.leaf.example.demo.HelloService", "1.0.0");

GenericInvoke genericInvoke = GenericProxyFactory.factory()
.consumer(leafClient)
.providers(new UnresolvedAddress("127.0.0.1", 9180))
.directory(serviceMeta)
.timeMillis(3000L)
.newProxy();
Expand All @@ -34,9 +31,9 @@ public static void main(String[] args) throws Throwable {
GenericInvoke genericInvoke2 = GenericProxyFactory.factory()
.consumer(leafClient)
.directory(serviceMeta)
.timeMillis(1L)
.timeMillis(100L)
.newProxy();
String s2 = (String) genericInvoke2.$invoke("sayHello", "yefei", "18");
String s2 = (String) genericInvoke2.$invoke("sayHello", new User("yefei", "18"));
System.out.printf("---------->: receive provider message %s \n", s2);

}
Expand Down

This file was deleted.

Loading

0 comments on commit 3be6488

Please sign in to comment.