【发布时间】:2021-02-14 01:03:42
【问题描述】:
我正在尝试使用响应式 mongodb 和 EventSource 进行工作的 Spring Boot。 但是,我面临重复重新打开连接的问题,因为它已被服务器关闭。我什至怀疑这是否真的有效,因为我没有找到任何具有反应式数据库和事件源的工作示例......
能否请您指出一个工作示例或告诉我我的代码有什么问题?
这里是代码的主要部分:
pom.xml
<properties>
<java.version>1.8</java.version>
<junit-jupiter.version>5.3.2</junit-jupiter.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
</parent>
<dependencies>
<!-- webflux reactive -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<!-- exclude junit 4, prefer junit 5 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- junit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
正如您在 pom 中看到的,我使用的是嵌入式 tomcat(我已经尝试过使用 Netty,默认的 Spring Boot 服务器......)。 此外,我正在将应用程序部署到任何远程服务器,但只是在我的本地服务器上尝试(Windows 10)。
网页:
let source = new EventSource("/comment/stream");
source.addEventListener("message", function (event) {
// These events are JSON, so parsing and DOM fiddling are needed
var comment = JSON.parse(event.data);
console.log(comment );
});
source.addEventListener("error", function (event) {
console.log("error", event);
this.close();
});
休息控制器:
@RestController
public class CommentController {
@Autowired
private CommentRepository commentRepository;
@PostMapping(path = "/comment")
public Mono<Comment> comment(@RequestBody Comment comment) {
return this.commentRepository.save(comment);
}
@GetMapping(path = "/comment/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Comment> feed() {
return this.commentRepository.findAll();
}
}
数据库存储库:
@Repository
public interface CommentRepository extends ReactiveSortingRepository<Comment, String> {
Flux<Comment> findAll();
}
同样,使用 EventSource 的 Web 客户端每秒都在重新连接,因为连接已被服务器关闭。
谢谢!
【问题讨论】:
-
您的代码中没有任何内容可以解释原因,我们需要更多信息、更多客户端代码、更多日志。
标签: mongodb spring-boot spring-webflux eventsource spring-reactive