Spring Cloud基于zuul 实现API网关并整合swagger

创建示例服务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

创建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接口在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值