forked from hzy38324/simple-rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
9 changed files
with
291 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.sexycode.simple-rpc</groupId> | ||
<artifactId>simple-rpc</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<properties> | ||
<slf4jVersion>1.6.1</slf4jVersion> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>${slf4jVersion}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-log4j12</artifactId> | ||
<version>${slf4jVersion}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>log4j</groupId> | ||
<artifactId>log4j</artifactId> | ||
<version>1.2.16</version> | ||
<scope>runtime</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/sexycode/simplerpc/comsumer/app/ComsumerApp.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,22 @@ | ||
package com.sexycode.simplerpc.comsumer.app; | ||
|
||
import com.sexycode.simplerpc.comsumer.service.CalculatorRemoteImpl; | ||
import com.sexycode.simplerpc.provider.app.ProviderApp; | ||
import com.sexycode.simplerpc.provider.service.Calculator; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* <p> | ||
* | ||
* @author: hzy created on 2018/05/02 | ||
*/ | ||
public class ComsumerApp { | ||
private static Logger log = LoggerFactory.getLogger(ComsumerApp.class); | ||
|
||
public static void main(String[] args) { | ||
Calculator calculator = new CalculatorRemoteImpl(); | ||
int result = calculator.add(1, 2); | ||
log.info("result is {}", result); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/com/sexycode/simplerpc/comsumer/service/CalculatorRemoteImpl.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,75 @@ | ||
package com.sexycode.simplerpc.comsumer.service; | ||
|
||
import com.sexycode.simplerpc.provider.service.Calculator; | ||
import com.sexycode.simplerpc.reuqest.CalculateRpcRequest; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.InvalidObjectException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.net.Socket; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* <p> | ||
* | ||
* @author: hzy created on 2018/05/02 | ||
*/ | ||
public class CalculatorRemoteImpl implements Calculator { | ||
public static final int PORT = 9090; | ||
private static Logger log = LoggerFactory.getLogger(CalculatorRemoteImpl.class); | ||
|
||
public int add(int a, int b) { | ||
List<String> addressList = lookupProviders("Calculator.add"); | ||
String address = chooseTarget(addressList); | ||
try { | ||
Socket socket = new Socket(address, PORT); | ||
|
||
// 将请求序列化 | ||
CalculateRpcRequest calculateRpcRequest = generateRequest(a, b); | ||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream()); | ||
|
||
// 将请求发给服务提供方 | ||
objectOutputStream.writeObject(calculateRpcRequest); | ||
|
||
// 将响应体反序列化 | ||
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream()); | ||
Object response = objectInputStream.readObject(); | ||
|
||
log.info("response is {}", response); | ||
|
||
if (response instanceof Integer) { | ||
return (Integer) response; | ||
} else { | ||
throw new InternalError(); | ||
} | ||
|
||
} catch (Exception e) { | ||
log.error("fail", e); | ||
throw new InternalError(); | ||
} | ||
} | ||
|
||
private CalculateRpcRequest generateRequest(int a, int b) { | ||
CalculateRpcRequest calculateRpcRequest = new CalculateRpcRequest(); | ||
calculateRpcRequest.setA(a); | ||
calculateRpcRequest.setB(b); | ||
calculateRpcRequest.setMethod("add"); | ||
return calculateRpcRequest; | ||
} | ||
|
||
private String chooseTarget(List<String> providers) { | ||
if (null == providers || providers.size() == 0) { | ||
throw new IllegalArgumentException(); | ||
} | ||
return providers.get(0); | ||
} | ||
|
||
public static List<String> lookupProviders(String name) { | ||
List<String> strings = new ArrayList(); | ||
strings.add("127.0.0.1"); | ||
return strings; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/com/sexycode/simplerpc/provider/app/ProviderApp.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,66 @@ | ||
package com.sexycode.simplerpc.provider.app; | ||
|
||
import com.sexycode.simplerpc.provider.service.Calculator; | ||
import com.sexycode.simplerpc.provider.service.CalculatorImpl; | ||
import com.sexycode.simplerpc.reuqest.CalculateRpcRequest; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
|
||
/** | ||
* <p> | ||
* | ||
* @author: hzy created on 2018/05/02 | ||
*/ | ||
public class ProviderApp { | ||
private static Logger log = LoggerFactory.getLogger(ProviderApp.class); | ||
|
||
private Calculator calculator = new CalculatorImpl(); | ||
|
||
public static void main(String[] args) throws IOException { | ||
new ProviderApp().run(); | ||
} | ||
|
||
private void run() throws IOException { | ||
ServerSocket listener = new ServerSocket(9090); | ||
try { | ||
while (true) { | ||
Socket socket = listener.accept(); | ||
try { | ||
// 将请求反序列化 | ||
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream()); | ||
Object object = objectInputStream.readObject(); | ||
|
||
log.info("request is {}", object); | ||
|
||
// 调用服务 | ||
int result = 0; | ||
if (object instanceof CalculateRpcRequest) { | ||
CalculateRpcRequest calculateRpcRequest = (CalculateRpcRequest) object; | ||
if ("add".equals(calculateRpcRequest.getMethod())) { | ||
result = calculator.add(calculateRpcRequest.getA(), calculateRpcRequest.getB()); | ||
} else { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} | ||
|
||
// 返回结果 | ||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream()); | ||
objectOutputStream.writeObject(new Integer(result)); | ||
} catch (Exception e) { | ||
log.error("fail", e); | ||
} finally { | ||
socket.close(); | ||
} | ||
} | ||
} finally { | ||
listener.close(); | ||
} | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/sexycode/simplerpc/provider/service/Calculator.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,10 @@ | ||
package com.sexycode.simplerpc.provider.service; | ||
|
||
/** | ||
* <p> | ||
* | ||
* @author: hzy created on 2018/05/02 | ||
*/ | ||
public interface Calculator { | ||
int add(int a, int b); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/sexycode/simplerpc/provider/service/CalculatorImpl.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,12 @@ | ||
package com.sexycode.simplerpc.provider.service; | ||
|
||
/** | ||
* <p> | ||
* | ||
* @author: hzy created on 2018/05/02 | ||
*/ | ||
public class CalculatorImpl implements Calculator { | ||
public int add(int a, int b) { | ||
return a + b; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/sexycode/simplerpc/reuqest/CalculateRpcRequest.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,50 @@ | ||
package com.sexycode.simplerpc.reuqest; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* <p> | ||
* | ||
* @author: hzy created on 2018/05/02 | ||
*/ | ||
public class CalculateRpcRequest implements Serializable { | ||
|
||
private static final long serialVersionUID = 7503710091945320739L; | ||
|
||
private String method; | ||
private int a; | ||
private int b; | ||
|
||
public String getMethod() { | ||
return method; | ||
} | ||
|
||
public void setMethod(String method) { | ||
this.method = method; | ||
} | ||
|
||
public int getA() { | ||
return a; | ||
} | ||
|
||
public void setA(int a) { | ||
this.a = a; | ||
} | ||
|
||
public int getB() { | ||
return b; | ||
} | ||
|
||
public void setB(int b) { | ||
this.b = b; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "CalculateRpcRequest{" + | ||
"method='" + method + '\'' + | ||
", a=" + a + | ||
", b=" + b + | ||
'}'; | ||
} | ||
} |
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,9 @@ | ||
# Set root logger level to DEBUG and its only appender to A1. | ||
log4j.rootLogger=DEBUG, A1 | ||
|
||
# A1 is set to be a ConsoleAppender. | ||
log4j.appender.A1=org.apache.log4j.ConsoleAppender | ||
|
||
# A1 uses PatternLayout. | ||
log4j.appender.A1.layout=org.apache.log4j.PatternLayout | ||
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n |
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,9 @@ | ||
# Set root logger level to DEBUG and its only appender to A1. | ||
log4j.rootLogger=DEBUG, A1 | ||
|
||
# A1 is set to be a ConsoleAppender. | ||
log4j.appender.A1=org.apache.log4j.ConsoleAppender | ||
|
||
# A1 uses PatternLayout. | ||
log4j.appender.A1.layout=org.apache.log4j.PatternLayout | ||
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n |