优化-Spring Boot项目服务端接口超时设置

背景

  《优化-Spring Boot项目服务端接口超时设置》近期断断续续处理了一些优化类问题,其中有一类问题是超时设置,本文对此进行一些介绍,避免使用不到造成未达到理想期望。

异步

  很多开发者在遇到超时问题之后,第一反应是将请求改为异步,但是会遇到客户端收到了请求超时的信息,但是服务端仍在执行代码,造成前端展示与后端业务执行的误差,绝大部分解决方案如下所示。

Callable异步

  实现逻辑如下所示,配置文件中增加异步请求的超时时间,Controller接口中返回 Callable 对象。


spring.mvc.async.request-timeout=5000

@GetMapping("/test")
public Callable<String> test() throws InterruptedException {
    Callable<String> callable = () -> { orderService.insert(new Order()); return "success"; };
    return callable;
}

@Transactional(timeout = 5) 注解

  实现逻辑如下所示,在 Service 实现类中,增加事务注解,并且设置超时时间,这样如果业务代码执行时间过长,则直接回滚代码


@GetMapping("/special")
public String special() {
    orderService.insert(new Order());
    return "success";
}

@Override
@Transactional(timeout = 5)
public Order insert(Order order)  {
    for(int i = 0; i < 100000000; i++) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        orderRepository.save(new Order());
    }
    return order;
}

其他

  还有类似使用 CompletableFuture 进行实现的方式,通过结合线程池的方式处理,本文不过多介绍。

总结

  成本最低的解决方案是使用 @Transactional(timeout = 5) 注解,此方案不需要做额外的改动,可以快速增加服务端的超时设置,并且保证前后端理解一致。

参考

  1. http://pap-docs.pap.net.cn/
  2. https://gitee.com/alexgaoyh
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个关于使用 tio-websocket-spring-boot-starter 组件实现服务消息的接收和发送的问题。 首先,你需要在你的 Spring Boot 项目中添加 tio-websocket-spring-boot-starter 组件的依赖。可以在 Maven 或 Gradle 中添加以下依赖: ```xml <dependency> <groupId>com.github.t-io</groupId> <artifactId>tio-websocket-spring-boot-starter</artifactId> <version>2.7.0.v20190920-RELEASE</version> </dependency> ``` 或 ```groovy implementation 'com.github.t-io:tio-websocket-spring-boot-starter:2.7.0.v20190920-RELEASE' ``` 接下来,你需要创建一个 WebSocket 服务类,并使用 `@TioWebSocketServer` 注解将其标记为 Tio WebSocket 服务器。例如: ```java import org.springframework.stereotype.Component; import org.tio.server.ServerGroupContext; import org.tio.websocket.server.WsServerConfig; import org.tio.websocket.server.WsServerStarter; import org.tio.websocket.server.handler.IWsMsgHandler; @Component @TioWebSocketServer public class MyWebSocketServer { private WsServerStarter wsServerStarter; public MyWebSocketServer(IWsMsgHandler wsMsgHandler) { WsServerConfig wsServerConfig = new WsServerConfig(8080); ServerGroupContext serverGroupContext = new ServerGroupContext("my-websocket-server", wsMsgHandler); wsServerStarter = new WsServerStarter(wsServerConfig, serverGroupContext); } public void start() throws Exception { wsServerStarter.start(); } public void stop() throws Exception { wsServerStarter.stop(); } } ``` 在上面的代码中,我们创建了一个名为 `MyWebSocketServer` 的 Spring 组件,并使用 `@TioWebSocketServer` 注解将其标记为 Tio WebSocket 服务器。`MyWebSocketServer` 类中的构造函数中注入了一个 `IWsMsgHandler` 类型的参数,这个参数将在后面用来处理 WebSocket 消息。 然后,我们创建了一个 `WsServerConfig` 对象,指定了 WebSocket 服务器的口号。接下来,我们创建了一个 `ServerGroupContext` 对象,并将 `WsMsgHandler` 对象传递给它,用来处理 WebSocket 消息。最后,我们创建了一个 `WsServerStarter` 对象,并传递了上面创建的 `WsServerConfig` 和 `ServerGroupContext` 对象,用来启动 WebSocket 服务器。 接下来,你需要创建一个 `IWsMsgHandler` 实现类,用来处理 WebSocket 消息。例如: ```java import org.springframework.stereotype.Component; import org.tio.websocket.common.WsRequest; import org.tio.websocket.common.WsResponse; import org.tio.websocket.server.handler.IWsMsgHandler; @Component public class MyWsMsgHandler implements IWsMsgHandler { @Override public Object onText(WsRequest wsRequest, String s) throws Exception { System.out.println("Received message: " + s); WsResponse wsResponse = WsResponse.fromText("Hello, " + s, "utf-8"); return wsResponse; } @Override public Object onBytes(WsRequest wsRequest, byte[] bytes) throws Exception { return null; } @Override public void onAfterHandshaked(WsRequest wsRequest, WsResponse wsResponse) throws Exception { System.out.println("WebSocket handshake succeeded"); } @Override public void onAfterClose(WsRequest wsRequest, byte[] bytes) throws Exception { System.out.println("WebSocket closed"); } } ``` 在上面的代码中,我们创建了一个名为 `MyWsMsgHandler` 的 Spring 组件,并实现了 `IWsMsgHandler` 接口。在 `onText` 方法中,我们将接收到的 WebSocket 文本消息进行处理,并返回一个包含响应消息的 `WsResponse` 对象。在 `onAfterHandshaked` 方法中,我们在 WebSocket 握手成功之后进行处理。在 `onAfterClose` 方法中,我们在 WebSocket 连接关闭之后进行处理。 最后,我们可以在 Spring Boot 应用程序中使用 `MyWebSocketServer` 组件来启动 WebSocket 服务器。例如: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class MyApplication { public static void main(String[] args) throws Exception { ConfigurableApplicationContext context = SpringApplication.run(MyApplication.class, args); MyWebSocketServer myWebSocketServer = context.getBean(MyWebSocketServer.class); myWebSocketServer.start(); } } ``` 在上面的代码中,我们创建了一个 Spring Boot 应用程序,并在 `main` 方法中启动了 WebSocket 服务器。我们首先通过 `SpringApplication.run` 方法启动 Spring Boot 应用程序,并获取了一个 `ConfigurableApplicationContext` 对象。然后,我们通过 `context.getBean` 方法获取了刚才创建的 `MyWebSocketServer` 组件,并调用了 `start` 方法来启动 WebSocket 服务器。 现在,你已经成功地使用 tio-websocket-spring-boot-starter 组件实现了服务消息的接收和发送。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值