疯狂创客圈 Java 高并发【 亿级流量聊天室实战】实战系列 【博客园总入口

架构师成长+面试必备之 高并发基础书籍 【Netty Zookeeper Redis 高并发实战


前言

Crazy-SpringCloud 微服务脚手架 &视频介绍

Crazy-SpringCloud 微服务脚手架,是为 Java 微服务开发 入门者 准备的 学习和开发脚手架。并配有一系列的使用教程和视频,大致如下:

高并发 环境搭建 图文教程和演示视频,陆续上线:

中间件 链接地址
Linux Redis 安装(带视频) Linux Redis 安装(带视频)
Linux Zookeeper 安装(带视频) Linux Zookeeper 安装, 带视频
Windows Redis 安装(带视频) Windows Redis 安装(带视频)
RabbitMQ 离线安装(带视频) RabbitMQ 离线安装(带视频)
ElasticSearch 安装, 带视频 ElasticSearch 安装, 带视频
Nacos 安装(带视频) Nacos 安装(带视频)

Crazy-SpringCloud 微服务脚手架 图文教程和演示视频,陆续上线:

组件 链接地址
Eureka Eureka 入门,带视频
SpringCloud Config springcloud Config 入门,带视频
spring security spring security 原理+实战
Spring Session SpringSession 独立使用
分布式 session 基础 RedisSession (自定义)
重点: springcloud 开发脚手架 springcloud 开发脚手架
SpingSecurity + SpringSession 死磕 (写作中) SpingSecurity + SpringSession 死磕

小视频以及所需工具的百度网盘链接,请参见 疯狂创客圈 高并发社群 博客

Zuul Swagger整合的场景

我们知道,Swagger2整合到项目中,可以非常方便地进行接口测试,是前后端对接效率提高。

现在,我们可以在Zuul中整合Swagger2,通过Zuul配置文件配置的映射路径,来生成整体的微服务服务接口的统一测试的 Dashboard。

step1: Zuul引入swagger2的依赖:

在cloud-zuul子模块的pom.xml文件中引入swagger2的两个Maven依赖:

 <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>

step2: Zuul的Swagger2配置类

需要2个比较重要的配置类:

(1)是SwaggerConfig 类,主要用于配置Swagger描述信息;

(2)是DocumentationConfig 类,主要用于整合其他服务的接口API。

一: SwaggerConfig 类

SwaggerConfig 配置类的代码,大致如下:

package com.crazymaker.springcloud.cloud.center.zuul.config;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket buildDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(buildApiInf()) // .apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(""))// 需要生成文档的包的位置
.paths(PathSelectors.any())
.build();
}
//api的描述信息构建
private ApiInfo buildApiInf() {
return new ApiInfoBuilder()
.title("疯狂创客圈springcloud 高并发实战")
.description("Zuul+Swagger2构建RESTful APIs")
.termsOfServiceUrl("https://www.cnblogs.com/crazymakercircle/")
.contact(new Contact("疯狂创客圈", "https://www.cnblogs.com/crazymakercircle/", ""))
.version("1.0")
.build();
}
}

二:DocumentationConfig 类

主要用于整合其他服务的接口API,代码大致如下:

package com.crazymaker.springcloud.cloud.center.zuul.config;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider; import java.util.ArrayList;
import java.util.List; @Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {
private final RouteLocator routeLocator; public DocumentationConfig(RouteLocator routeLocator) {
this.routeLocator = routeLocator;
} /**
* 配置所有的微服务
*
* @return
*/
/* @Override
public List<SwaggerResource> get() {
List<SwaggerResource> resources = new ArrayList<>();
List<Route> routes = routeLocator.getRoutes();
routes.forEach(route -> {
resources.add(swaggerResource(route.getId(), route.getFullPath().replace("**", "v2/api-docs"), "1.0"));
});
return resources;
}*/ /**
* 配置特定的微服务
* @return
*/
@Override
public List<SwaggerResource> get() {
List resources = new ArrayList<>();
resources.add(swaggerResource("秒杀", "/seckill-provider/v2/api-docs", "1.0"));
resources.add(swaggerResource("消息", "/message-provider/v2/api-docs", "1.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;
}
}

SwaggerResourcesProvider 是资源提供者,我们重写他,把各个微服务的API文档资源路径返回。

既可以 配置所有的微服务,也可以配置特定的微服务。

如果 配置所有的微服务(注释部分),可以通过遍历eureka路由方式自动添加所有微服务 API 文档。

step3:微服务的Swagger2配置类

微服务提供者,也需要引入Swagger2依赖包,并且进行Swagger2的配置,代码如下:

package com.crazymaker.springcloud.user.info.config;

import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; /**
* swagger配置
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig { @Bean
public Docket templateApi() {
ParameterBuilder tokenPar = new ParameterBuilder();
tokenPar.name("token").description("token令牌")
.modelRef(new ModelRef("string")).parameterType("header").required(false).build();
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.useDefaultResponseMessages(false)
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.build().globalOperationParameters(Lists.newArrayList(tokenPar.build()));
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springcloud 高并发 实战 Rest API 文档")
.version("1.0")
.build();
}
}

step4:配置 SpringSecurity

如果加了SpringSecurity 进行权限控制,需要给 swagger 的连接,加上全部的许可,不进行拦截。

@EnableWebSecurity()
public class UserWebSecurityConfig extends WebSecurityConfigurerAdapter { @Resource
private UserAuthService userAuthService; protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers(
"/v2/api-docs",
"/swagger-resources/configuration/ui",
"/swagger-resources",
"/swagger-resources/configuration/security",
"/swagger-ui.html",
"/api/user/login/v1",
// "/api/user/add/v1",
// "/api/user/speed/test/v1",
// "/api/user/say/hello/v1",
// "/api/user/*/detail/v1",
"/api/crazymaker/duty/info/user/login")
.permitAll()
.anyRequest().authenticated() // .antMatchers("/image/**").permitAll()
// .antMatchers("/admin/**").hasAnyRole("ADMIN")
.and() .formLogin().disable()
.sessionManagement().disable()
.cors()
.and() .addFilterAfter(new OptionsRequestFilter(), CorsFilter.class)
.apply(new JsonLoginConfigurer<>()).loginSuccessHandler(jsonLoginSuccessHandler())
.and()
.apply(new JwtAuthConfigurer<>()).tokenValidSuccessHandler(jwtRefreshSuccessHandler()).permissiveRequestUrls("/logout")
.and()
.logout()
// .logoutUrl("/logout") //默认就是"/logout"
.addLogoutHandler(tokenClearLogoutHandler())
.logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler())
.and()
.addFilterBefore(springSessionRepositoryFilter(), SessionManagementFilter.class)
.sessionManagement().disable()
; } @Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(
"/api/user/login/v1",
"/v2/api-docs",
"/swagger-resources/configuration/ui",
"/swagger-resources",
"/swagger-resources/configuration/security",
// "/api/user/say/hello/v1",
// "/api/user/add/v1",
// "/api/user/speed/test/v1",
// "/api/user/*/detail/v1",
"/images/**",
"/swagger-ui.html",
"/webjars/**",
"**/favicon.ico",
"/css/**",
"/js/**",
"/api/crazymaker/info/user/login"
); }
///....
}

生产环境上,需要在 反向代理Nginx上,将swagger的路径,进行拒绝

API整合之结果查看

运行相关服务和zuul-swagger网关,输入:http://192.168.233.1:7799/swagger-ui.html

具体,请关注 Java 高并发研习社群博客园 总入口


最后,介绍一下疯狂创客圈:疯狂创客圈,一个Java 高并发研习社群博客园 总入口

疯狂创客圈,倾力推出:面试必备 + 面试必备 + 面试必备 的基础原理+实战 书籍 《Netty Zookeeper Redis 高并发实战


疯狂创客圈 Java 死磕系列

  • Java (Netty) 聊天程序【 亿级流量】实战 开源项目实战
  • Netty 源码、原理、JAVA NIO 原理
  • Java 面试题 一网打尽

Zuul Swagger 整合的更多相关文章

  1. SpringBoot+Swagger整合API

    SpringBoot+Swagger整合API Swagger:整合规范的api,有界面的操作,测试 1.在pom.xml加入swagger依赖 <!--整合Swagger2配置类--> ...

  2. springboot+jpa+mysql+swagger整合

    Springboot+jpa+MySQL+swagger整合 创建一个springboot web项目 <dependencies> <dependency>      < ...

  3. SpringBoot与Swagger整合

    1 SpringBoot与Swagger整合https://blog.csdn.net/jamieblue1/article/details/99847744 2 Swagger详解(SpringBo ...

  4. Swagger整合Jwt授权配置

    Swagger整合Jwt授权配置 欢迎关注博主公众号「Java大师」, 专注于分享Java领域干货文章http://www.javaman.cn/sb2/swagger-jwt 一.Swagger入门 ...

  5. springcloud+zuul+swagger 分布式接口文档

    https://gitee.com/didispace/swagger-butler 1.引用上面项目中的swagger 工具包 2.zuul 网关配置 zuul.routes.api-apiserv ...

  6. springboot+jpa+mysql+redis+swagger整合步骤

    springboot+jpa+MySQL+swagger框架搭建好之上再整合redis: 在电脑上先安装redis: 一.在pom.xml中引入redis 二.在application.yml里配置r ...

  7. Maven+SpringMVC+SpringFox+Swagger整合示例

    查考链接:https://my.oschina.net/wangmengjun/blog/907679 coding地址:https://git.coding.net/conding_hjy/Spri ...

  8. 一步步完成Maven+SpringMVC+SpringFox+Swagger整合示例

    本文给出一个整合Maven+SpringMVC+SpringFOX+Swagger的示例,并且一步步给出完成步骤. 本人在做实例时发现 http://blog.csdn.net/zth1002/art ...

  9. SpringMVC、SpringFox和Swagger整合项目实例

    目标 在做项目的时候,有时候需要提供其它平台(如业务平台)相关的HTTP接口,业务平台则通过开放的HTTP接口获取相关的内容,并完成自身业务~ 提供对外开放HTTP API接口,比较常用的是采用Spr ...

随机推荐

  1. 简单地认识一下 HTML

    简单复盘一下 HTML. 1.HTML 什么是 HTML?HTML 是 Hyper Text Markup Language 的简写,译成中文是「超文本标记语言」. 顾名思义,超文本,就是不止于文本, ...

  2. No provider available for the service com.xxx.xxx 错误解决

    HTTP Status 500 - Servlet.init() for servlet springmvc threw exception type Exception report message ...

  3. react中简单倒计时跳转

    其实在react中实现倒计时的跳转方法有很多中,其中我认为较为好用的就是通过定时器更改state中的时间值. 首先在constructor中设置10秒的时间值: constructor () { su ...

  4. JSON.parse() 报错和一些解决方法

    js 报错 Unexpected end of JSON input,Unexpected token u in JSON at position 0 JSON 通常用于与服务端交换数据. 在接收服务 ...

  5. php实现微信拼手气红包

    $result = sendHB(3, 5); echo '<pre>'; var_export($result); echo array_sum($result); /** * 拼手气红 ...

  6. JavaEE基础(01):Servlet实现方式,生命周期执行过程

    本文源码:GitHub·点这里 || GitEE·点这里 一.Servlet简介 Java编写的服务器端程序,具有独立于平台和协议的特性,主要功能在于交互式地浏览和生成数据,生成动态Web内容.使用S ...

  7. 微信小程序——template详细使用

    WXML提供模板(template),可以在模板中定义代码片段,然后在不同的地方调用减少冗余代码. 1.1定义模板 1.1.1.创建模板文件夹  1.1.2.使用 name 属性,作为模板的名字.然后 ...

  8. DRF Django REST framework 之 解析器(二)

    引入 Django Rest framework帮助我们实现了处理application/json协议请求的数据,如果不使用DRF,直接从 request.body 里面拿到原始的客户端请求的字节数据 ...

  9. luogu P1336 最佳课题选择 |背包dp

    题目描述 Matrix67要在下个月交给老师n篇论文,论文的内容可以从m个课题中选择.由于课题数有限,Matrix67不得不重复选择一些课题.完成不同课题的论文所花的时间不同.具体地说,对于某个课题i ...

  10. 浏览器主页锁定之战——IE:我太难了

    精彩回顾: 我是一个explorer的线程 我是一个杀毒软件线程 我是一个IE浏览器线程 比特宇宙-TCP/IP的诞生 产品vs程序员:你知道www是怎么来的吗? Hello, World! 我是一个 ...