SpringCloudGateway開發(fā)過程解析
路由簡介:
1.SpringCloudGateWay 是用于替代zuul作為API網(wǎng)關(guān),在gateway中有三個重要的名詞:過濾器,斷言,路由
過濾器與斷言是路由的一部分,路由便是將請求進(jìn)行一系列的處理后分發(fā)到各個服務(wù)的一個過程。
路由的過程:首先會加載斷言以及路由,在接受到請求后根據(jù)斷言加載的順序會匹配到先加載的斷言,只有與斷言匹配了的請求才會進(jìn)入路由,沒有匹配到的服務(wù)會將請求當(dāng)成普通的訪問請求。
2:路由加載斷言的方式:
斷言加載的方式有四種,分別是配置文件,java編碼,數(shù)據(jù)庫以及注冊中心
第一種配置文件:
在官方文檔中主要介紹的就是配置文件的加載方式
官方地址:https://cloud.spring.io/spring-cloud-gateway/reference/html/#gateway-starter
一般的斷言有三種要素:id,uri,predicate.
id是斷言的標(biāo)識,uri是ip+端口,predicate則是斷言匹配的規(guī)則
3:示例:
新建一個springboot項目,并且引入springcloudgateway的依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
在啟動類注冊三個全局過濾器
@SpringBootApplication
public class GateWayApplication {
public static void main(String[] args) {
SpringApplication.run(GateWayApplication.class, args);
}
@Bean
@Order(-1)
public GlobalFilter a() {
return (exchange, chain) -> {
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
System.out.println(-1);
}));
};
}
@Bean
@Order(0)
public GlobalFilter b() {
return (exchange, chain) -> {
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
System.out.println(0);
}));
};
}
@Bean
@Order(1)
public GlobalFilter c() {
return (exchange, chain) -> {
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
System.out.println(1);
}));
};
}
}
在配置文件類配置兩條路由
server.port: 7777
spring:
application:
name: gateway
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: method_route
uri: http://127.0.0.1:9999
predicates:
- Method=GET
- id: method_route
uri: http://127.0.0.1:8006
predicates:
- Method=GET
發(fā)送請求,請求到達(dá)后匹配的是第一條路由,由此可以知道路由匹配的順序會根據(jù)加載的順序來

4:SpringCloudGateWay從注冊中心獲得路由
在官方文檔中,我們可以看到有這樣的一段話
Configuring Predicates and Filters For DiscoveryClient Routes
By default the Gateway defines a single predicate and filter for routes created via a DiscoveryClient.
The default predicate is a path predicate defined with the pattern /serviceId/**, where serviceId is the id of the service from the DiscoveryClient.
The default filter is rewrite path filter with the regex /serviceId/(?<remaining>.*) and the replacement /${remaining}. This just strips the service id from the path before the request is sent downstream.
If you would like to customize the predicates and/or filters used by the DiscoveryClient routes you can do so by setting spring.cloud.gateway.discovery.locator.predicates[x] and spring.cloud.gateway.discovery.locator.filters[y]. When doing so you need to make sure to include the default predicate and filter above, if you want to retain that functionality. Below is an example of what this looks like.
地址 :https://cloud.spring.io/spring-cloud-gateway/reference/html/#_global_filters
spring.cloud.gateway.discovery.locator.predicates[0].name: Path
spring.cloud.gateway.discovery.locator.predicates[0].args[pattern]: "'/'+serviceId+'/**'"
spring.cloud.gateway.discovery.locator.predicates[1].name: Host
spring.cloud.gateway.discovery.locator.predicates[1].args[pattern]: "'**.foo.com'"
spring.cloud.gateway.discovery.locator.filters[0].name: Hystrix
spring.cloud.gateway.discovery.locator.filters[0].args[name]: serviceId
spring.cloud.gateway.discovery.locator.filters[1].name: RewritePath
spring.cloud.gateway.discovery.locator.filters[1].args[regexp]: "'/' + serviceId + '/(?<remaining>.*)'"
spring.cloud.gateway.discovery.locator.filters[1].args[replacement]: "'/${remaining}'"
根據(jù)文檔介紹,依照這種方式,可以從注冊中心獲得斷言與過濾器的配置
5:SpringGateWay從數(shù)據(jù)庫配置路由
public class DBRouteDefinitionRepository implements RouteDefinitionRepository
項目中實現(xiàn)了RouteDefinitionRepository后,springgateway會采用你實現(xiàn)的這個類去加載路由,如果不實現(xiàn)則采用他默認(rèn)的方式加載路由
public class DBRouteDefinitionRepository implements RouteDefinitionRepository {
//保存路由
private final Map<String, RouteDefinition> routes = synchronizedMap(new LinkedHashMap<String, RouteDefinition>());
private Logger log = LoggerFactory.getLogger(DBRouteDefinitionRepository.class);
//初始標(biāo)準(zhǔn)
private boolean init_flag = true;
//
private final GatewayProperties properties;
private DynamicRouteServiceImpl service;
public DBRouteDefinitionRepository(GatewayProperties properties) {
this.properties = properties;
this.service = new DynamicRouteServiceImpl();
}
@Override
public Flux<RouteDefinition> getRouteDefinitions() {
if(init_flag) {
List<RouteDefinition> routeDefinitions = new ArrayList<>();
List<RouteDefinition> rs = new ArrayList<>();
try {
routeDefinitions = service.quertAllRoutes();//從數(shù)據(jù)庫中加載route
rs = this.properties.getRoutes();//獲得配置文件的route
for (RouteDefinition rse : rs) {
routeDefinitions.add(rse);
}
routes.clear();
routeDefinitions.forEach(x->routes.put(x.getId(), x));
init_flag=false;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error("Init Route Fail,Can't get Routes.",e);
}
return Flux.fromIterable(routeDefinitions);
}else {
return Flux.fromIterable(routes.values());
}
}
@Override
public Mono<Void> delete(Mono<String> routeId) {
return routeId.flatMap(id -> {
if (routes.containsKey(id)) {
routes.remove(id);
return Mono.empty();
}
return Mono.defer(() -> Mono.error(new NotFoundException("RouteDefinition not found: "+routeId)));
});
}
@Override
public Mono<Void> save(Mono<RouteDefinition> route) {
return route.flatMap( r -> {
routes.put(r.getId(), r);
return Mono.empty();
});
}
}
這個是我自己實現(xiàn)的類,這個類可以從數(shù)據(jù)庫與配置文件中獲得路由配置,從數(shù)據(jù)庫中獲得路由配置可以根據(jù)個人的要求來
@Validated
public class RouteDefinition {
@NotEmpty
private String id = UUID.randomUUID().toString();
@NotEmpty
@Valid
private List<PredicateDefinition> predicates = new ArrayList<>();
@Valid
private List<FilterDefinition> filters = new ArrayList<>();
@NotNull
private URI uri;
private int order = 0;
public RouteDefinition() {
}
public RouteDefinition(String text) {
int eqIdx = text.indexOf('=');
if (eqIdx <= 0) {
throw new ValidationException("Unable to parse RouteDefinition text '" + text
+ "'" + ", must be of the form name=value");
}
setId(text.substring(0, eqIdx));
String[] args = tokenizeToStringArray(text.substring(eqIdx + 1), ",");
setUri(URI.create(args[0]));
for (int i = 1; i < args.length; i++) {
this.predicates.add(new PredicateDefinition(args[i]));
}
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<PredicateDefinition> getPredicates() {
return predicates;
}
public void setPredicates(List<PredicateDefinition> predicates) {
this.predicates = predicates;
}
public List<FilterDefinition> getFilters() {
return filters;
}
public void setFilters(List<FilterDefinition> filters) {
this.filters = filters;
}
public URI getUri() {
return uri;
}
public void setUri(URI uri) {
this.uri = uri;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RouteDefinition routeDefinition = (RouteDefinition) o;
return Objects.equals(id, routeDefinition.id)
&& Objects.equals(predicates, routeDefinition.predicates)
&& Objects.equals(order, routeDefinition.order)
&& Objects.equals(uri, routeDefinition.uri);
}
@Override
public int hashCode() {
return Objects.hash(id, predicates, uri);
}
@Override
public String toString() {
return "RouteDefinition{" + "id='" + id + '\'' + ", predicates=" + predicates
+ ", filters=" + filters + ", uri=" + uri + ", order=" + order + '}';
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot使用Flyway進(jìn)行數(shù)據(jù)庫管理的操作方法
Flyway是一個開源的數(shù)據(jù)庫版本管理工具,并且極力主張“約定大于配置”,簡單、專注、強(qiáng)大。接下來通過本文給大家介紹SpringBoot使用Flyway進(jìn)行數(shù)據(jù)庫管理的方法,感興趣的朋友一起看看吧2021-09-09
Java模擬有序鏈表數(shù)據(jù)結(jié)構(gòu)的示例
這篇文章主要介紹了Java模擬有序鏈表數(shù)據(jù)結(jié)構(gòu)的示例,包括一個反序的單鏈表結(jié)構(gòu)的例子,需要的朋友可以參考下2016-04-04
Java調(diào)用wsdl接口的兩種方法(axis和wsimport)
本文主要介紹了Java調(diào)用wsdl接口的兩種方法(axis和wsimport),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
SpringBoot整合ShardingSphere的示例代碼
本文主要介紹了SpringBoot整合ShardingSphere的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
使用Swagger時Controller中api接口顯示不全的問題分析及解決
swagger是一個十分好用的api接口管理、測試框架,現(xiàn)在越來越多的人使用這個做接口的測試和管理,但經(jīng)常遇到Controller中的api接口顯示不全的問題,所以本文給大家詳細(xì)分析了問題以及解決方法,需要的朋友可以參考下2024-02-02
Java算法之BFS,DFS,動態(tài)規(guī)劃和貪心算法的實現(xiàn)
廣度優(yōu)先搜索(BFS)和深度優(yōu)先搜索(DFS)是圖遍歷算法中最常見的兩種算法,主要用于解決搜索和遍歷問題。動態(tài)規(guī)劃和貪心算法則用來解決優(yōu)化問題。本文就來看看這些算法的具體實現(xiàn)吧2023-04-04

