1.解決unknown:
<!--引入spring cloud alibaba--> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.3.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--解決Cannot resolve com.alibaba.cloud:aliyun-oss-spring-boot-starter:unknown--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>aliyun-oss-spring-boot-starter</artifactId> <version>1.0.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2.執行上傳測試報錯
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘com.cevent.yameng.web.mall.product.YamengMallProductApplicationTests’: Unsatisfied dependency expressed through field ‘ossClient’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.aliyun.oss.OSSClient’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
3.問題分析:阿里雲dependencies版本不兼容
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'aliCloudEdasSdk' defined in class path resource [com/alibaba/cloud/spring/boot/context/autoconfigure/EdasContextAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.cloud.context.edas.AliCloudEdasSdk]: Factory method 'aliCloudEdasSdk' threw exception; nested exception is java.lang.NoSuchMethodError: com.aliyuncs.profile.DefaultProfile.getHttpClientConfig()Lcom/aliyuncs/http/HttpClientConfig;
4.修改common-pom
<?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>yameng-mall</artifactId> <groupId>com.cevent.yameng.web.mall</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>yameng-mall-common</artifactId> <description>各個微服務系統依賴的公共服務包,包括bean、utils等工具類</description> <dependencies> <!--引入mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.1</version> </dependency> <!--引入lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency> <!--引入httpStatus依賴的java發送http請求組件--> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.13</version> </dependency> <!--引入stringUtils需要的依賴--> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <!--引入mysql驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency> <!--引入http servlet包依賴,tomcat一般都有,所有這里設置為scope=provided,打包時候不需要--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <!--引入ali-Nacos服務發現中心--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <!--<version>2.2.0.RELEASE</version>--> </dependency> <!--引入ali-Nacos服務配置中心--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!--引入阿里雲封裝好的cloud oss--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alicloud-oss</artifactId> </dependency> </dependencies> <!--引入spring cloud alibaba--> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <!--<version>2.2.3.RELEASE</version>--> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--解決Cannot resolve com.alibaba.cloud:aliyun-oss-spring-boot-starter:unknown--> <!--<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>aliyun-oss-spring-boot-starter</artifactId> <version>1.0.0</version> <type>pom</type> <scope>import</scope> </dependency>--> </dependencies> </dependencyManagement> </project>
5.Product.pom繼續使用之前的oss,可省略,只為測試原生上傳
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version> <!--<version>2.1.8.RELEASE</version>--> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.cevent.yameng.web.mall</groupId> <artifactId>yameng-mall-product</artifactId> <version>0.0.1-SNAPSHOT</version> <name>yameng-mall-product</name> <description>亞盟商城-商品服務模組</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR8</spring-cloud.version> </properties> <dependencies> <!--添加common公共模塊依賴--> <dependency> <groupId>com.cevent.yameng.web.mall</groupId> <artifactId>yameng-mall-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!--引入阿里雲對象存儲,后期用alibaba cloud oss 可省略--> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.10.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
6. Product.yml配置阿里雲access-key、secret-key、endpoint
7.yml配置詳情
spring: datasource: username: root password: cevent url: jdbc:mysql://***:3306/***?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai driver-class-name: com.mysql.jdbc.Driver #4.配置nacos注冊中心 cloud: nacos: discovery: server-addr: 127.0.0.1:8848 #5.配置阿里巴巴-cloud-oss,這里對應common.pom的spring-cloud-alibaba-dependencies(2.1.0.RELEASE),不兼容 alicloud: access-key: *** secret-key: *** oss: endpoint: *** ##1.mapper-locations默認值: private String[] mapperLocations = new String[]{"classpath*:/mapper/**/*.xml"}; #classpath*:掃描包括本product系統及各個引用包下的類路徑 #classpath:只掃描本product系統 mybatis-plus: mapper-locations: classpath*:/mapper/**/*.xml #2.調整entity實體類的tableId為自增主鍵,數據量大后,進行分庫分表的主鍵生成規則/自定義生成規則 global-config: db-config: id-type: auto # mybatis全局邏輯刪除:1代表刪除 0代表沒有刪除 logic-delete-value: 1 logic-not-delete-value: 0 #3.配置啟動端口 server: port: 6622 #4.設置打印日志 logging: level: com.cevent.yameng.web.mall: debug #5.配置阿里巴巴-cloud-oss,這里是新版本aliyun的配置,不兼容 #alibaba: # cloud: # access-key: *** # secret-key: *** # oss: # endpoint: ***
8.test測試
package com.cevent.yameng.web.mall.product; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClient; import com.aliyun.oss.OSSClientBuilder; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.cevent.yameng.web.mall.product.entity.BrandEntity; import com.cevent.yameng.web.mall.product.service.BrandService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.List; /** * 測試brand-service */ @SpringBootTest class YamengMallProductApplicationTests { @Autowired BrandService brandService; //1.測試新增品牌 @Test void contextLoads() { BrandEntity brandEntity = new BrandEntity(); brandEntity.setName("亞盟"); brandService.save(brandEntity); System.out.println("保存的brand-entity:" + brandEntity.getName()); } //2.修改品牌 @Test void updateBrand() { BrandEntity brandEntity = new BrandEntity(); //id為long brandEntity.setBrandId(1L); brandEntity.setDescript("亞盟電子商務有限公司"); brandService.updateById(brandEntity); System.out.println("更新的brand-entity:" + brandEntity.getDescript()); } //3.查詢品牌 @Test void selectBrand() { List<BrandEntity> brandEntityList; //包裝查詢wrapper,根據指定id查詢集合 brandEntityList = brandService.list(new QueryWrapper<BrandEntity>().eq("brand_id", 1L)); //遍歷集合 brandEntityList.forEach((item) -> { System.out.println("查詢的brand-entity:" + item); }); } //4.oss文件上傳測試,這里與OSSClient沖突 @Test void testUpload() throws FileNotFoundException { // Endpoint以杭州為例,其它Region請按實際情況填寫。 String endpoint = "endpoint"; // 雲賬號AccessKey有所有API訪問權限,建議遵循阿里雲安全最佳實踐, // 創建並使用RAM子賬號進行API訪問或日常運維,請登錄 https://ram.console.aliyun.com 創建。 String accessKeyId = "ak"; String accessKeySecret = "sk"; // 創建OSSClient實例。 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 上傳文件流 //1.指定文件 InputStream inputStream = new FileInputStream("D:\\CEVENT_INFO\\1_marknelson_anotherplace_t.jpg"); //2.上傳到bucket ossClient.putObject("cevent-yameng-shop", "cevent_icon.jpg", inputStream); // 關閉OSSClient。 ossClient.shutdown(); System.out.println("OSS上傳完成!"); } @Autowired OSSClient ossClient; //5.alibaba-cloud-oss文件上傳測試 @Test void testCloudOSSUpload() throws FileNotFoundException { // 上傳文件流 //1.指定文件 InputStream inputStream = new FileInputStream("D:\\CEVENT_INFO\\icon1.jpg"); //2.上傳到bucket ossClient.putObject("cevent-yameng-shop", "cevent_red2.jpg", inputStream); // 關閉OSSClient。 ossClient.shutdown(); System.out.println("aliyun-OSS上傳完成!"); } }
來源:https://blog.csdn.net/weixin_37056888/article/details/108953093