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

创建示例服务A和B

pom.xml


<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.0modelVersion>
	<parent>
		<groupId>com.jigroupId>
		<artifactId>spring-clound-learningartifactId>
		<version>1.0.0version>
	parent>
	<artifactId>service-aartifactId>
	
	<packaging>jarpackaging>

	<name>service-a or  service-bname>
	<description>Spring Cloud projectdescription>

	<properties>
		<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
		<java.version>1.8java.version>
	properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>
		<dependency>
			<groupId>org.springframework.cloudgroupId>
			<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
		dependency>
		
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-actuatorartifactId>
		dependency>

		<dependency>
			<groupId>org.projectlombokgroupId>
			<artifactId>lombokartifactId>
		dependency>

		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-testartifactId>
			<scope>testscope>
		dependency>

		<dependency>
			<groupId>com.spring4allgroupId>
			<artifactId>swagger-spring-boot-starterartifactId>
		dependency>

	dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloudgroupId>
				<artifactId>spring-cloud-dependenciesartifactId>
				<version>Finchley.SR2version>
				<type>pomtype>
				<scope>importscope>
			dependency>
		dependencies>
	dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
				<executions>
					<execution>
						<goals>
							<goal>build-infogoal>
						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配置


<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-learningartifactId>
        <groupId>com.jigroupId>
        <version>1.0.0version>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>api-gatwayartifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-zuulartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>

        <dependency>
            <groupId>com.spring4allgroupId>
            <artifactId>swagger-spring-boot-starterartifactId>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>build-infogoal>
                        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接口Spring Cloud基于zuul 实现API网关并整合swagger_第1张图片

你可能感兴趣的:(SpringCloud)