springcloud 之zuul整合swagger

1、項目的工程文件



2、eureka-server的配置

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.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>Dalston.SR2</spring-cloud.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>


		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<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>
		</dependencies>
	</dependencyManagement>

3、eureka-server的yml文件配置

server:
  port: 8001


spring:
  application:
    name: eureka-server


eureka:
  client:
    fetch-registry: false
    register-with-eureka: false
    serviceUrl:
      defaultZone: http://localhost:8001/eureka/


4、核心:zuul網關的配置,核心依賴

<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zuul</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!-- swagger -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.6.1</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.6.1</version>
		</dependency>
	</dependencies>


5、zuul中yml文件配置

server:
  port: 8002

spring:
  application:
    name: eureka-gateway


eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8001/eureka/
zuul:
  routes:
    service-a:
      path: /service-a/** # 配置路由規則
    service-b:
      path: /service-b/** # 配置路由規則


6、構建一個整合swagger的api zuul網關,把各個微服務的swagger路徑整合到zuul的swagger下,可以通過下拉列表進行訪問,不過需要zuul進行路由,見上面的路由配置

@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
@ComponentScan("com.xaq")
public class SpringCloudZullApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudZullApplication.class, args);
	}

	@Component
	@Primary
	class DocumentationConfig implements SwaggerResourcesProvider {

		@Override
		public List<SwaggerResource> get() {
			List resources = new ArrayList();
			resources.add(swaggerResource("這是我的A服務Api","/service-a/v2/api-docs","2.0"));
			resources.add(swaggerResource("這是我的B服務Api","/service-b/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;
		}
	}

}


7、我們需要統一訪問zuul網關下的swagger界面,這是zuul中swagger的配置

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("分佈式購物系統")
                .description("購物系統接口文檔說明")
                .termsOfServiceUrl("http://localhost:8081")
                .contact(new Contact("vker", "", "[email protected]"))
                .version("1.0")
                .build();
    }

    @Bean
    UiConfiguration uiConfig() {
        return new UiConfiguration(null, "list", "alpha", "schema",
                UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L);
    }
}



8、service-a 項目中依賴

<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!-- swagger -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.6.1</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.6.1</version>
		</dependency>

	</dependencies>


9、service-a 中swagger的配置,開啓swagger註解

@EnableSwagger2
@Configuration
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xaq"))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("service-a")
                .description("接口文檔說明")
                .contact(new Contact("xaq", "", "[email protected]"))
                .version("2.0")
                .build();
    }
}


10、在service-a 寫一個測試的controller,需要在上面swagger配置的掃描路徑下

 */
@Api("測試的controller")
@RestController
public class TestController {

    @ApiOperation("hello")
    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }
}


11、通過訪問api網關的swagger路徑可以看到下面的界面,下拉框可以進行切換不同微服務,查看到不同的api



源碼:github

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章