-
Notifications
You must be signed in to change notification settings - Fork 192
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
1 parent
0a6d7ec
commit 8467b19
Showing
11 changed files
with
157 additions
and
45 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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
import java.io.Serializable; | ||
|
||
/** | ||
* 测试用api的实体 | ||
* @author ziyang | ||
*/ | ||
@Data | ||
|
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,6 +1,7 @@ | ||
package top.guoziyang.rpc.api; | ||
|
||
/** | ||
* 测试用api的接口 | ||
* @author ziyang | ||
*/ | ||
public interface HelloService { | ||
|
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
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
46 changes: 46 additions & 0 deletions
46
rpc-core/src/main/java/top/guoziyang/rpc/registry/DefaultServiceRegistry.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,46 @@ | ||
package top.guoziyang.rpc.registry; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import top.guoziyang.rpc.enumeration.RpcError; | ||
import top.guoziyang.rpc.exception.RpcException; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* 默认的服务注册表 | ||
* @author ziyang | ||
*/ | ||
public class DefaultServiceRegistry implements ServiceRegistry { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(DefaultServiceRegistry.class); | ||
|
||
private final Map<String, Object> serviceMap = new ConcurrentHashMap<>(); | ||
private final Set<String> registeredService = ConcurrentHashMap.newKeySet(); | ||
|
||
@Override | ||
public synchronized <T> void register(T service) { | ||
String serviceName = service.getClass().getCanonicalName(); | ||
if(registeredService.contains(serviceName)) return; | ||
registeredService.add(serviceName); | ||
Class<?>[] interfaces = service.getClass().getInterfaces(); | ||
if(interfaces.length == 0) { | ||
throw new RpcException(RpcError.SERVICE_NOT_IMPLEMENT_ANY_INTERFACE); | ||
} | ||
for(Class<?> i : interfaces) { | ||
serviceMap.put(i.getCanonicalName(), service); | ||
} | ||
logger.info("向接口: {} 注册服务: {}", interfaces, serviceName); | ||
} | ||
|
||
@Override | ||
public synchronized Object getService(String serviceName) { | ||
Object service = serviceMap.get(serviceName); | ||
if(service == null) { | ||
throw new RpcException(RpcError.SERVICE_NOT_FOUND); | ||
} | ||
return service; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
rpc-core/src/main/java/top/guoziyang/rpc/registry/ServiceRegistry.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,23 @@ | ||
package top.guoziyang.rpc.registry; | ||
|
||
/** | ||
* 服务注册表通用接口 | ||
* @author ziyang | ||
*/ | ||
public interface ServiceRegistry { | ||
|
||
/** | ||
* 将一个服务注册进注册表 | ||
* @param service 待注册的服务实体 | ||
* @param <T> 服务实体类 | ||
*/ | ||
<T> void register(T service); | ||
|
||
/** | ||
* 根据服务名称获取服务实体 | ||
* @param serviceName 服务名称 | ||
* @return 服务实体 | ||
*/ | ||
Object getService(String serviceName); | ||
|
||
} |
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
47 changes: 47 additions & 0 deletions
47
rpc-core/src/main/java/top/guoziyang/rpc/server/RequestHandlerThread.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,47 @@ | ||
package top.guoziyang.rpc.server; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import top.guoziyang.rpc.entity.RpcRequest; | ||
import top.guoziyang.rpc.entity.RpcResponse; | ||
import top.guoziyang.rpc.registry.ServiceRegistry; | ||
|
||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.net.Socket; | ||
|
||
/** | ||
* 处理RpcRequest的工作线程 | ||
* @author ziyang | ||
*/ | ||
public class RequestHandlerThread implements Runnable { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(RequestHandlerThread.class); | ||
|
||
private Socket socket; | ||
private RequestHandler requestHandler; | ||
private ServiceRegistry serviceRegistry; | ||
|
||
public RequestHandlerThread(Socket socket, RequestHandler requestHandler, ServiceRegistry serviceRegistry) { | ||
this.socket = socket; | ||
this.requestHandler = requestHandler; | ||
this.serviceRegistry = serviceRegistry; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try (ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream()); | ||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream())) { | ||
RpcRequest rpcRequest = (RpcRequest) objectInputStream.readObject(); | ||
String interfaceName = rpcRequest.getInterfaceName(); | ||
Object service = serviceRegistry.getService(interfaceName); | ||
Object result = requestHandler.handle(rpcRequest, service); | ||
objectOutputStream.writeObject(RpcResponse.success(result)); | ||
objectOutputStream.flush(); | ||
} catch (IOException | ClassNotFoundException e) { | ||
logger.error("调用或发送时有错误发生:", e); | ||
} | ||
} | ||
|
||
} |
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