前面一篇已經簡單了講了Eureka Zuul Ribbon Feign,這章只單獨講Gateway 以及Gateway 和Zuul的區別,
Gateway簡介:
Spring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技術開發的網關,Spring Cloud Gateway旨在為微服務架構提供一種簡單而有效的統一的API路由管理方式,Spring Cloud Gateway作為Spring Cloud生態系中的網關,目標是替代ZUUL,其不僅提供統一的路由方式,并且基于Filter鏈的方式提供了網關基本的功能,例如:安全,監控/埋點,和限流等,
Spring Cloud Gateway 可以看做是一個 Zuul 1.x 的升級版和代替品,比 Zuul 2 更早的使用 Netty 實作異步 IO,從而實作了一個簡單、比 Zuul 1.x 更高效的、與 Spring Cloud 緊密配合的 API 網關,
Spring Cloud Gateway 里明確的區分了 Router 和 Filter,并且一個很大的特點是內置了非常多的開箱即用功能,并且都可以通過 SpringBoot 配置或者手工編碼鏈式呼叫來使用,
比如內置了 10 種 Router,使得我們可以直接配置一下就可以隨心所欲的根據 Header、或者 Path、或者 Host、或者 Query 來做路由,
比如區分了一般的 Filter 和全域 Filter,內置了 20 種 Filter 和 9 種全域 Filter,也都可以直接用,當然自定義 Filter 也非常方便
gateway與zuul的區別:
Gateway:內部實作了限流,負載均衡的功能,擴展性更強
限制僅適合于spring cloud 套件
支持異步
Zuul: 可以擴展至其他微服務框架中
內部沒有實作限流,負載均衡等功能
2.x系列非阻塞異步模式,1.x系列同步模式
單機Demo創建:
1. 新建maven專案huaun-eugt


2. 新建eureka模塊



新建eureka模塊后,查看eureka模塊的pom.xml的組態檔:
<?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 https://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.3.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.huaun</groupId> <artifactId>eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka</name> <description>eureka</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR9</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </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>
將eureka模塊中的application.properties檔案改成application.yml檔案,并進行如下配置
server: port: 8000 spring: application: name: eureka-service eureka: instance: hostname: localhost client: fetch-registry: false register-with-eureka: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka server: peer-node-connect-timeout-ms: 300
@EnableEurekaServer注解eureka塊中的EurekaApplication啟動類
package com.huaun.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
啟動Eureka服務,瀏覽器輸入http://localhost:8000/可以得到如下圖結果

3. 新建微服務userapi模塊(不含 Feign)



新建userapi模塊后,查看userapi模塊的pom.xml的組態檔:
<?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 https://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.3.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.huaun</groupId> <artifactId>userapi</artifactId> <version>0.0.1-SNAPSHOT</version> <name>userapi</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR9</spring-cloud.version> </properties> <dependencies> <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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </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>
將userapi模塊中的application.properties檔案改成application.yml檔案,并進行如下配置:
server: port: 7000 spring: application: name: user-service eureka: client: service-url: defaultZone: http://localhost:8000/eureka #當有多個eureka server時,以逗號隔開 register-with-eureka: true fetch-registry: true registry-fetch-interval-seconds: 30 #每30s獲取服務器串列間隔 instance: prefer-ip-address: true #將當前實體的ip注冊到eureka server中,默認是false,注冊主機名 lease-expiration-duration-in-seconds: 9 #如果9秒沒有發心跳包,服務器就會干掉該服務 lease-renewal-interval-in-seconds: 3 #每隔3秒發一次那條
@EnableDiscoveryClient注解userapi塊中的UserapiApplication啟動類
package com.huaun.userapi; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class UserapiApplication { public static void main(String[] args) { SpringApplication.run(UserapiApplication.class, args); } }
在userapi模塊中創建restful介面
package com.huaun.userapi; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class UserapiApplication { public static void main(String[] args) { SpringApplication.run(UserapiApplication.class, args); } }
3. 新建微服務模塊:userapi01,userapi02
創建方式、resultful介面,以及配置 均和userapi一樣,
配置不同的地方是:userapi01中application.yml中的server.port =7002
userapi02中application.yml中的server.port=7003
userapi01中的controller中的name:user01改為name:user02
userapi02中的controller中的name:user01改為name:user03
4. 新建微服務模塊systemapi (包含fegin,用于呼叫userapi,userapi01,userapi02模塊中的介面)



systemapi模塊的pom.xml的組態檔:
<?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 https://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.3.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.huaun</groupId> <artifactId>systemapi</artifactId> <version>0.0.1-SNAPSHOT</version> <name>systemapi</name> <description>systemapi</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR9</spring-cloud.version> </properties> <dependencies> <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-web</artifactId> </dependency> <!--將服務提供者偽裝成服務消費者 呼叫其他服務提供者的微服務--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </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>
將systemapi模塊中的application.properties檔案改成application.yml檔案,并進行如下配置
server: port: 7009 spring: application: name: system-service eureka: instance: prefer-ip-address: true client: fetch-registry: true register-with-eureka: true service-url: defaultZone: http://localhost:8000/eureka feign: client: config: default: connectTimeout: 5000 readTimeout: 5000 loggerLevel: basic okhttp: enabled: true hystrix: enabled: true
@EnableDiscoveryClient @EnableFeignClients注解systemapi塊中的SystemapiApplication啟動類
package com.huaun.systemapi; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class SystemapiApplication { public static void main(String[] args) { SpringApplication.run(SystemapiApplication.class, args); } }
創建controller類和Feign客戶端類:
package com.huaun.systemapi; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class SystemapiApplication { public static void main(String[] args) { SpringApplication.run(SystemapiApplication.class, args); } }
package com.huaun.systemapi.controller; import com.huaun.systemapi.feign.UserFeignClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DeptController { @Autowired private UserFeignClient userFeignClient; @GetMapping("getDeptInfo") public String getDeptInfo(){ String userInfo= userFeignClient.getUserInfo(); return userInfo+",所屬部門:研發部"; } }
4. 新建客戶端路由模塊gateway(包含Ribbon 負載均衡)



Gateway模塊的pom.xml的組態檔:
<?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 https://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.3.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.huaun</groupId> <artifactId>gateway</artifactId> <version>0.0.1-SNAPSHOT</version> <name>gateway</name> <description>gateway</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR9</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!--負載均衡 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </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>
將Gatway模塊中的application.properties檔案改成application.yml檔案,并進行如下配置:
server: port: 8080 spring: application: name: gate-service cloud: discovery: enabled: true gateway: default-filters: routes: - id: user-api-routs uri: lb://user-service #注冊中心的serverId predicates: - Path=/user-service/** filters: # 定義過濾規則 - StripPrefix=1 # 轉發后去掉一個前綴 /ms-provider - id: system-api-routs uri: lb://system-service #注冊中心的serverId predicates: - Path=/system-service/** filters: # 定義過濾規則 - StripPrefix=1 # 轉發后去掉一個前綴 /ms-provider user-service: #負載均衡 ribbon: listOfServers: localhost:7000, localhost:7001,localhost:7002 NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule eureka: instance: prefer-ip-address: true client: fetch-registry: true register-with-eureka: true service-url: defaultZone: http://localhost:8000/eureka
@EnableDiscoveryClient 注解gateway塊中的GatewayApplication啟動類
package com.huaun.gateway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
依次啟動gateway,userapi01,userapi02,systemapi模塊服務:
重繪http://localhost:8000,如下圖:

訪問userapi:http://localhost:7001/getUserInfo

訪問userapi01:http://localhost:7002/getUserInfo

訪問userapi02:http://localhost:7003/getUserInfo

訪問systemapi:http://localhost:7009/getDeptInfo

通過路由的負載均衡訪問userapi模塊:http://localhost:8080/user-service/getUserInfo

通過路由訪問systemapi模塊:http://localhost:8080/system-service/getDeptInfo

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/249462.html
標籤:其他
上一篇:MyBatis 查詢的時候屬性名和欄位名不一致的問題
下一篇:資料庫主從復制
