spring-boot-protocol
spring-boot-protocol copied to clipboard
split to core module plus protocol modules
Great job! Is it possible to split the code into core module plus protocol modules (servlet, mqtt...) and auto loading the protocol modules when application startup depending on protocol modules dependency involved.
And is there embeded support for http file server besides servlet.
可能是文档没写清楚,我刚才补充了一下文档, 你看下这个链接刚补充的文档。
还有/src/test目录下也有代码示例, 有上传下载文件, mqtt等的使用方法。
目前除了http,nrpc。 其他mqtt或其他协议都是默认不开启的。
你好, 子豪: 我想了解的是否有 HTML file request 支持,而不仅仅是 servlet. 例如: http://server_address/.../xxx.html 现在有内置支持功能吗? 还是需要添加一个类似 FileRequestDispatcher 的功能,并Override ServletContext. getRequestDispatcher。
还有返回的生成数据,是在哪个地方被 ctx.write 的。
我可以返回一个数据list,让 ctx 可以逐个 write吗?
另外返回的数据可以是个 Consumer
从 Windows 版邮件https://go.microsoft.com/fwlink/?LinkId=550986发送
发件人: @.> 发送时间: 2022年4月10日 11:32 收件人: @.> 抄送: Xingjian @.>; @.> 主题: Re: [wangzihaogithub/spring-boot-protocol] split to core module plus protocol modules (Issue #16)
可能是文档没写清楚,我刚才补充了一下文档, 你看下这个链接刚补充的文档。
还有/src/test目录下也有代码示例, 有上传下载文件, mqtt等的使用方法。
目前除了http,nrpc。 其他mqtt或其他协议都是默认不开启的。
https://github.com/wangzihaogithub/spring-boot-protocol/wiki#%E7%A4%BA%E4%BE%8B1-springboot%E7%89%88%E4%BD%BF%E7%94%A8http%E6%88%96websocket%E6%A8%A1%E5%9D%97%E4%BD%BF%E7%94%A8springboot%E5%90%8E%E9%BB%98%E8%AE%A4%E6%98%AF%E5%BC%80%E5%90%AFhttp%E7%9A%84
― Reply to this email directly, view it on GitHubhttps://github.com/wangzihaogithub/spring-boot-protocol/issues/16#issuecomment-1094167920, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAH3LDHCSRQQFHWBL7OY6A3VEJDWPANCNFSM5TACC4XQ. You are receiving this because you authored the thread.Message ID: @.***>
下面这个问题, 这样用.
我可以返回一个数据list,让 ctx 可以逐个 write吗?
另外返回的数据可以是个 Consumer
,
try(NettyOutputStream outputStream = (NettyOutputStream)response.getOutputStream()){
outputStream.write(mappedByteBuffer);
outputStream.write(mappedByteBuffer);
outputStream.write(mappedByteBuffer);
}
`package com.github.netty.protocol.servlet;
/**
* use netty zero copy
* if you need flush {@link ServletOutputStream#flush()}
* @author wangzihaogithub
* 2020-06-07 14:13:36
*/
public interface NettyOutputStream extends Flushable, Closeable {
/**
* batch send packet. Immediately other side peer receive write finish data
* Try not to use this method, it is possible for maximum performance.
* Unless you need to need to get to the end part of the data received immediately
* @throws IOException if close
*/
@Override
void flush() throws IOException;
/**
* close http.
* if keepAlive. It does not turn off tcpConnection. Otherwise, turn off tcpConnection
*/
@Override
void close();
/**
* direct write to tcp outputStream.
* @param httpBody jdk ByteBuffer httpBody
* @see MappedByteBuffer
* @see ByteBuffer
* @return ChannelProgressivePromise
* @throws IOException if close
*/
ChannelProgressivePromise write(ByteBuffer httpBody) throws IOException;
/**
* direct write to tcp outputStream
* @param httpBody netty ByteBuf httpBody
* @see ChunkedFile
* @see ChunkedNioStream
* @see ChunkedNioFile
* @see ChunkedStream
* @return ChannelProgressivePromise
* @throws IOException if close
*/
ChannelProgressivePromise write(ByteBuf httpBody) throws IOException;
/**
* use netty batch write
* @param httpBody ChunkedInput httpBody
* @see ChunkedFile
* @see ChunkedNioStream
* @see ChunkedNioFile
* @see ChunkedStream
* @return ChannelProgressivePromise
* @throws IOException if close
*/
ChannelProgressivePromise write(ChunkedInput httpBody) throws IOException;
/**
* use netty zero copy
* @param httpBody FileChannel httpBody
* @param count count
* @param position position
* @return ChannelProgressivePromise {@link ChannelProgressivePromise#addListener(GenericFutureListener)} }
* @see GenericProgressiveFutureListener
* @throws IOException if close
*/
ChannelProgressivePromise write(FileChannel httpBody, long position, long count) throws IOException;
/**
* use netty zero copy
* @param httpBody File httpBody
* @param count count
* @param position position
* @return ChannelProgressivePromise {@link ChannelProgressivePromise#addListener(GenericFutureListener)} }
* @see GenericProgressiveFutureListener
* @throws IOException if close
*/
ChannelProgressivePromise write(File httpBody, long position, long count) throws IOException;
/**
* use netty zero copy
* @param httpBody File httpBody
* @return ChannelProgressivePromise {@link ChannelProgressivePromise#addListener(GenericFutureListener)} }
* @see GenericProgressiveFutureListener
* @throws IOException if close
*/
ChannelProgressivePromise write(File httpBody) throws IOException;
}
`
这个问题, springboot本身就支持. spring怎么用,这里就怎么用. spring的用法是, 在resources包下, 建立静态目录, 访问目录下的文件.index.html即可, 不需要多余的配置
HTML file request 支持,而不仅仅是 servlet.
下面这个问题. 这个是spring处理完后, 会帮你序列化, 然后response.getOutputStream().write(byte[]), 因为spring默认实现了一个servlet.
还有返回的生成数据,是在哪个地方被 ctx.write 的。
这个静态文件的内置功能, 目前没有, 但是spring有, 如果不想用spring, 可以手工写一个或者网上copy一个 FileServlet, 即可
比如 处理所有 /static/ 目录下的静态文件, 可以加一个这样的servlet
我想了解的是否有 HTML file request 支持,而不仅仅是 servlet. 例如: http://server_address/.../xxx.html 现在有内置支持功能吗?
Hi, Zihao,
我尝试采用 apache/Catalina 的 DefaultServlet., 发现当前实现中 ServletHttpServletRequest 的 getPathInfo 没有实现。
ServletHttpServletRequest与 catalina 的 Request 都是 HttpServletRequest接口的实现。
ServletHttpServletRequest 在对 Netty 特性支持上有什么特别之处吗?
Xingjian
从 Windows 版邮件https://go.microsoft.com/fwlink/?LinkId=550986发送
发件人: @.> 发送时间: 2022年4月11日 12:00 收件人: @.> 抄送: Xingjian @.>; @.> 主题: Re: [wangzihaogithub/spring-boot-protocol] split to core module plus protocol modules (Issue #16)
这个静态文件的内置功能, 目前没有, 但是spring有, 如果不想用spring, 可以手工写一个或者网上copy一个 FileServlet, 即可
我想了解的是否有 HTML file request 支持,而不仅仅是 servlet. 例如: http://server_address/.../xxx.html 现在有内置支持功能吗?
― Reply to this email directly, view it on GitHubhttps://github.com/wangzihaogithub/spring-boot-protocol/issues/16#issuecomment-1094518019, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAH3LDC3H2HNZAD2VUPOFDTVEOPWTANCNFSM5TACC4XQ. You are receiving this because you authored the thread.Message ID: @.***>
没有特别之处, 这个方法会影响到使用吗? 我可以补上
我刚发布了2.2.4版本, 加了HttpServletRequest#getPathInfo方法实现. 应该可以满足你的要求. 现在除了request.login 系列的接口, 其他的都与tomcat保持一致了
发布2.2.4 1.增加HttpServletRequest#getPathInfo方法实现 2.修复springboot访问localhost时, 没有访问到index.html的问题 3.修复servlet异步时,客户端强制abort中止后,使用request对象会报空指针的bug. 4.修复forward,async,include时.获取requestURI路径不正确的bug fix https://github.com/wangzihaogithub/spring-boot-protocol/issues/16
Hi, Zihao,
I try DefaultServlet @ tomcat-embed-core @ 9.0.62
First the request changed with more elements /index.html/;jsessionid=node01no75msitjo427u0tsup0ae0o0.node0/;jsessionid=node01no75msitjo427u0tsup0ae0o0.node0/;jsessionid=node01no75msitjo427u0tsup0ae0o0.node0/;jsessionid=node01no75msitjo427u0tsup0ae0o0.node0/;jsessionid=node01no75msitjo427u0tsup0ae0o0.node0/;jsessionid=node01no75msitjo427u0tsup0ae0o0.node0/;jsessionid=node01no75msitjo427u0tsup0ae0o0.node0/;jsessionid=node01no75msitjo427u0tsup0ae0o0.node0
And when request.getServletPath(); The return is “”, but not /index.html Which is called by DefaultServlet when getRelativePath Without right relativePath, DefaultServlet can not find the file resource.
My init code like this: private static HttpServletProtocol newHttpProtocol() { ServletContext servletContext = new ServletContext(); servletContext.addServlet("testServlet", new TestServlet()).addMapping("/test");
servletContext.addServlet("fileServlet", new DefaultServlet()).addMapping("/");
return new HttpServletProtocol(servletContext);
}
Is there test case for this, and better to build a native DeaultServlet instead of Tomcat DefaltServelt.
Thanks.
从 Windows 版邮件https://go.microsoft.com/fwlink/?LinkId=550986发送
发件人: @.> 发送时间: 2022年4月13日 20:10 收件人: @.> 抄送: Xingjian @.>; @.> 主题: Re: [wangzihaogithub/spring-boot-protocol] split to core module plus protocol modules (Issue #16)
我刚发布了2.2.4版本, 加了HttpServletRequest#getPathInfo方法实现. 应该可以满足你的要求. 现在除了request.login 系列的接口, 其他的都与tomcat保持一致了
发布2.2.4 1.增加HttpServletRequest#getPathInfo方法实现 2.修复springboot访问localhost时, 没有访问到index.html的问题 3.修复servlet异步时,客户端强制abort中止后,使用request对象会报空指针的bug. 4.修复forward,async,include时.获取requestURI路径不正确的bug fix #16https://github.com/wangzihaogithub/spring-boot-protocol/issues/16
― Reply to this email directly, view it on GitHubhttps://github.com/wangzihaogithub/spring-boot-protocol/issues/16#issuecomment-1097976344, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAH3LDGSOACDUNWBHYGSJ2TVE22UPANCNFSM5TACC4XQ. You are receiving this because you authored the thread.Message ID: @.***>
我明白了你的意图, 改好后告诉你.
I understand what you're trying to do. I'll let you know when I fix it
我刚发布了2.2.5版本, 应该过1~2天就自动同步到maven中央仓库了.
代码这样写就可以了.
`
public static void main(String[] args) {
StartupServer server = new StartupServer(80);
server.addProtocol(newHttpProtocol());
server.start();
}
private static HttpServletProtocol newHttpProtocol() {
ServletContext servletContext = new ServletContext();
servletContext.setDocBase("D://demo", "/webapp"); // 静态资源文件夹
servletContext.addServlet("myHttpServlet", new com.github.netty.protocol.servlet.DefaultServlet())
.addMapping("/*");
return new HttpServletProtocol(servletContext);
}
`
发布2.2.5 1.实现默认的文件Servlet服务,com.github.netty.protocol.servlet.DefaultServlet
Hi, Zihao,
Fantastic, the html page is show correctly with css/js;
There was a implemented code for rpc/netty-message, how can I integrate the code into the protocols. Is there a demo for this?
Thanks;
从 Windows 版邮件https://go.microsoft.com/fwlink/?LinkId=550986发送
发件人: @.> 发送时间: 2022年4月14日 14:10 收件人: @.> 抄送: Xingjian @.>; @.> 主题: Re: [wangzihaogithub/spring-boot-protocol] split to core module plus protocol modules (Issue #16)
我刚发布了2.2.5版本, 应该过1~2天就自动同步到maven中央仓库了.
代码这样写就可以了.
public static void main(String[] args) { StartupServer server = new StartupServer(80); server.addProtocol(newHttpProtocol()); server.start(); } private static HttpServletProtocol newHttpProtocol() { ServletContext servletContext = new ServletContext(); servletContext.setDocBase("D://demo", "/webapp"); // 静态资源文件夹 servletContext.addServlet("myHttpServlet", new com.github.netty.protocol.servlet.DefaultServlet()) .addMapping("/*"); return new HttpServletProtocol(servletContext); }
发布2.2.5 1.实现默认的文件Servlet服务,com.github.netty.protocol.servlet.DefaultServlet
― Reply to this email directly, view it on GitHubhttps://github.com/wangzihaogithub/spring-boot-protocol/issues/16#issuecomment-1098743796, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAH3LDC473TWJHPLNKW3TRLVE6ZFZANCNFSM5TACC4XQ. You are receiving this because you authored the thread.Message ID: @.***>
Hi, Zihao,
I notice that you use “com.github.netty” for code base package, and “com.github.netty” for artifactId. Is it a long term usage?
从 Windows 版邮件https://go.microsoft.com/fwlink/?LinkId=550986发送
发件人: @.> 发送时间: 2022年4月14日 14:10 收件人: @.> 抄送: Xingjian @.>; @.> 主题: Re: [wangzihaogithub/spring-boot-protocol] split to core module plus protocol modules (Issue #16)
我刚发布了2.2.5版本, 应该过1~2天就自动同步到maven中央仓库了.
代码这样写就可以了.
public static void main(String[] args) { StartupServer server = new StartupServer(80); server.addProtocol(newHttpProtocol()); server.start(); } private static HttpServletProtocol newHttpProtocol() { ServletContext servletContext = new ServletContext(); servletContext.setDocBase("D://demo", "/webapp"); // 静态资源文件夹 servletContext.addServlet("myHttpServlet", new com.github.netty.protocol.servlet.DefaultServlet()) .addMapping("/*"); return new HttpServletProtocol(servletContext); }
发布2.2.5 1.实现默认的文件Servlet服务,com.github.netty.protocol.servlet.DefaultServlet
― Reply to this email directly, view it on GitHubhttps://github.com/wangzihaogithub/spring-boot-protocol/issues/16#issuecomment-1098743796, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAH3LDC473TWJHPLNKW3TRLVE6ZFZANCNFSM5TACC4XQ. You are receiving this because you authored the thread.Message ID: @.***>
Hi, Zihao,
I notice that you use “com.github.netty” for code base package, and “com.github.netty” for artifactId. Is it a long term usage?
The issue is for above java9, the package name can not duplicate btw different modules.
从 Windows 版邮件https://go.microsoft.com/fwlink/?LinkId=550986发送
发件人: @.> 发送时间: 2022年4月14日 14:10 收件人: @.> 抄送: Xingjian @.>; @.> 主题: Re: [wangzihaogithub/spring-boot-protocol] split to core module plus protocol modules (Issue #16)
我刚发布了2.2.5版本, 应该过1~2天就自动同步到maven中央仓库了.
代码这样写就可以了.
public static void main(String[] args) { StartupServer server = new StartupServer(80); server.addProtocol(newHttpProtocol()); server.start(); } private static HttpServletProtocol newHttpProtocol() { ServletContext servletContext = new ServletContext(); servletContext.setDocBase("D://demo", "/webapp"); // 静态资源文件夹 servletContext.addServlet("myHttpServlet", new com.github.netty.protocol.servlet.DefaultServlet()) .addMapping("/*"); return new HttpServletProtocol(servletContext); }
发布2.2.5 1.实现默认的文件Servlet服务,com.github.netty.protocol.servlet.DefaultServlet
― Reply to this email directly, view it on GitHubhttps://github.com/wangzihaogithub/spring-boot-protocol/issues/16#issuecomment-1098743796, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAH3LDC473TWJHPLNKW3TRLVE6ZFZANCNFSM5TACC4XQ. You are receiving this because you authored the thread.Message ID: @.***>
rpc/netty-message demo
`
package com.github.netty.javanrpc.server;
// server demo
public class RpcServerApplication {
public static void main(String[] args) {
StartupServer server = new StartupServer(80);
server.addProtocol(newHttpProtocol());
server.addProtocol(newRpcMessageProtocol());
server.start();
}
private static NRpcProtocol newRpcMessageProtocol() {
ApplicationX applicationX = new ApplicationX();
applicationX.scanner(true,"com.github.netty.javanrpc.server")
.inject();
return new NRpcProtocol(applicationX);
}
@ApplicationX.Component
@NRpcService(value = "/demo", version = "1.0.0")
public static class DemoService {
public Map hello(String name) {
Map result = new LinkedHashMap();
result.put("name", name);
result.put("timestamp", System.currentTimeMillis());
return result;
}
}
}
`
`
// client demo
public class RpcClientApplication {
public static void main(String[] args){
RpcClient rpcClient = new RpcClient("localhost", 80);
DemoClient demoClient = rpcClient.newInstance(DemoClient.class);
Map result = demoClient.hello("wang");
System.out.println("result = " + result);
}
@NRpcService(value = "/demo", version = "1.0.0", timeout = 2000)
public interface DemoClient {
Map hello(@NRpcParam("name") String name);
}
}
`
我这里并没有用java9的模块话拆分, 所以即使升级到java9也是没问题的 测试过java9, 可以正常使用.
artifactId是固定的, 我是不会改的
I'm not using java9 modules here, so upgrading to java9 is fine
Java9 has been tested and can be used normally.
ArtifactId is fixed, I'm not going to change it
Hi, Zihao,
Package name, 我的意思是虽然这个ArtifactId是个通用功能,可能被用到不同的应用里,那些应用可能是需要Java9 模块功能支持的。 如果应用里面还有一个jar里面有相同 package,就会出现conflict编译不了。因此采用一个不那么通用的package name就减少了以后可能发生的问题。
从 Windows 版邮件https://go.microsoft.com/fwlink/?LinkId=550986发送
发件人: @.> 发送时间: 2022年4月15日 14:39 收件人: @.> 抄送: Xingjian @.>; @.> 主题: Re: [wangzihaogithub/spring-boot-protocol] split to core module plus protocol modules (Issue #16)
我这里并没有用java9的模块话拆分, 所以即使升级到java9也是没问题的 测试过java9, 可以正常使用.
artifactId是固定的, 我是不会改的
I'm not using java9 modules here, so upgrading to java9 is fine
Java9 has been tested and can be used normally.
ArtifactId is fixed, I'm not going to change it
― Reply to this email directly, view it on GitHubhttps://github.com/wangzihaogithub/spring-boot-protocol/issues/16#issuecomment-1099894845, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAH3LDFW55KRYPBFCQODAR3VFEFIPANCNFSM5TACC4XQ. You are receiving this because you authored the thread.Message ID: @.***>
Hi, Zihao,
代码很漂亮,简洁。这段代码应该不许spring-boot吧。
那如何定义一个异步调用带有callback的,例如: <IN, OUT> void asyncCall(IN msg, Callback<OUT> callback);
以及仅仅发生一个消息 <IN> void send(IN msg);
从 Windows 版邮件https://go.microsoft.com/fwlink/?LinkId=550986发送
发件人: @.> 发送时间: 2022年4月15日 14:07 收件人: @.> 抄送: Xingjian @.>; @.> 主题: Re: [wangzihaogithub/spring-boot-protocol] split to core module plus protocol modules (Issue #16)
rpc/netty-message demo
`
// server demo
public class RpcServerApplication {
public static void main(String[] args) {
StartupServer server = new StartupServer(80);
server.addProtocol(newHttpProtocol());
server.addProtocol(newRpcMessageProtocol());
server.start();
}
private static NRpcProtocol newRpcMessageProtocol() {
ApplicationX applicationX = new ApplicationX();
applicationX.scanner(true,"com.github.netty.javanrpc.server")
.inject();
return new NRpcProtocol(applicationX);
}
@ApplicationX.Component
@NRpcService(value = "/demo", version = "1.0.0")
public static class DemoService {
public Map hello(String name) {
Map result = new LinkedHashMap();
result.put("name", name);
result.put("timestamp", System.currentTimeMillis());
return result;
}
}
}
`
`
// client demo
public class RpcClientApplication {
public static void main(String[] args){
RpcClient rpcClient = new RpcClient("localhost", 80);
DemoClient demoClient = rpcClient.newInstance(DemoClient.class);
Map result = demoClient.hello("wang");
System.out.println("result = " + result);
}
@NRpcService(value = "/demo", version = "1.0.0", timeout = 2000)
public interface DemoClient {
Map ***@***.***("name") String name);
}
}
`
― Reply to this email directly, view it on GitHubhttps://github.com/wangzihaogithub/spring-boot-protocol/issues/16#issuecomment-1099880802, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAH3LDGA6WT63UVCWDY52VLVFEBRTANCNFSM5TACC4XQ. You are receiving this because you authored the thread.Message ID: @.***>
Hi, Zihao,
我看到你有通过 Future 实现的 aync call。但是 Future 回来了后还是要本地实现线程异步。如果有 callback,那就只需要提供个回调代码就可以了,比较方便一些。 比如 约定 如果 一个Method的最后一个参数是 Callback,那就按照异步调用。 将这个callback参数记录下来,替换为null,并将call标注为async,write到server端并保留一个promise。 Server端受到一个async 方法,生成一个 本地 callback,调用服务,服务在实现业务逻辑后,将结果参数到 callback Server端callback 将结果writeback到客户端 客户端 async promise 将结果通过本地保存的callback call回客户端业务逻辑。
不清楚这样可以实现吗?
从 Windows 版邮件https://go.microsoft.com/fwlink/?LinkId=550986发送 w 发件人: Wang @.> 发送时间: 2022年4月16日 2:25 收件人: @.> 主题: 回复: [wangzihaogithub/spring-boot-protocol] split to core module plus protocol modules (Issue #16)
Hi, Zihao,
代码很漂亮,简洁。这段代码应该不许spring-boot吧。
那如何定义一个异步调用带有callback的,例如: <IN, OUT> void asyncCall(IN msg, Callback<OUT> callback);
以及仅仅发生一个消息 <IN> void send(IN msg);
从 Windows 版邮件https://go.microsoft.com/fwlink/?LinkId=550986发送
发件人: @.> 发送时间: 2022年4月15日 14:07 收件人: @.> 抄送: Xingjian @.>; @.> 主题: Re: [wangzihaogithub/spring-boot-protocol] split to core module plus protocol modules (Issue #16)
rpc/netty-message demo
`
// server demo
public class RpcServerApplication {
public static void main(String[] args) {
StartupServer server = new StartupServer(80);
server.addProtocol(newHttpProtocol());
server.addProtocol(newRpcMessageProtocol());
server.start();
}
private static NRpcProtocol newRpcMessageProtocol() {
ApplicationX applicationX = new ApplicationX();
applicationX.scanner(true,"com.github.netty.javanrpc.server")
.inject();
return new NRpcProtocol(applicationX);
}
@ApplicationX.Component
@NRpcService(value = "/demo", version = "1.0.0")
public static class DemoService {
public Map hello(String name) {
Map result = new LinkedHashMap();
result.put("name", name);
result.put("timestamp", System.currentTimeMillis());
return result;
}
}
}
`
`
// client demo
public class RpcClientApplication {
public static void main(String[] args){
RpcClient rpcClient = new RpcClient("localhost", 80);
DemoClient demoClient = rpcClient.newInstance(DemoClient.class);
Map result = demoClient.hello("wang");
System.out.println("result = " + result);
}
@NRpcService(value = "/demo", version = "1.0.0", timeout = 2000)
public interface DemoClient {
Map ***@***.***("name") String name);
}
}
`
― Reply to this email directly, view it on GitHubhttps://github.com/wangzihaogithub/spring-boot-protocol/issues/16#issuecomment-1099880802, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAH3LDGA6WT63UVCWDY52VLVFEBRTANCNFSM5TACC4XQ. You are receiving this because you authored the thread.Message ID: @.***>
demo
`
// rpc client demo
public class RpcClientApplication {
public static void main(String[] args){
RpcClient rpcClient = new RpcClient("localhost", 80);
DemoClient demoClient = rpcClient.newInstance(DemoClient.class);
DemoMessageClient demoMessageClient = rpcClient.newInstance(DemoMessageClient.class);
DemoAsyncClient demoAsyncClient = rpcClient.newInstance(DemoAsyncClient.class);
Map result = demoClient.hello("wang");
System.out.println("result = " + result);
demoAsyncClient.hello("wang").whenComplete((data, exception) -> {
System.out.println("data = " + data);
System.out.println("exception = " + exception);
});
// ...
}
@NRpcService(value = "/demo", version = "1.0.0", timeout = 2000)
public interface DemoClient {
Map hello(@NRpcParam("name") String name);
}
@NRpcService(value = "/demo", version = "1.0.0", timeout = 2000)
public interface DemoAsyncClient {
CompletableFuture<Map> hello(@NRpcParam("name") String name);
}
@NRpcService(value = "/demo", version = "1.0.0", timeout = 2000)
public interface DemoMessageClient {
// void is only send a message. not need to wait peer server for a reply
void hello(@NRpcParam("name") String name);
}
}
`
这段代码不需要spring-boot, 只需要引入自己即可, 并且版本号大于等于2.2.6, 其他都不需要.
<!-- https://mvnrepository.com/artifact/com.github.wangzihaogithub/spring-boot-protocol -->
<dependency>
<groupId>com.github.wangzihaogithub</groupId>
<artifactId>spring-boot-protocol</artifactId>
<version>2.2.6</version>
</dependency>
关于ArtifactId的问题, 我对java9还不是很了解, 可以麻烦您帮忙指导一下我怎么改, 或者提供一下思路吗?
Hi, Zihao,
我对Java9了解有限。这个和ArtifactId没有关系,引入到 java9 模块化应用里面,包名 package name, 也就是 com.github.netty 是比较关键的。包名在一个模块化应用的多个jar也就是模块中间必须是不能重复的,也就是是不同jar中不能包含相同的包名。我提到的也是可能有的的潜在风险,因为 com.jithub.netty 是个很github 的通用的包名,大概应该是由 GitHub 自己的jar使用才合适。
关于ArtifactId,我觉得问题不大,groupId是命名空间,在这个命名空间里面,ArtifactId唯一都是可以的。
从 Windows 版邮件https://go.microsoft.com/fwlink/?LinkId=550986发送
发件人: @.> 发送时间: 2022年4月16日 13:01 收件人: @.> 抄送: Xingjian @.>; @.> 主题: Re: [wangzihaogithub/spring-boot-protocol] split to core module plus protocol modules (Issue #16)
关于ArtifactId的问题, 我对java9还不是很了解, 可以麻烦您帮忙指导一下我怎么改, 或者提供一下思路吗?
― Reply to this email directly, view it on GitHubhttps://github.com/wangzihaogithub/spring-boot-protocol/issues/16#issuecomment-1100571702, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAH3LDF3UO4QDLYJIWBPVB3VFJCS5ANCNFSM5TACC4XQ. You are receiving this because you authored the thread.Message ID: @.***>
这个是maven中央仓库审核项目时的要求,项目包必须属于某个组织,我就归属到com.github下了。应该是没问题的,不然许多别人的项目也同样会出现这个问题。
Hi, Zihao,
这样就可以放心了。另外那个async的你有考虑实现吗? 我还是建议你考虑一些将代码分为core 和 protocols,http-protocol, nrpc-protocol等。如果这样的化,我可以很容易的实现copy/past一个xrpc-protocl出来了。
从 Windows 版邮件https://go.microsoft.com/fwlink/?LinkId=550986发送
发件人: @.> 发送时间: 2022年4月16日 17:17 收件人: @.> 抄送: Xingjian @.>; @.> 主题: Re: [wangzihaogithub/spring-boot-protocol] split to core module plus protocol modules (Issue #16)
这个是maven中央仓库审核项目时的要求,项目包必须属于某个组织,我就归属到com.github下了。应该是没问题的,不然许多别人的项目也同样会出现这个问题。
― Reply to this email directly, view it on GitHubhttps://github.com/wangzihaogithub/spring-boot-protocol/issues/16#issuecomment-1100617207, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAH3LDHTLBEEHBQZA535ZS3VFKAUDANCNFSM5TACC4XQ. You are receiving this because you authored the thread.Message ID: @.***>
目前是支持的,你看我的这个回复
https://github.com/wangzihaogithub/spring-boot-protocol/issues/16#issuecomment-1100568490
https://github.com/wangzihaogithub/spring-boot-protocol/issues/16#issuecomment-1100569829
不拆分代码也可以很轻松的实现一个xrpc-protocl
现在用pom.xml管理依赖了吗?导入这个包后,就能用了。
<!-- https://mvnrepository.com/artifact/com.github.wangzihaogithub/spring-boot-protocol -->
<dependency>
<groupId>com.github.wangzihaogithub</groupId>
<artifactId>spring-boot-protocol</artifactId>
<version>2.2.6</version>
</dependency>
你看下这个例子,自定义协议
`
public class MyServer {
public static void main(String[] args) {
StartupServer server = new StartupServer(8080);
// 添加mqtt协议
server.addProtocol(new com.github.netty.protocol.MqttProtocol());
// 添加mysql协议
server.addProtocol(new com.github.netty.protocol.MysqlProtocol(new InetSocketAddress("l92.168.101.1",3306)));
// 添加一种rpc协议
server.addProtocol(new com.github.netty.protocol.NRpcProtocol(new ApplicationX()));
// 添加http或websocket
server.addProtocol(new com.github.netty.protocol.HttpServletProtocol(new ServletContext()));
// 添加自定义协议
server.addProtocol(new AbstractProtocol() {
@Override
public String getProtocolName() {
return "hello world";
}
@Override
public boolean canSupport(ByteBuf clientFirstMsg) {
return true;
}
@Override
public void addPipeline(Channel channel) throws Exception {
channel.pipeline().addLast(new AbstractChannelHandler<ByteBuf, ByteBuf>() {
@Override
protected void onMessageReceived(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
System.out.println("收到! = " + msg.toString(Charset.forName("utf-8")));
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("新连接进入");
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("连接断开");
}
});
}
});
// 启动
server.start();
}
}
`
这个应用场景我碰到一些,比如客户端提供请求,返回结果确实服务器受到请求后继续其他工作,服务器端直接返回的是收到确认,但是因为逻辑只需要异常执行。
从 Windows 版邮件https://go.microsoft.com/fwlink/?LinkId=550986发送
发件人: Wang @.> 发送时间: 2022年4月16日 17:54 收件人: @.> 主题: 回复: [wangzihaogithub/spring-boot-protocol] split to core module plus protocol modules (Issue #16)
Hi, Zihao,
@.***@.***标明的interface,但是如果服务端是继承interface的化。 比如: void asyncHello(String message, Callback<String> callback)
服务端我也要实现这个 方法。因为服务端代码也是异步的,调用完直接返回了,当方法异步执行完毕后,回调这个callback返回结果。
Client端也proxy同样的调用。
现在你的代码实现要求服务端必须调用返回结果,当然可以转异步但是就要麻烦一些。
不知道我描述的是不是清楚。
从 Windows 版邮件https://go.microsoft.com/fwlink/?LinkId=550986发送
发件人: @.> 发送时间: 2022年4月16日 17:29 收件人: @.> 抄送: Xingjian @.>; @.> 主题: Re: [wangzihaogithub/spring-boot-protocol] split to core module plus protocol modules (Issue #16)
目前是支持的,你看我的这个回复
#16 (comment)https://github.com/wangzihaogithub/spring-boot-protocol/issues/16#issuecomment-1100568490
― Reply to this email directly, view it on GitHubhttps://github.com/wangzihaogithub/spring-boot-protocol/issues/16#issuecomment-1100619512, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAH3LDHIUIP5WIUEHDELCHTVFKB63ANCNFSM5TACC4XQ. You are receiving this because you authored the thread.Message ID: @.***>