springcloud剛推出的時候用的是netflix全家桶,路由用的zuul(springcloud-路由Zull),可是聽說zull1.0在大數據量訪問的時候存在較大性能問題,2.0就沒集成到springcloud中了,springcloud推出了本身的路由-springcloud gateway,親兒子,目前官網主推。html
netfelix的zull路由:java
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
在springcloud項目中,新建的項目所有是基於springboot的(springboot與springcloud的關係),新建springboot項目再囉嗦一遍吧(springboot項目快速搭建)spring
next->next 更改下項目名稱。apache
(1)註冊中心客戶端springboot
(2) 選擇springcloud gatewayapp
next->next, 完成。maven
開始介紹springcloud-gateway,爲了區分上一篇的關於zull的介紹,項目名稱設置爲springgate。spring-boot
<?xml version="1.0" encoding="UTF-8"?> <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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.yutong</groupId> <artifactId>springgateway</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springgateway</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
說明,重點就兩個starter:性能
(1)gateway,不一樣於zull,這個是直接位於spring-cloud下面,是springcloud的嫡系部隊。大數據
(2)註冊中心客戶端,很少說了
package com.yutong.springgateway; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; @SpringBootApplication public class SpringgatewayApplication { @Value("${test.uri}") private String uri; @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() //basic proxy .route(r -> r.path("/**") .uri(uri) ).build(); } public static void main(String[] args) { SpringApplication.run(SpringgatewayApplication.class, args); } }
說明:
(1)不一樣於路由zull,須要zull的標籤,這裏只須要springboot的啓動標籤@SpringBootApplication就能夠了。
(2)增長@Bean標籤,RouteLocator 是路由規則,這個稍微複雜點,示例中只作了簡單配置,路由規則用於過濾控制路由等,可根據具體需求,參考路由規則API進行設置。
test: uri: lb://client spring: application: name: springgateway cloud: gateway: discovery: locator: lower-case-service-id: true eureka: client: service-url: defaultZone: http://localhost:8761/eureka server: port: 9000
說明:
(1)第一行的uri在啓動類application中用到,指訪問的客戶端服務,client爲註冊中心客戶端服務名稱。
(2)defaultZone爲註冊中心地址。
(3) 爲了區分介紹zull的項目,name設置爲:springgateway
(1)註冊中心
(2)路由springcloud gateway訪問
說明:
路由訪問的時候不須要指定客戶端名稱client,由於在gateway啓動類中已經指定過了。
I'm 軟件老王,若是以爲還能夠的話,關注下唄!若有不許確或疑問的地方,可經過討論區、QQ溝通,多謝!
原文出處:https://www.cnblogs.com/ruanjianlaowang/p/11197118.html