1、项目的工程文件
2、eureka-server的配置
org.springframework.boot
spring-boot-starter-parent
1.5.6.RELEASE
UTF-8
UTF-8
1.8
Dalston.SR2
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
3、eureka-server的yml文件配置
server:
port: 8001
spring:
application:
name: eureka-server
eureka:
client:
fetch-registry: false
register-with-eureka: false
serviceUrl:
defaultZone: http://localhost:8001/eureka/
4、核心:zuul网关的配置,核心依赖
org.springframework.cloud
spring-cloud-starter-zuul
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-test
test
io.springfox
springfox-swagger2
2.6.1
io.springfox
springfox-swagger-ui
2.6.1
5、zuul中yml文件配置
server:
port: 8002
spring:
application:
name: eureka-gateway
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8001/eureka/
zuul:
routes:
service-a:
path: /service-a/** # 配置路由规则
service-b:
path: /service-b/** # 配置路由规则
6、构建一个整合swagger的api zuul网关,把各个微服务的swagger路径整合到zuul的swagger下,可以通过下拉列表进行访问,不过需要zuul进行路由,见上面的路由配置
@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
@ComponentScan("com.xaq")
public class SpringCloudZullApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudZullApplication.class, args);
}
@Component
@Primary
class DocumentationConfig implements SwaggerResourcesProvider {
@Override
public List get() {
List resources = new ArrayList();
resources.add(swaggerResource("这是我的A服务Api","/service-a/v2/api-docs","2.0"));
resources.add(swaggerResource("这是我的B服务Api","/service-b/v2/api-docs","2.0"));
return resources;
}
private SwaggerResource swaggerResource(String name, String location, String version) {
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion(version);
return swaggerResource;
}
}
}
7、我们需要统一访问zuul网关下的swagger界面,这是zuul中swagger的配置
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("分布式购物系统")
.description("购物系统接口文档说明")
.termsOfServiceUrl("http://localhost:8081")
.contact(new Contact("vker", "", "[email protected]"))
.version("1.0")
.build();
}
@Bean
UiConfiguration uiConfig() {
return new UiConfiguration(null, "list", "alpha", "schema",
UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L);
}
}
8、service-a 项目中依赖
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-test
test
io.springfox
springfox-swagger2
2.6.1
io.springfox
springfox-swagger-ui
2.6.1
9、service-a 中swagger的配置,开启swagger注解
@EnableSwagger2
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.xaq"))
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("service-a")
.description("接口文档说明")
.contact(new Contact("xaq", "", "[email protected]"))
.version("2.0")
.build();
}
}
10、在service-a 写一个测试的controller,需要在上面swagger配置的扫描路径下
*/
@Api("测试的controller")
@RestController
public class TestController {
@ApiOperation("hello")
@RequestMapping("/hello")
public String hello() {
return "hello";
}
}
11、通过访问api网关的swagger路径可以看到下面的界面,下拉框可以进行切换不同微服务,查看到不同的api
源码:github