springboot整合webflux访问openai接口报错

发布时间:2023年12月18日

报错信息:?

org.springframework.web.reactive.function.client.WebClientRequestException: 远程主机强迫关闭了一个现有的连接。; nested exception is java.io.IOException: 远程主机强迫关闭了一个现有的连接。
	at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException$9(ExchangeFunctions.java:141)
	Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
	*__checkpoint ? Request to POST https://api.openai.com/v1/chat/completions [DefaultWebClient]
Original Stack Trace:
		at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException$9(ExchangeFunctions.java:141)
		at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction$$Lambda$1634/1256286891.get(Unknown Source)
		at reactor.core.publisher.MonoErrorSupplied.subscribe(MonoErrorSupplied.java:55)
		at reactor.core.publisher.Mono.subscribe(Mono.java:4400)
		at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:103)
		at reactor.core.publisher.FluxPeek$PeekSubscriber.onError(FluxPeek.java:222)
		at reactor.core.publisher.FluxPeek$PeekSubscriber.onError(FluxPeek.java:222)
		at reactor.core.publisher.FluxPeek$PeekSubscriber.onError(FluxPeek.java:222)
		at reactor.core.publisher.MonoNext$NextSubscriber.onError(MonoNext.java:93)
		at reactor.core.publisher.MonoFlatMapMany$FlatMapManyMain.onError(MonoFlatMapMany.java:204)
		at reactor.core.publisher.SerializedSubscriber.onError(SerializedSubscriber.java:124)
		at reactor.core.publisher.FluxRetryWhen$RetryWhenMainSubscriber.whenError(FluxRetryWhen.java:225)
		at reactor.core.publisher.FluxRetryWhen$RetryWhenOtherSubscriber.onError(FluxRetryWhen.java:274)
		at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.drain(FluxConcatMap.java:415)
		at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.onNext(FluxConcatMap.java:251)
		at reactor.core.publisher.EmitterProcessor.

当你的应用程序在使用 HTTPS(SSL)时遇到 "远程主机强迫关闭了一个现有的连接" 的错误时,可能有多个原因导致这个问题。

解决方法(配置SSL证书)

1.?生成证书和密钥存储库文件。您可以使用keytool命令生成证书和密钥存储库文件。例如,以下命令将生成一个名为keystore.p12的密钥存储库文件():

?

keytool -genkeypair -alias mydomain -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore keystore.p12 -validity 3650 -ext SAN=DNS:api.openai.com

?

?

?

?

?

这里的 -ext SAN=DNS:api.openai.com 将 api.openai.com 添加到证书的 Subject Alternative Name。

如果你的服务需要支持多个子域名,可以考虑使用通配符证书,它能够匹配多个子域名。例如,*.openai.com 将匹配 api.openai.com、www.openai.com 等。

2. 将keystore.p12文件拷贝到项目resources

?

3. 在application.yml文件中配置ssl属性。例如,以下是一个application.properties文件的示例:?

server:
  port: 8009
  ssl:
    key-store: classpath:keystore.p12
    key-store-password: 你的密钥
    key-store-type: PKCS12
    key-alias: mydomain(这里是上面命令设置的别名)
    enabled-protocols: TLSv1.2
    ciphers: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256

4. 测试访问? ?https://localhost:8009/openai

?

文章来源:https://blog.csdn.net/weixin_43652507/article/details/135041913
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。