springboot整合webflux访问openai接口报错

报错信息: 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yixian123.com

谢谢打赏,祝老板心想事成

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值