spring-cloud在k8s环境下的服务发现调用,似乎有点不同,还有本地开发环境,如何能调用到如文件上传微服务?
通常在抽取出一个微服务后,需要给其他开发人员调用,通?;崽峁﹎aven的jar包来调用此微服务。具体过程分成3部分如下:
-
1.文件微服务fileupload。
-
2.调用文件微服务的jar包??椋颐墙小疚募⒎馽lient】。
-
3.调用文件微服务的测试服务test,通常也就是其他微服务。
注意为了方便把【1.文件微服务fileupload】和【3.测试服务test】写在同一个工程下。
工程结构如下:
主要的就是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", /**扫描文件微服务client的类*/
"com.xxx.xxx" /**扫描本工程下的类*/
})
@EnableTransactionManagement
//k8s的服务发现,注意:这个和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类文件上传微服务类,对外调用。
public class FileUploadController {
/**
* 上传文件生成url,并标记此文件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类调用【文件微服务client】进行微服务的调用。
@RestController
@RequestMapping("/fileupload-test")
@Validated
@Slf4j
public class FileUploadTestController {
//调用【文件微服务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和文件微服务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.文件微服务fileupload】和【3.测试服务test】完成。
2.调用【文件微服务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类调用【文件微服务】接口。
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部署的微服务名字*/
url = "http://10.10.94.2:8080", /**直接通过url进行本地环境调用*/
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>