谷粒商城ThirdPartyApplication启动报错: Oss endpoint can‘t be empty解决总结

我创建的项目引入Springboot版本:2.6.13,Springcloud版本:2021.0.5,Spring-cloud-aplibaba版本:2021.0.5.0,nacos版本:2.2.1.RELEASE。项目在父pom.xml中使用dependencyManagement减少子项目重复引入jar,统一版本的管理。

最近学习谷粒商城P63 OSS云存储练手时,运行创建第三方服务GulimallThirdPartyApplication.java报错:Oss endpoint can't be empty

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.aliyun.oss.OSS]: Factory method 'ossClient' threw exception; nested exception is java.lang.IllegalArgumentException: Oss endpoint can't be empty.
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.23.jar:5.3.23]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.23.jar:5.3.23]
	... 19 common frames omitted
Caused by: java.lang.IllegalArgumentException: Oss endpoint can't be empty.
	at org.springframework.util.Assert.isTrue(Assert.java:121) ~[spring-core-5.3.23.jar:5.3.23]
	at com.alibaba.alicloud.context.oss.OssContextAutoConfiguration.ossClient(OssContextAutoConfiguration.java:53) ~[spring-cloud-alicloud-context-2.2.0.RELEASE.jar:2.2.0.RELEASE]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_221]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_221]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_221]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.23.jar:5.3.23]
	... 20 common frames omitted

单独把oss.yml的配置放到application.yml中运行正常,但把oss.yml的提取出来配置到Nacos的配置中心运行就报这个错,搞得郁闷得很,一阵网上搜索解决了。

在网上搜索这种报错总结大概分为3种情况:

第一种:在oss.yml里配置错误。

第二种:在thirdparty的pom.xml里面配置springboot和springcloud版本不对应。

第三种:在application.yml配置不对。

于是乎我按网上说的方法逐一排查,先按第一种:检查nacos的配置oss.yml发现其中确实配置错了,修改后正确如下:

spring:
  cloud:
    alicloud:
      access-key: LTAI5tS6Kit9dU2KPJnBvByb
      secret-key: YCo25kTUzfbiuXz5QDNdauwJQiD2Rg
      oss:
        endpoint: oss-cn-beijing.aliyuncs.com
    util:
      enabled: false

改了这个配置后重启项目还是报相同的错,于是又按第二种:修改第三方服务(thirdparty)的pom.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mygulishop</artifactId>
        <groupId>com.tfq</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>third-party</artifactId>

    <name>third-party</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>
    <packaging>jar</packaging>
    <description>第三方服务</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.tfq</groupId>
            <artifactId>guli-common</artifactId>
            <version>1.0-SNAPSHOT</version>
            <!-- 暂时不使用Mybatis,排除-->
            <exclusions>
                <exclusion>
                    <groupId>com.baomidou</groupId>
                    <artifactId>mybatis-plus-boot-starter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alicloud-oss</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
    </dependencies>

    <build>

    </build>
</project>
spring-cloud-starter-bootstrap是Springboot从2.4.x以后加载bootstrap.yml必须要的。不然会报以下错:
org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor$ImportException: No spring.config.import set
	at org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor.postProcessEnvironment(ConfigDataMissingEnvironmentPostProcessor.java:82)
	at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEnvironmentPreparedEvent(EnvironmentPostProcessorApplicationListener.java:102)
	at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEvent(EnvironmentPostProcessorApplicationListener.java:87)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
	at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:85)
	at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:66)
	at java.util.ArrayList.forEach(ArrayList.java:1257)
	at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120)
	at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:114)
	at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:65)
	at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:343)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:301)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1317)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
	at com.tfq.ThirdPartyApplication.main(ThirdPartyApplication.java:11)
17:24:09.339 [main] ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - 

***************************
APPLICATION FAILED TO START
***************************

Description:

No spring.config.import property has been defined

Action:

Add a spring.config.import=nacos: property to your configuration.
	If configuration is not required add spring.config.import=optional:nacos: instead.
	To disable this check, set spring.cloud.nacos.config.import-check.enabled=false.


Process finished with exit code 1
spring-cloud-starter-alibaba-nacos-config是加载nacos的oss.yml的文件,当你不引入此包,会报相同错:Oss endpoint can't be empty。
我用了前面两种方式后,由于当前idea2022没有重新编译,导致也报相同错误。注:大家按第一二种方式处理了一定要重新compile。
于我做完第一二种方法没有重新编译,又试了第三种在aplication.yml中配置spring.cloud.bootstrap.enabled=true没用。官方说可以配置但没用,仍然报这个错。官方链接:

Spring Cloud Config

最后我又重新编译就不报错,能加载oss的配置了,ossclient也能实例化了。

参考:

java.lang.IllegalArgumentException: Oss endpoint can‘t be empty.*-CSDN博客

Oss endpoint can‘t be empty.记录第一次弄了三四个小时的bug_spring boot_m0_63161054-华为云开发者联盟

记录一次错误解决:Oss endpoint can‘t be empty(谷粒商城P63)_oss endpoint can't be empty.-CSDN博客

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值