zuul 网关聚合 swagger api

最终效果,访问网关ip/swgger-ui.html 统一API文档入口,在页面右上可以切换项目
zuul 网关聚合 swagger api_第1张图片
parent项目管理依赖

 
        org.springframework.boot
        spring-boot-starter-parent
        2.0.6.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
        Finchley.RELEASE
    


        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            

            
                org.projectlombok
                lombok
                1.16.10
            

            
                com.spring4all
                swagger-spring-boot-starter
                1.9.0.RELEASE
            

        

    

zuul网关项目pom

 
        
            org.springframework.cloud
            spring-cloud-starter-netflix-zuul
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            com.spring4all
            swagger-spring-boot-starter
        

    

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 get() {
        List 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

你可能感兴趣的:(springcloud,springcloud,zuul,swgger)