spring-cloud在k8s環(huán)境下的服務(wù)發(fā)現(xiàn)調(diào)用凿滤,似乎有點不同,還有本地開發(fā)環(huán)境利诺,如何能調(diào)用到如文件上傳微服務(wù)?
通常在抽取出一個微服務(wù)后剩燥,需要給其他開發(fā)人員調(diào)用慢逾,通常會提供maven的jar包來調(diào)用此微服務(wù)。具體過程分成3部分如下:
-
1.文件微服務(wù)fileupload灭红。
-
2.調(diào)用文件微服務(wù)的jar包模塊侣滩,我們叫【文件微服務(wù)client】。
-
3.調(diào)用文件微服務(wù)的測試服務(wù)test变擒,通常也就是其他微服務(wù)君珠。
注意為了方便把【1.文件微服務(wù)fileupload】和【3.測試服務(wù)test】寫在同一個工程下。
工程結(jié)構(gòu)如下:
主要的就是Application類注解的配置:
package com.xxx.xxx;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@MapperScan("com.xxx.xxx.dao")
@ComponentScan({
"com.xxx.fileupload.feign", /**掃描文件微服務(wù)client的類*/
"com.xxx.xxx" /**掃描本工程下的類*/
})
@EnableTransactionManagement
//k8s的服務(wù)發(fā)現(xiàn)娇斑,注意:這個和spring-cloud的不一樣策添。
@EnableDiscoveryClient
//注意材部,如果不配置basePackages可能會無法實例化類。
@EnableFeignClients(basePackages = "com.xxx.fileupload.feign")
//定時器配置注解
@EnableScheduling
public class UploadFileApplication {
public static void main(String[] args) {
SpringApplication.run(UploadFileApplication.class, args);
System.out.println("main end");
}
}
FileUploadController類文件上傳微服務(wù)類唯竹,對外調(diào)用乐导。
public class FileUploadController {
/**
* 上傳文件生成url,并標(biāo)記此文件url緩存2個小時浸颓,2個小時刪除此文件物臂。
*
* @param ao
* @return
*/
@PostMapping("/uploadFile")
@ApiOperation(value = "上傳文件")
public ResultVo<Object> uploadFile(@Valid UploadFileAo ao) {
PutObjectResult result;
try {
result = obsService.putObjectAndSetPreAccessStrategy(ao.fileMd5(), ao.getFile().getInputStream());
} catch (IOException e) {
return new ResultVo<>(500, "failed", "上傳文件異常");
}
if (result.getStatusCode() == 200) {
myRedisCache.hset("upfile", ao.fileMd5(), "{url:\"" + result.getObjectUrl() + "\",\"time\":" + System.currentTimeMillis() + "}");
}
return new ResultVo<>(result.getStatusCode(), "success", result);
}
}
FileUploadTestController類調(diào)用【文件微服務(wù)client】進(jìn)行微服務(wù)的調(diào)用。
@RestController
@RequestMapping("/fileupload-test")
@Validated
@Slf4j
public class FileUploadTestController {
//調(diào)用【文件微服務(wù)client】的類
@Autowired
FileUploadClient client;
/**
*
* @param ao
* @return
*/
@PostMapping("/uploadFile")
@ApiOperation(value = "上傳文件")
public ResultVo<Object> uploadFile(@Valid UploadFileAo ao) {
return client.uploadFile(ao);
}
pom.xml主要依賴产上,k8s和文件微服務(wù)client鹦聪。
<dependencies>
<dependency>
<groupId>com.xxx.xxx</groupId>
<artifactId>common-fileupload</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-config</artifactId>
</dependency>
<dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
</dependencies>
yml需要配置
logging:
level:
com.xxx.fileupload.feign.FileUploadClient: DEBUG
#熔斷器
feign:
hystrix:
enabled: true
由此,【1.文件微服務(wù)fileupload】和【3.測試服務(wù)test】完成蒂秘。
2.調(diào)用【文件微服務(wù)client】模塊:
FeignLoggerConfiguration 類配置feign的log級別泽本,以便打印出feign請求的log。
package com.xxx.fileupload.feign;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignLoggerConfiguration {
//指定feign的日志級別
@Bean
feign.Logger.Level feignLoggerLevel(){
return feign.Logger.Level.FULL;
}
}
FileUploadClient類調(diào)用【文件微服務(wù)】接口姻僧。
package com.xxx.fileupload.feign;
import com.xxx.fileupload.ao.SubmitUrlAo;
import com.xxx.fileupload.ao.UploadFileAo;
import com.xxx.fileupload.vo.ResultVo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name = "service-fileupload", /**k8s部署的微服務(wù)名字*/
url = "http://10.10.94.2:8080", /**直接通過url進(jìn)行本地環(huán)境調(diào)用*/
fallback = FileUploadClientFallback.class, /**熔斷處理類*/
configuration = FeignLoggerConfiguration.class) /**log配置類*/
public interface FileUploadClient {
@RequestMapping(path = "/fileupload/uploadFile", method = RequestMethod.POST)
ResultVo<Object> uploadFile(UploadFileAo ao);
@RequestMapping(path = "/fileupload/submitFileUrlsMd5", method = RequestMethod.POST)
ResultVo<Object> submitFileUrlsMd5(SubmitUrlAo ao);
}
FileUploadClientFallback 類熔斷處理规丽。
package com.xxx.fileupload.feign;
import com.xxx.fileupload.ao.SubmitUrlAo;
import com.xxx.fileupload.ao.UploadFileAo;
import com.xxx.fileupload.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class FileUploadClientFallback implements FileUploadClient {
private Logger logger = LoggerFactory.getLogger(FileUploadClientFallback.class);
@Override
public ResultVo<Object> uploadFile(UploadFileAo ao) {
logger.info("uploadFile {}",ao.toString());
return new ResultVo<>(500, "fallback");
}
@Override
public ResultVo<Object> submitFileUrlsMd5(SubmitUrlAo ao) {
logger.info("submitFileUrlsMd5 {}",ao.toString());
return new ResultVo<>(500, "fallback");
}
}
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
</dependencies>