服务器使用 Spring Boot 和 WebFlux 发送事件
Posted
技术标签:
【中文标题】服务器使用 Spring Boot 和 WebFlux 发送事件【英文标题】:Server Sent Events with Spring Boot and WebFlux 【发布时间】:2019-01-26 12:31:08 【问题描述】:我正在处理服务器发送事件。我得到了帮助
http://sinhamohit.com/writing/spring-boot-reactive-sse
https://github.com/mohitsinha/spring-boot-reactive-sse
上述示例的问题是所有内容都定义在一个类中。我正在尝试做不同的课程,但它失败了,但有例外:
2018-08-20 17:03:15.521 WARN 10964 --- [ main]
onfigReactiveWebServerApplicationContext : Exception encountered during
context initialization - cancelling refresh attempt:
org.springframework.context.ApplicationContextException: Unable to start
reactive web server; nested exception is
org.springframework.context.ApplicationContextException: Unable to start
ReactiveWebApplicationContext due to missing ReactiveWebServerFactory bean.
2018-08-20 17:03:15.599 ERROR 10964 --- [ main]
o.s.boot.SpringApplication : Application run failed
org.springframework.context.ApplicationContextException: Unable to start
reactive web server; nested exception is
org.springframework.context.ApplicationContextException: Unable to start
ReactiveWebApplicationContext due to missing ReactiveWebServerFactory bean.
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.onRefresh(ReactiveWebServerApplicationContext.java:76) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:61) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:330) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at hello.SpringBootApplication.main(SpringBootApplication.java:8) [classes/:na]
代码:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
@RestController
@RequestMapping("/stock/transaction")
public class StockTransactionController
@Autowired
StockTransactionService stockTransactionService;
@GetMapping(produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Flux<StockTransaction> stockTransactionEvents()
return stockTransactionService.getStockTransactions();
@Service
public class StockTransactionService
List<Stock> stockList = new ArrayList<>();
List<String> stockNames =
Arrays.asList("mango,banana,guava,infinity".split(","));
@Bean
CommandLineRunner commandLineRunner()
return args ->
createRandomStock();
stockList.forEach(System.out::println);
;
public Flux<StockTransaction> getStockTransactions()
Flux<Long> interval = Flux.interval(Duration.ofSeconds(1));
interval.subscribe((i) -> stockList.forEach(stock ->
stock.setPrice(changePrice(stock.getPrice()))));
Flux<StockTransaction> stockTransactionFlux = Flux
.fromStream(Stream.generate(() -> new
StockTransaction(getRandomUser(), getRandomStock(), new Date())));
return Flux.zip(interval, stockTransactionFlux).map(Tuple2::getT2);
请帮忙。
【问题讨论】:
它看起来像是缺少/损坏的依赖项。您是从 IDE 还是命令行运行它?您可以尝试mvn dependency:purge-local-repository
并重新开始您的应用程序吗?
【参考方案1】:
问题出在注释上。 @SpringBootApplication
【讨论】:
以上是关于服务器使用 Spring Boot 和 WebFlux 发送事件的主要内容,如果未能解决你的问题,请参考以下文章
AWS 可以与 Spring Boot 和 React 一起使用吗?
spring Boot(十九):使用Spring Boot Actuator监控应用
使用 Spring Boot 和 Spring Cloud 的微服务项目结构