互联网公司中网关都是集群
Zuul如何搭建集群版本 Nginx+Zuul 一主一备 或者轮询多个
在微服务中,所有服务请求都会统一请求到Zuul网关上
#### 上游服务器 默认集群
upstream backServer{
server 127.0.0.1:81;
server 127.0.0.1:82;
}
server {
listen 80;
server_name wg.itmayiedu.com;
location / {
### 指定上游服务器负载均衡服务器
proxy_pass http://backServer/;
index index.html index.htm;
}
}
127.0.0.1 wg.itmayiedu.com
启动nginx->启动注册中心-> 启动网关服务-> 启动会员服务
访问路径:http://wg.itmayiedu.com/api-member?userToken=111
[外链图片转存失败(img-1jntHcux-1569486523801)(images/1569380470162.png)]
Nginx与网关区别: (Nginx也是可以实现网关,那为什么不用Nginx实现网关呢)
因为微服务网关是针对整个微服务实现统一请求拦截,网关基本上都是采用自己熟悉的语言开发的,目的是方便易学
课题引入
随着微服务架构体系的发展和应用, 为了前后端能够更好的集成与对接,同时为了项目的方便交付,每个项目都需要提供相应的API文档。
来源:PC端、微信端、H5端、移动端(安卓和IOS端)
传统的API文档编写存在以下几个痛点:
对API文档进行更新的时候,需要通知前端开发人员,导致文档更新交流不及时;
API接口返回信息不明确
大公司中肯定会有专门文档服务器对接口文档进行更新。
缺乏在线接口测试,通常需要使用相应的API测试工具,比如postman、SoapUI等
接口文档太多,不便于管理
为了解决传统API接口文档维护的问题,为了方便进行测试后台Restful接口并实现动态的更新,因而引入Swagger接口工具。
Swagger具有以下优点
创建springboot项目 springboot-swagger
添加maven依赖信息
<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>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.1.RELEASEversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>springboot-swaggerartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>2.8.0version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>2.8.0version>
dependency>
dependencies>
<repositories>
<repository>
<id>spring-milestonesid>
<name>Spring Milestonesname>
<url>https://repo.spring.io/libs-milestoneurl>
<snapshots>
<enabled>falseenabled>
snapshots>
repository>
repositories>
project>
创建 SwaggerConfig
package com.itmayiedu.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
// @EnableSwagger2 表示开启 Swagger功能
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
// 生成API扫包范围
.apis(RequestHandlerSelectors.basePackage("com.itmayiedu.api")).paths(PathSelectors.any()).build();
}
// 创建API文档信息
private ApiInfo apiInfo() {
// title 文档标题
// description 描述信息
// termsOfServiceUrl 网址
// version 版本号
return new ApiInfoBuilder().title("每特教育|蚂蚁课堂 微服务电商系统").description("每特教育|蚂蚁课堂 Java分布式&微服务培训")
.termsOfServiceUrl("http://www.itmayiedu.com")
// .contact(contact)
.version("1.0").build();
}
}
添加application.yml
###服务启动端口号
server:
port: 8011
spring:
application:
name: springboot-swagger
添加测试接口类
package com.itmayiedu.api;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@Api("SwaggerDemo控制器")
@RestController
public class SwaggerController {
@ApiOperation("swagger演示服务接口")
@GetMapping("/swaggerIndex")
public String swaggerIndex (){
System.out.println("swaggerIndex");
return "swaggerIndex";
}
@ApiOperation("获取会员相关信息")
@ApiImplicitParam(name = "userName", value = "用户姓名参数", required = true, dataType = "String")
@PostMapping("/getMember")
public String getMember(String userName){
System.out.println( "userName: " + userName);
return "userName: " + userName;
}
}
创建启动类
package com.itmayiedu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AppSwagger {
public static void main(String[] args) {
SpringApplication.run(AppSwagger.class, args);
}
}
启动项目 访问: http://localhost:8011/swagger-ui.html#
[外链图片转存失败(img-lnOcUtDJ-1569486523802)(images/1569458118647.png)]
在我们微服务中,Swagger是每个服务(比如会员服务、订单服务、支付服务) 进行集成
如何将整个微服务中的Swagger进行合成,在同一台服务上面 叫做 Swagger集群
SpringBoot支持对Swagger管理,只需要Zuul网关 添加对应服务Swagger文档即可
[外链图片转存失败(img-WZK6law8-1569486523803)(images/1569482858661.png)]
会员服务和订单服务引入Maven依赖
<dependency>
<groupId>com.spring4allgroupId>
<artifactId>swagger-spring-boot-starterartifactId>
<version>1.7.0.RELEASEversion>
dependency>
会员服务和订单服务修改application.yml
swagger:
base-package: com.itmayeidu.api
会员服务和订单服务启动类添加注解@EnableSwagger2Doc+
package com.itmayiedu;
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;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableSwagger2Doc
public class AppMember {
public static void main(String[] args) {
SpringApplication.run(AppMember.class, args);
}
}
package com.itmayiedu.api;
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.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
@EnableSwagger2Doc
public class AppOrder {
public static void main(String[] args) {
SpringApplication.run(AppOrder.class, args);
}
}
ZuulGateway网关服务添加maven依赖
<dependency>
<groupId>com.spring4allgroupId>
<artifactId>swagger-spring-boot-starterartifactId>
<version>1.7.0.RELEASEversion>
dependency>
修改Zuul启动类
package com.itmayiedu;
import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
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;
// @EnableZuulProxy 表示开启网关代理
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableSwagger2Doc
public class AppGateWay {
public static void main(String[] args) {
SpringApplication.run(AppGateWay.class, args);
}
// zuul配置能够使用config实现实时更新
@RefreshScope
@ConfigurationProperties("zuul")
public ZuulProperties zuulProperties() {
return new ZuulProperties();
}
@Component
@Primary
class DocumentationConfig implements SwaggerResourcesProvider {
@Override
public List<SwaggerResource> get() {
List resources = new ArrayList<>();
// app-itmayiedu-order
resources.add(swaggerResource("app-itmayiedu-member", "/api-member/v2/api-docs", "2.0"));
resources.add(swaggerResource("app-itmayiedu-order", "/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;
}
}
}
访问地址: http://localhost:82/swagger-ui.html#/
[外链图片转存失败(img-ZdBHAWYa-1569486523803)(images/1569486479690.png)]