最终效果,访问网关ip/swgger-ui.html 统一API文档入口,在页面右上可以切换项目
parent项目管理依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.0.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
zuul网关项目pom
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
</dependency>
</dependencies>
zuul项目启动类
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class AppGateWay {
public static void main(String[] args) {
SpringApplication.run(AppGateWay.class, args);
}
}
DocumentationConfig
@EnableSwagger2Doc
@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {
@Autowired
private DiscoveryClient discoveryClient;
@Value("${spring.application.name}")
private String applicationName;
@Override
public List<SwaggerResource> get() {
List<SwaggerResource> resources = new ArrayList<>();
// 排除自身,将其他的服务添加进去
discoveryClient.getServices().stream().filter(s -> !s.equals(applicationName)).forEach(name -> {
resources.add(swaggerResource(name, "/" + name + "/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;
}
}
application.yml
###服务启动端口号
server:
port: 80
###服务名称(服务注册到eureka名称)
spring:
application:
name: card-manager-zuul
###服务注册到eureka地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}(${spring.cloud.client.ipaddress}:${spring.application.instance_id:${server.port}})
### 配置网关反向代理
zuul:
routes:
api-a:
### 以 /api/user/访问转发到会员服务
path: /api/user/**
serviceId: CARD-MANAGER-SERVICE-MEMBER
api-b:
### 以 /api/message/访问转发到消息服务
path: /api/message/**
serviceId: CARD-MANAGER-SERVICE-MESSAGE