04-spring-boot-resttemplate netty定制使用

04-spring-boot-resttemplate netty定制使用

rest template可以使用netty定制的工厂类-Netty4ClientHttpRequestFactory,完成相关rest接口访问工作,由于上一节已经大致介绍了相关rest template的使用,本文的重点放在替换的处理要点盘点以及Netty4ClientHttpRequestFactory的实现讨论上。

1 核心依赖

    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation group: 'io.netty', name: 'netty-all', version: '4.1.53.Final'

https://www.jianshu.com/p/c99bfdb5bf11

2 Netty4ClientHttpRequestFactory定制代码

其实主要代码与上一节相同,仅Supplier requestFactory的工场bean换成了我们的Supplier requestFactory。

image-20201024193332165

3 工作原理探究

为了证实相关代码的工作,使用了arthas监控相关代码,打印出了Netty4ClientHttpRequestFactory的具体堆栈调用,arthas指令如下是所示

stack org.springframework.http.client.Netty4ClientHttpRequestFactory

如下图所示,可以看到两个堆栈,一个是netty线程的消息发送,通道相关处理,另外一个则是发送rest请求的相关接口的底层调用栈,可以看到,相关rest接口的发送,底层实际上是Netty4ClientHttpRequestFactory调用netty完成的,从两个堆栈就可以非常明确。

image-20201024194849294

4 不推荐理由

spring官方目前已经不支持netty底层实现,这里是原因说明:

image-20201024193830545

原来底层netty实现的方式每次都会关闭http连接,这里确实不值得推荐。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
netty-websocket-spring-boot-starter是一个基于Netty实现的WebSocket框架,可以方便地在Spring Boot应用中集成WebSocket功能。使用该框架可以快速构建实时通信、消息推送等功能。 下面是一个使用netty-websocket-spring-boot-starter的简单案例: 1. 在pom.xml中添加依赖: ```xml <dependency> <groupId>com.github.wujiuye</groupId> <artifactId>netty-websocket-spring-boot-starter</artifactId> <version>1.0.0.RELEASE</version> </dependency> ``` 2. 编写WebSocket处理器 ```java @ServerEndpoint("/websocket/{userId}") @Component public class WebSocketHandler { private static final Logger logger = LoggerFactory.getLogger(WebSocketHandler.class); private Session session; private String userId; @OnOpen public void onOpen(Session session, @PathParam("userId") String userId) { this.session = session; this.userId = userId; logger.info("WebSocket连接建立,userId: {}", userId); } @OnMessage public void onMessage(String message) { logger.info("收到来自客户端的消息:{}", message); sendMessage("服务端已收到消息:" + message); } @OnClose public void onClose() { logger.info("WebSocket连接关闭,userId: {}", userId); } @OnError public void onError(Throwable t) { logger.error("WebSocket连接异常,userId: {}", userId, t); } public void sendMessage(String message) { try { this.session.getBasicRemote().sendText(message); } catch (IOException e) { logger.error("发送WebSocket消息失败,userId: {}", userId, e); } } } ``` 3. 配置WebSocket 在配置类中添加@EnableWebSocket注解,启用WebSocket功能,同时,也可以配置WebSocket的一些参数,例如端口号、路径等。 ```java @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Autowired private WebSocketHandler webSocketHandler; @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(webSocketHandler, "/websocket/{userId}") .setAllowedOrigins("*"); } } ``` 4. 测试WebSocket 使用浏览器或WebSocket客户端连接WebSocket服务端,例如:ws://localhost:8080/websocket/123,其中123为userId。 以上就是一个简单的netty-websocket-spring-boot-starter使用案例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值