要求:
有一定的swagger基础,java基础,springboot基础,springcloud集群。
单节点swgger2地址
https://blog.csdn.net/qq_16855077/article/details/103123407
随着时代的发现,我们的项目也从以前的,单节点项目(所有功能都向一个项目中堆,维护性差),最近几年,微服务使用的人群越越来越广,一个简单的电影系统,我们也可以按模块进行切换,例如,分为订单模块,电影模块,支付模块,会员模块等等。
而文档维护起来的成本也越来越高,有时候,我们一个系统,就可以拆分成上100个服务,这是,我们的文档如何维护了?前面的博客都是单节点的swagger,假设,我们有100个服务,我们搭建100个swagger,那就得有100个网站,这我们开发人员每天都得说,我太难勒。针对这种情况,我们只能通过swagger集群的方式来解决。
com.spring4all
spring-boot-starter-swagger
1.5.1.RELEASE
这是springboot集成的一个start类。不然,我们这里就需要引入两个依赖。
####服务器端口号
server:
port: 8001
###服务别名---服务器注册到注册中心的名称
spring:
application:
name: fqyd-member
eureka:
client:
###需要将我的服务注册到eureka上
registerWithEureka: true
###检索服务
fetchRegistry: true
###当前服务注册到eureka服务地址
serviceUrl:
defaultZone: http://localhost:8100/eureka/
####扫描swagger注解
swagger:
base-package: com.fqyd
1. fqyd-member----------------服务名,后续会用到。
2.项目开发过程中使用到了euraka,这里就不过多阐述,不了解的可以查看(一般项目调用都是通过ip+端口等调用,这里使用euraka的目的是,通过服务名和ip进行绑定,映射到eurake server上,我们通过服务名,实际上就是访问的ip)
https://blog.csdn.net/qq_16855077/article/details/90752257
3.swagger的一些配置,swagger+springboot启动器,会帮我们默认设置,因本博客是测试,所以这里也只设置勒包扫描区间。
package com.fqyd.api.service.impl;
import com.fqyd.api.entity.User;
import com.fqyd.api.service.IMemberService;
import com.fqyd.util.BaseApiService;
import com.fqyd.util.BaseDataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Description:
* Author: wude
* Date: 2019/7/25 15:36
* Modified By:
*/
@RestController
@Api("会员服务接口")
public class MemberServiceImpl extends BaseApiService implements IMemberService {
@Value("${server.port}")
private Integer serverPort;
@ApiOperation("swagger")
@ApiImplicitParam(name="name",value="用户名",dataType="String", paramType = "query")
@PostMapping("/hello")
public String hello(String name){
System.out.println("hello swagger"+name);
return "hello swagger "+name;
};
@RequestMapping("getMember")
@Override
public User getMember(@RequestParam("name") String name) {
User user = new User();
user.setName("通过端口:"+serverPort+","+name);
user.setAge(18);
return user;
}
@RequestMapping("getUserInfo")
@Override
public BaseDataResult getUserInfo() {
try {
//延迟1.5s
Thread.sleep(1500);
} catch (Exception e) {
}
return setResultSuccess("订单服务调用会员接口成功...");
}
}
package com.fqyd;
import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* Description:
* Author: wude
* Date: 2019/7/25 16:49
* Modified By:
*/
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableSwagger2Doc
public class MemberServiceImplApplication {
public static void main(String[] args) {
SpringApplication.run(MemberServiceImplApplication.class,args);
}
}
@EnableSwagger2Doc 开启swagger文档
实现的方式跟会员服务类似,增加订单服务,只是为了延迟分布式swagger效果。
org.springframework.cloud
spring-cloud-starter-netflix-zuul
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
com.spring4all
spring-boot-starter-swagger
1.5.1.RELEASE
####服务器端口号
server:
port: 82
###服务别名---服务器注册到注册中心的名称
spring:
application:
name: zuul-service
eureka:
client:
###需要将我的服务注册到eureka上
registerWithEureka: true
###检索服务
fetchRegistry: true
###当前服务注册到eureka服务地址
serviceUrl:
defaultZone: http://localhost:8100/eureka/
##设置feign超时时间
ribbon:
##指的是创建连接所用的时间,适用于网络状况正常的情况下,两端所用的时间,默认是1s
ReadTimeout: 3000
##指的是创建连接后从服务器读取可用资源所用的时间
ConnectTimeout: 3000
##开启Hystrix断路器
feign:
hystrix:
enabled: true
zuul:
routes:
member:
##类似于转发
path: /member/**
serviceId: fqyd-member
order:
path: /order/**
serviceId: fqyd-order
网关详情: https://blog.csdn.net/qq_16855077/article/details/102960033
package com.fqyd;
import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
/**
* Description:
* Author: wude
* Date: 2019/7/25 16:49
* Modified By:
*/
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
@EnableSwagger2
public class ZuulServiceImplApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulServiceImplApplication.class,args);
}
@Component
@Primary
class DocumentationConfig implements SwaggerResourcesProvider {
@Override
public List get() {
List resources = new ArrayList();
resources.add(swaggerResource("会员服务Api","/member/v2/api-docs","2.0"));
resources.add(swaggerResource("订单服务Api","/order/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;
}
}
}
member和order不是随便定义的,而是根据yml里面zuul的路由决定的。
注意:zuul中需要使用@EnableSwagger2,而不能使用@EnableSwagger2Doc,不然会报一个错误。
https://blog.csdn.net/qq_16855077/article/details/103146969
这两个服务显示的界面,都是通过一个网址打开的,就这样大功告成勒!
如果你热衷技术,喜欢交流,欢迎加入我们!