创建示例服务A和B
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ji</groupId>
<artifactId>spring-clound-learning</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>service-a</artifactId>
<!--服务B启用<artifactId>service-b</artifactId> -->
<packaging>jar</packaging>
<name>service-a or service-b</name>
<description>Spring Cloud project</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 监控中心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
yml配置
info:
groupId: @project.groupId@
artifactId: @project.artifactId@
version: @project.version@
spring:
application:
name: service-a
#服务B启用name: service-b
swagger:
title: ${spring.application.name}
server:
port: 2222
eureka:
instance:
hostname: localhost
status-page-url: http://localhost:${server.port}/swagger-ui.html
client:
service-url:
defaultZone: http://localhost:1111/eureka/
发布示例服务
示例服务A和B暂且发相同服务
@RestController
@Slf4j
public class ComputeController {
@Autowired
EurekaRegistration eurekaRegistration;
@RequestMapping(value = "/add" ,method = RequestMethod.GET)
@SneakyThrows
public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
Integer r = a + b;
Thread.sleep(10000);
log.info("/add, host:" + eurekaRegistration.getHost()+ ", service_id:" + eurekaRegistration.getServiceId() + ", result:" + r);
return r;
}
}
分别启动服务A和B
-
打开 Spring Cloud 基于Spring Boot 2.0.6的服务注册与发现(Eureka)创建的服务注册中心
打开浏览器,地址栏输入http://localhost:1111/ 进入服务注册中心 -
打开对应服务的status链接的进入swagger界面
创建zuul API网关
pom.xm配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-clound-learning</artifactId>
<groupId>com.ji</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>api-gatway</artifactId>
<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>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
yml配置
info:
groupId: @project.groupId@
artifactId: @project.artifactId@
version: @project.version@
spring:
application:
name: spring-cloud-zuulswagger:
title: ${spring.application.name}
server:
port: 9000
eureka:
instance:
hostname: localhost
status-page-url: http://localhost:${server.port}/swagger-ui.html
client:
service-url:
defaultZone: http://localhost:1111/eureka/
management:
endpoints:
web:
exposure:
include: '*'
zuul:
routes:
service-a: /service-a/**
service-b: /service-b/**
Application.java
api网关本身没有发布api,所以swagger是没有api接口输出的,为了能在API网关能看到对应的swagger api接口,需要整合一点代码如下
@EnableZuulProxy
@SpringCloudApplication
@EnableSwagger2Doc
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@RefreshScope
@ConfigurationProperties("zuul")
public ZuulProperties zuulProperties() {
return new ZuulProperties();
}
@Component
@Primary
class DocumentationConfig implements SwaggerResourcesProvider {
@Autowired
ZuulProperties zuulProperties;
@Override
public List<SwaggerResource> get() {
Set<String> routeKeys = zuulProperties.getRoutes().keySet();
List resources = new ArrayList();
for (String service : routeKeys) {
resources.add(swaggerResource(service,"/"+service+"/v2/api-docs","2.0"));
resources.add(swaggerResource(service,"/"+service+"/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;
}
}
}
启动API网关服务
打开浏览器,地址栏输入http://localhost:9000/ swagger-ui.html
进入swagger界面,默认显示服务A swagger接口