SpringCloud 閘道器配置:spring-cloud-gateway
微服務閘道器
微服務閘道器的功能:
路由轉發,接收一切外界請求,轉發到後端的微服務上去;
請求過濾,在服務閘道器中可以完成一系列的橫切功能,例如許可權校驗、限流以及監控等,這些都可以通過過濾器完成(其實路由轉發也是通過過濾器實現的)
微服務閘道器種類
Zuul,spring-cloud-gateway,linkerd,Nignx,還有lyft的Envoy,Undertow等等備用方案。
主要的為前4個工具:
Zuul, zuul 1.x 是一個基於阻塞IO的API 閘道器。提供動態路由,監控,彈性,安全等的邊緣服務。是Netflix出品的一個基於JVM路由和服務端的負載均衡器。zuul 2.x 基於Netty,實現了非阻塞IO,支援長連線的API閘道器。SpringCloud暫時還沒有整合計劃。
SpringCloud-gateway: 非阻塞的IO 的API閘道器。上手簡單,功能強大。
Linkerd ,基於Scala實現的,目前是市面上僅有生產級別的Service Mesh。
Nginx,不說了。
本文主要介紹spring-cloud-starter-gateway的搭建。如果想看高階的教程的話:
http://www.iocoder.cn/categories/Spring-Cloud-Gateway/
簡單的環境搭建(基於Spring-boot-starter-parent,2.0.0.RELEASE)
1.引用依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.isomorphism</groupId> <artifactId>token-bucket</artifactId> <version>1.7</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-reflect</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> <configuration> <args> <arg>-Xjsr305=strict</arg> </args> <jvmTarget>1.8</jvmTarget> </configuration> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> <configuration> <sourceDirs> <source>src/main/java</source> <source>src/main/kotlin</source> </sourceDirs> </configuration> </execution> <execution> <id>test-compile</id> <phase>test-compile</phase> <goals> <goal>test-compile</goal> </goals> <configuration> <sourceDirs> <source>src/test/java</source> <source>src/test/kotlin</source> </sourceDirs> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <compilerArgs> <arg>-parameters</arg> </compilerArgs> </configuration> <executions> <!-- Replacing default-compile as it is treated specially by maven --> <execution> <id>default-compile</id> <phase>none</phase> </execution> <!-- Replacing default-testCompile as it is treated specially by maven --> <execution> <id>default-testCompile</id> <phase>none</phase> </execution> <execution> <id>java-compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>java-test-compile</id> <phase>test-compile</phase> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build>
2.啟動類編寫
@SpringBootConfiguration
@EnableAutoConfiguration
public class GatewaySampleApplication {
public static void main(String[] args) {
SpringApplication.run(GatewaySampleApplication.class, args);
}
}
3.編寫配置檔案
spring: cloud: gateway: default-filters: - AddResponseHeader=X-Response-Default-Foo, Default-Bar - AddRequestHeader=fangxiaobai,its sound good - PrefixPath=/httpbin routes: - id: fangxiaobai_test host: 127.0.0.1 # 除了ws,還有lb,不知道什麼意思 uri: http://www.baidu.com/ predicates: - Host=127.0.0.1 - Method=GET - Path=/web filters: - AddResponseHeader=X-Response-Foo, Bar - SetPath=/web
詳細:http://www.iocoder.cn/Spring-Cloud-Gateway/route-definition-locator-properties/
https://github.com/YunaiV/spring-cloud-gateway/blob/382a4cd98fbb8ac53a83a5559bacb0f885838074/spring-cloud-gateway-core/src/test/resources/application.yml#L10
以上這種方式是使用配置檔案的形式來配置路由。除了這種方式,還有供API的形式和kotlin的形式來配置,
對於路由的儲存,還有從配置檔案中讀取,從儲存器中讀取,從註冊中心中讀取,也可以組成以上這三種形式的讀取路由。
接下來記錄一下,從註冊中心讀取。
非常簡單,在配置檔案中加入如下配置就可;
cloud:
gateway:
discovery:
locator:
enabled: true
當然你還得有一個註冊服務中心,你可以手動搭建一個,也可以。。。。使用這個http://eureka.didispace.com/
今天用了一天的時間,學習了這些內容,其他內容後續補充,,,,,
學習內容:http://www.iocoder.cn/categories/Spring-Cloud-Gateway/
學習內容,,記錄下。