使用微服务网关Zuul统一管理swaggerApi
1.创建项目shop-basics-springcloud-zuul
2.引入Maven依赖
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.7.0.RELEASE</version>
</dependency>
3.在SpringBoot启动类添加统一管理API代码
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableSwagger2Doc
public class AppGateWay {
public static void main(String[] args) {
SpringApplication.run(AppGateWay.class, args);
}
// 添加文档来源
@Component
@Primary
class DocumentationConfig implements SwaggerResourcesProvider {
@Override
public List<SwaggerResource> get() {
List resources = new ArrayList<>();
resources.add(swaggerResource("app-bajin-member", "/app-bajin-member/v2/api-docs", "2.0"));
resources.add(swaggerResource("app-bajin-weixin", "/app-bajin-weixin/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;
}
}
}
4.增加配置文件
###服务启动端口号
server:
port: 80
###服务名称(服务注册到eureka名称)
spring:
application:
name: app-bajin-zuul
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
### 配置网关反向代理
zuul:
routes:
api-a:
### 以 /api-weixin/访问转发到会员服务
path: /api-weixin/**
serviceId: app-bajin-weixin
api-b:
### 以 /api-member/访问转发到订单服务
path: /api-member/**
serviceId: app-bajin-member
5.在浏览器输入如下地址访问
http://127.0.0.1/swagger-ui.html