写在前面的话:自己的总结,有什么不对的地方,希望指教交流
service端是服务提供者,除了提供接口外,还要封装client SDK 包,用于client端方便调用。我这里用的是swagger-codegen 来通过swagger接口生成swagger.json接口定义文件,然后再通过swagger.json生成client包项目,打包上传,就可以被调用了,节省了很多时间(spring的包管理用的是maven)。
在pom.xml build中添加swagger-maven-plugin插件
方便复制,这里粘贴上代码
<plugin>
<groupId>com.github.kongchengroupId>
<artifactId>swagger-maven-pluginartifactId>
<version>3.1.7version>
<configuration>
<apiSources>
<apiSource>
<springmvc>truespringmvc>
<locations>xxx.xxx.xxx.controllerlocations>
<schemes>httpschemes>
<host>localhost:8300host>
<basePath>/xxx-apibasePath>
<info>
<title>xxx-xxx-servicetitle>
<version>1.0.0version>
<description>API For xxx Servicedescription>
info>
<swaggerDirectory>${basedir}/target/generated/swagger-uiswaggerDirectory>
apiSource>
apiSources>
configuration>
<executions>
<execution>
<phase>compilephase>
<goals>
<goal>generategoal>
goals>
execution>
executions>
plugin>
这里可以通过swagger-codegen命令行,也可以通过maven插件
在pom.xml build中添加swagger-codegen-maven-plugin插件
方便复制,这里粘贴上代码
<plugin>
<groupId>io.swaggergroupId>
<artifactId>swagger-codegen-maven-pluginartifactId>
<version>2.2.3version>
<executions>
<execution>
<phase>installphase>
<goals>
<goal>generategoal>
goals>
<configuration>
<inputSpec>target/generated/swagger-ui/swagger.jsoninputSpec>
<output>target/generated/swagger-uioutput>
<language>springlanguage>
<library>spring-cloudlibrary>
<groupId>xxx.xxxgroupId>
<artifactId>xxx-xxx-service-clientartifactId>
<artifactVersion>1.0.0-SNAPSHOTartifactVersion>
<apiPackage>com.xxx.xxx.client.apiapiPackage>
<modelPackage>com.xxx.xxx.client.modelmodelPackage>
configuration>
execution>
executions>
plugin>
通过mvn install 命令,就可以生成client 包项目
这里有一个坑,好像是swagger-codegen留下的,不知道现在改了没
生成的client包中的代码:
@RequestPart注释到client包中成了@RequestParam,我在尝试了很多次,一直报错,在swagger Github issues 中有人提出来了。不知道这个是bug,还是我用的不得法,总之卡在这好久。这里需要把@RequestParam手动改为@RequestPart,然后打包上传。
如果你有私服,可以在client包配置中,添加上你的私服配置,就可以通过mvn deploy 上传了。
这里生成的是feign 接口,所以在web端,需要使用feign来进行接口调动。
<dependency>
<groupId>com.xxxgroupId>
<artifactId>xxx-xxx-service-clientartifactId>
<version>1.0.0-SNAPSHOTversion>
dependency>
在包中有feign的默认url,如果想指定url,可以在application中自定义配置,覆盖默认配置即可
自定义encoder:
@Bean
@Primary
@Scope("prototype")
public Encoder multipartFormEncoder() {
return new SpringFormEncoder();
}
指定的此encoder会不会对其他的接口有影响,还不知道,需要遇到时,再想办法解决了。
service和web服务都跑起来,就可以尝试一下,微服务间的文件传输了!!!