-
Notifications
You must be signed in to change notification settings - Fork 1
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
12 changed files
with
247 additions
and
60 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
rpc-common/src/main/java/cn/fzzfrjf/annotation/Service.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,14 @@ | ||
package cn.fzzfrjf.annotation; | ||
|
||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Service { | ||
|
||
public String name() default ""; | ||
} |
14 changes: 14 additions & 0 deletions
14
rpc-common/src/main/java/cn/fzzfrjf/annotation/ServiceScan.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,14 @@ | ||
package cn.fzzfrjf.annotation; | ||
|
||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface ServiceScan { | ||
|
||
public String value() default ""; | ||
} |
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
99 changes: 99 additions & 0 deletions
99
rpc-common/src/main/java/cn/fzzfrjf/utils/ReflectUtils.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,99 @@ | ||
package cn.fzzfrjf.utils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.JarURLConnection; | ||
import java.net.URL; | ||
import java.net.URLDecoder; | ||
import java.util.Enumeration; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarFile; | ||
|
||
public class ReflectUtils { | ||
|
||
|
||
public static String getClassName(){ | ||
StackTraceElement[] stackTrace = new Throwable().getStackTrace(); | ||
String className = stackTrace[stackTrace.length - 1].getClassName(); | ||
return className; | ||
} | ||
|
||
|
||
public static Set<Class<?>> getClasses(String packageName){ | ||
Set<Class<?>> setClasses = new LinkedHashSet<>(); | ||
boolean recursive = true; | ||
String packageDirName = packageName.replace(".","/"); | ||
Enumeration<URL> dirs; | ||
try{ | ||
dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName); | ||
while(dirs.hasMoreElements()){ | ||
URL url = dirs.nextElement(); | ||
String protocol = url.getProtocol(); | ||
if("file".equals(protocol)){ | ||
String filePath = URLDecoder.decode(url.getFile(),"UTF-8"); | ||
findClassesInPackageByFile(packageName,filePath,recursive,setClasses); | ||
}else if("jar".equals(protocol)){ | ||
JarFile jarFile; | ||
try{ | ||
jarFile = ((JarURLConnection)url.openConnection()).getJarFile(); | ||
Enumeration<JarEntry> entries = jarFile.entries(); | ||
finaClassesInPackageByJar(packageName,entries,packageDirName,recursive,setClasses); | ||
}catch (IOException e){ | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
}catch (IOException e){ | ||
e.printStackTrace(); | ||
} | ||
return setClasses; | ||
} | ||
|
||
private static void findClassesInPackageByFile(String packageName,String packagePath,final boolean recursive,Set<Class<?>> set){ | ||
File dir = new File(packagePath); | ||
if(!dir.exists() || !dir.isDirectory()){ | ||
return; | ||
} | ||
File[] dirFiles = dir.listFiles(pathname -> (recursive || pathname.isDirectory()) || (pathname.getName().endsWith(".class"))); | ||
for(File file:dirFiles){ | ||
if(file.isDirectory()){ | ||
findClassesInPackageByFile(packageName + "." + file.getName(), file.getAbsolutePath(), recursive,set); | ||
}else{ | ||
String className = file.getName().substring(0,file.getName().length() - 6); | ||
try { | ||
set.add(Thread.currentThread().getContextClassLoader().loadClass(packageName + "." +className)); | ||
} catch (ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static void finaClassesInPackageByJar(String packageName, Enumeration<JarEntry> entries,String packageDirName , final boolean recursive,Set<Class<?>> set){ | ||
while(entries.hasMoreElements()){ | ||
JarEntry jarEntry = entries.nextElement(); | ||
String name = jarEntry.getName(); | ||
if(name.charAt(0) == '/'){ | ||
name = name.substring(1); | ||
} | ||
if(name.startsWith(packageDirName)){ | ||
int idx = name.lastIndexOf("/"); | ||
if(idx != -1){ | ||
packageName = name.substring(0,idx).replace("/","."); | ||
} | ||
if(idx != -1 || recursive){ | ||
if(name.endsWith(".class") && !jarEntry.isDirectory()){ | ||
String className = name.substring(packageName.length()+1,name.length() - 6); | ||
try { | ||
set.add(Thread.currentThread().getContextClassLoader().loadClass(packageName + "." + className)); | ||
} catch (ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
rpc-server/src/main/java/cn/fzzfrjf/core/AbstractServer.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,74 @@ | ||
package cn.fzzfrjf.core; | ||
|
||
import cn.fzzfrjf.annotation.Service; | ||
import cn.fzzfrjf.annotation.ServiceScan; | ||
import cn.fzzfrjf.enumeration.RpcError; | ||
import cn.fzzfrjf.exception.RpcException; | ||
import cn.fzzfrjf.service.RegisterService; | ||
import cn.fzzfrjf.service.ServerPublisher; | ||
import cn.fzzfrjf.utils.ReflectUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public abstract class AbstractServer implements CommonServer{ | ||
private static final Logger logger = LoggerFactory.getLogger(AbstractServer.class); | ||
|
||
protected ServerPublisher serverPublisher; | ||
protected RegisterService registerService; | ||
protected String host; | ||
protected int port; | ||
|
||
|
||
@Override | ||
public void scanServices(){ | ||
String mainClassName = ReflectUtils.getClassName(); | ||
Class<?> clazz; | ||
try{ | ||
clazz = Class.forName(mainClassName); | ||
if(!clazz.isAnnotationPresent(ServiceScan.class)){ | ||
logger.error("启动类缺少 @ServiceScan 注解"); | ||
throw new RpcException(RpcError.SERVICE_SCAN_ANNOTATION_NOT_FOUND); | ||
} | ||
} catch (ClassNotFoundException e) { | ||
logger.error("出现未知错误"); | ||
throw new RpcException(RpcError.RPC_UNKNOWN_ERROR); | ||
} | ||
String basePackage = clazz.getAnnotation(ServiceScan.class).value(); | ||
if("".equals(basePackage)){ | ||
basePackage = mainClassName.substring(0,mainClassName.lastIndexOf(".")); | ||
} | ||
Set<Class<?>> classes = ReflectUtils.getClasses(basePackage); | ||
for(Class<?> oneClass : classes){ | ||
if(oneClass.isAnnotationPresent(Service.class)) { | ||
Object service; | ||
try { | ||
service = oneClass.newInstance(); | ||
} catch (InstantiationException | IllegalAccessException e) { | ||
logger.error("创建" + oneClass + "时发生错误"); | ||
continue; | ||
} | ||
serverPublisher.addService(service); | ||
Class<?>[] interfaces = service.getClass().getInterfaces(); | ||
for (Class<?> anInterface : interfaces) { | ||
registerService.registry(anInterface.getCanonicalName(),new InetSocketAddress(host,port)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void publishService(List<Object> services) { | ||
for(Object service : services){ | ||
serverPublisher.addService(service); | ||
Class<?>[] interfaces = service.getClass().getInterfaces(); | ||
for (Class<?> anInterface : interfaces) { | ||
registerService.registry(anInterface.getCanonicalName(),new InetSocketAddress(host,port)); | ||
} | ||
} | ||
start(); | ||
} | ||
} |
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
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
14 changes: 14 additions & 0 deletions
14
test-server/src/main/java/cn/fzzfrjf/test/ByeServiceImpl.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,14 @@ | ||
package cn.fzzfrjf.test; | ||
|
||
import cn.fzzfrjf.annotation.Service; | ||
import cn.fzzfrjf.entity.ByeService; | ||
import cn.fzzfrjf.entity.RpcObject; | ||
|
||
|
||
@Service | ||
public class ByeServiceImpl implements ByeService { | ||
@Override | ||
public String bye(RpcObject object) { | ||
return "(" + object.getMessage() + "),bye!"; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
test-server/src/main/java/cn/fzzfrjf/test/HelloServiceImpl.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,14 @@ | ||
package cn.fzzfrjf.test; | ||
|
||
import cn.fzzfrjf.annotation.Service; | ||
import cn.fzzfrjf.entity.HelloService; | ||
import cn.fzzfrjf.entity.RpcObject; | ||
|
||
|
||
@Service | ||
public class HelloServiceImpl implements HelloService { | ||
@Override | ||
public String sayHello(RpcObject object) { | ||
return "这是id为:" + object.getId() + "发送的:" + object.getMessage(); | ||
} | ||
} |
19 changes: 5 additions & 14 deletions
19
test-server/src/main/java/cn/fzzfrjf/test/NettyServerTest.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,26 +1,17 @@ | ||
package cn.fzzfrjf.test; | ||
|
||
import cn.fzzfrjf.core.DefaultServerPublisher; | ||
|
||
import cn.fzzfrjf.annotation.ServiceScan; | ||
import cn.fzzfrjf.core.NettyServer; | ||
import cn.fzzfrjf.entity.ByeService; | ||
import cn.fzzfrjf.entity.HelloService; | ||
import cn.fzzfrjf.serializer.ProtobufSerializer; | ||
import cn.fzzfrjf.service.ByeServiceImpl; | ||
import cn.fzzfrjf.service.HelloServiceImpl; | ||
import cn.fzzfrjf.service.ServerPublisher; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
@ServiceScan | ||
public class NettyServerTest { | ||
|
||
public static void main(String[] args) { | ||
HelloService helloService = new HelloServiceImpl(); | ||
ByeService byeService = new ByeServiceImpl(); | ||
NettyServer server = new NettyServer(new ProtobufSerializer(),"127.0.0.1",9999); | ||
List<Object> services = new ArrayList<>(); | ||
services.add(helloService); | ||
services.add(byeService); | ||
server.publishService(services); | ||
server.start(); | ||
} | ||
} |