-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
anduo
committed
Apr 26, 2015
1 parent
574fd7e
commit ae85a31
Showing
63 changed files
with
599 additions
and
495 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,99 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>xyz.anduo</groupId> | ||
<artifactId>rpc</artifactId> | ||
<packaging>war</packaging> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>rpc Maven Webapp</name> | ||
<url>http://maven.apache.org</url> | ||
<dependencies> | ||
<!-- JUnit --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.11</version> | ||
<scope>test</scope> | ||
</dependency> | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>xyz.anduo</groupId> | ||
<artifactId>rpc</artifactId> | ||
<packaging>pom</packaging> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<modules> | ||
<module>rpc_client</module> | ||
<module>rpc_sieve</module> | ||
<module>rpc_common</module> | ||
<module>rpc_simple</module> | ||
</modules> | ||
<name>rpc</name> | ||
|
||
<!-- SLF4J --> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-log4j12</artifactId> | ||
<version>1.7.7</version> | ||
</dependency> | ||
<dependencyManagement> | ||
<dependencies> | ||
<!-- JUnit --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.11</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- Spring --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
<version>3.2.12.RELEASE</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-test</artifactId> | ||
<version>3.2.12.RELEASE</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<!-- SLF4J --> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-log4j12</artifactId> | ||
<version>1.7.7</version> | ||
</dependency> | ||
|
||
<!-- Netty --> | ||
<dependency> | ||
<groupId>io.netty</groupId> | ||
<artifactId>netty-all</artifactId> | ||
<version>4.0.24.Final</version> | ||
</dependency> | ||
<!-- Spring --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
<version>3.2.12.RELEASE</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-test</artifactId> | ||
<version>3.2.12.RELEASE</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- Protostuff --> | ||
<dependency> | ||
<groupId>com.dyuproject.protostuff</groupId> | ||
<artifactId>protostuff-core</artifactId> | ||
<version>1.0.8</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.dyuproject.protostuff</groupId> | ||
<artifactId>protostuff-runtime</artifactId> | ||
<version>1.0.8</version> | ||
</dependency> | ||
<!-- Netty --> | ||
<dependency> | ||
<groupId>io.netty</groupId> | ||
<artifactId>netty-all</artifactId> | ||
<version>4.0.24.Final</version> | ||
</dependency> | ||
|
||
<!-- ZooKeeper --> | ||
<dependency> | ||
<groupId>org.apache.zookeeper</groupId> | ||
<artifactId>zookeeper</artifactId> | ||
<version>3.4.6</version> | ||
</dependency> | ||
<!-- Protostuff --> | ||
<dependency> | ||
<groupId>com.dyuproject.protostuff</groupId> | ||
<artifactId>protostuff-core</artifactId> | ||
<version>1.0.8</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.dyuproject.protostuff</groupId> | ||
<artifactId>protostuff-runtime</artifactId> | ||
<version>1.0.8</version> | ||
</dependency> | ||
|
||
<!-- Apache Commons Collections --> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-collections4</artifactId> | ||
<version>4.0</version> | ||
</dependency> | ||
<!-- ZooKeeper --> | ||
<dependency> | ||
<groupId>org.apache.zookeeper</groupId> | ||
<artifactId>zookeeper</artifactId> | ||
<version>3.4.6</version> | ||
</dependency> | ||
|
||
<!-- Objenesis --> | ||
<dependency> | ||
<groupId>org.objenesis</groupId> | ||
<artifactId>objenesis</artifactId> | ||
<version>2.1</version> | ||
</dependency> | ||
<!-- Apache Commons Collections --> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-collections4</artifactId> | ||
<version>4.0</version> | ||
</dependency> | ||
|
||
<!-- CGLib --> | ||
<dependency> | ||
<groupId>cglib</groupId> | ||
<artifactId>cglib</artifactId> | ||
<version>3.1</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<finalName>rpc</finalName> | ||
</build> | ||
<!-- Objenesis --> | ||
<dependency> | ||
<groupId>org.objenesis</groupId> | ||
<artifactId>objenesis</artifactId> | ||
<version>2.1</version> | ||
</dependency> | ||
|
||
<!-- CGLib --> | ||
<dependency> | ||
<groupId>cglib</groupId> | ||
<artifactId>cglib</artifactId> | ||
<version>3.1</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</dependencyManagement> | ||
|
||
<build> | ||
<finalName>rpc</finalName> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
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>rpc</artifactId> | ||
<groupId>xyz.anduo</groupId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>rpc-client</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>xyz.anduo</groupId> | ||
<artifactId>rpc-common</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</dependency> | ||
<!-- SLF4J --> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-log4j12</artifactId> | ||
</dependency> | ||
|
||
<!-- Spring --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
</dependency> | ||
<!-- Netty --> | ||
<dependency> | ||
<groupId>io.netty</groupId> | ||
<artifactId>netty-all</artifactId> | ||
</dependency> | ||
|
||
<!-- Protostuff --> | ||
<dependency> | ||
<groupId>com.dyuproject.protostuff</groupId> | ||
<artifactId>protostuff-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.dyuproject.protostuff</groupId> | ||
<artifactId>protostuff-runtime</artifactId> | ||
</dependency> | ||
|
||
<!-- ZooKeeper --> | ||
<dependency> | ||
<groupId>org.apache.zookeeper</groupId> | ||
<artifactId>zookeeper</artifactId> | ||
</dependency> | ||
|
||
<!-- Apache Commons Collections --> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-collections4</artifactId> | ||
</dependency> | ||
|
||
<!-- Objenesis --> | ||
<dependency> | ||
<groupId>org.objenesis</groupId> | ||
<artifactId>objenesis</artifactId> | ||
</dependency> | ||
|
||
<!-- CGLib --> | ||
<dependency> | ||
<groupId>cglib</groupId> | ||
<artifactId>cglib</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
rpc_client/src/main/java/xyz/anduo/rpc/client/RpcProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package xyz.anduo.rpc.client; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.UUID; | ||
|
||
import net.sf.cglib.proxy.InvocationHandler; | ||
import net.sf.cglib.proxy.Proxy; | ||
|
||
public class RpcProxy { | ||
private String serverAddress; | ||
private ServiceDiscovery serviceDiscovery; | ||
|
||
public RpcProxy(String serverAddress) { | ||
this.serverAddress = serverAddress; | ||
} | ||
|
||
public RpcProxy(ServiceDiscovery serviceDiscovery) { | ||
this.serviceDiscovery = serviceDiscovery; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T> T create(Class<?> interfaceClass) { | ||
return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[]{interfaceClass}, | ||
new InvocationHandler() { | ||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
RpcRequest request = new RpcRequest(); // 创建并初始化 RPC 请求 | ||
request.setRequestId(UUID.randomUUID().toString()); | ||
request.setClassName(method.getDeclaringClass().getName()); | ||
request.setMethodName(method.getName()); | ||
request.setParameterTypes(method.getParameterTypes()); | ||
request.setParameters(args); | ||
|
||
if (serviceDiscovery != null) { | ||
serverAddress = serviceDiscovery.discover(); // 发现服务 | ||
} | ||
String[] array = serverAddress.split(":"); | ||
String host = array[0]; | ||
int port = Integer.parseInt(array[1]); | ||
|
||
RpcClient client = new RpcClient(host, port); // 初始化 RPC 客户端 | ||
RpcResponse response = client.send(request); // 通过 RPC客户端发送RPC请求并获取RPC响应 | ||
if (response.isError()) { | ||
throw response.getError(); | ||
} else { | ||
return response.getResult(); | ||
} | ||
} | ||
}); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...java/xyz/anduo/rpc/server/RpcRequest.java → ...java/xyz/anduo/rpc/client/RpcRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package xyz.anduo.rpc.server; | ||
package xyz.anduo.rpc.client; | ||
|
||
public class RpcRequest { | ||
private String requestId; | ||
|
2 changes: 1 addition & 1 deletion
2
...ava/xyz/anduo/rpc/server/RpcResponse.java → ...ava/xyz/anduo/rpc/client/RpcResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd | ||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> | ||
|
||
<context:property-placeholder location="classpath:client-config.properties"/> | ||
|
||
<!-- 配置服务发现组件 --> | ||
<bean id="serviceDiscovery" class="xyz.anduo.rpc.client.ServiceDiscovery"> | ||
<constructor-arg name="registryAddress" value="${registry.address}"/> | ||
</bean> | ||
|
||
<!-- 配置 RPC 代理 --> | ||
<bean id="rpcProxy" class="xyz.anduo.rpc.client.RpcProxy"> | ||
<constructor-arg name="serviceDiscovery" ref="serviceDiscovery"/> | ||
</bean> | ||
</beans> |
Oops, something went wrong.