您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)利用spring-cloud-gateway怎么實(shí)現(xiàn)一個(gè)動(dòng)態(tài)路由功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
機(jī)發(fā)布
藍(lán)綠部署
滾動(dòng)部署
灰度發(fā)布
停機(jī)發(fā)布 這種發(fā)布一般在夜里或者進(jìn)行大版本升級(jí)的時(shí)候發(fā)布,因?yàn)樾枰C(jī),所以現(xiàn)在大家都在研究 Devops
方案。
藍(lán)綠部署 需要準(zhǔn)備兩個(gè)相同的環(huán)境。一個(gè)環(huán)境新版本,一個(gè)環(huán)境舊版本,通過(guò)負(fù)載均衡進(jìn)行切換與回滾,目的是為了減少服務(wù)停止時(shí)間。
滾動(dòng)部署 就是在升級(jí)過(guò)程中,并不一下子啟動(dòng)所有新版本,是先啟動(dòng)一臺(tái)新版本,再停止一臺(tái)老版本,然后再啟動(dòng)一臺(tái)新版本,再停止一臺(tái)老版本,直到升級(jí)完成。基于 k8s
的升級(jí)方案默認(rèn)就是滾動(dòng)部署。
灰度發(fā)布 也叫金絲雀發(fā)布,灰度發(fā)布中,常常按照用戶設(shè)置路由權(quán)重,例如 90%的用戶維持使用老版本,10%的用戶嘗鮮新版本。不同版本應(yīng)用共存,經(jīng)常與 A/B 測(cè)試一起使用,用于測(cè)試選擇多種方案。
上邊介紹的幾種發(fā)布方案,主要是引出我們接下來(lái)介紹的 spring-cloud-gateway
動(dòng)態(tài)路由,我們可以基于動(dòng)態(tài)路由、負(fù)載均衡和策略加載去實(shí)現(xiàn) 灰度發(fā)布
。當(dāng)然現(xiàn)在有很多開源的框架可以實(shí)現(xiàn) 灰度發(fā)布
,這里只是研究學(xué)習(xí)。
spring-cloud-gateway
默認(rèn)將路由加載在內(nèi)存中。具體可以參見 InMemoryRouteDefinitionRepository
類的實(shí)現(xiàn)。
這里我們基于 Redis
實(shí)現(xiàn)動(dòng)態(tài)路由?;A(chǔ)項(xiàng)目見 spring-cloud-gateway 簡(jiǎn)介
1. 將 actuator 的端點(diǎn)暴露出來(lái)。
management: endpoints: web: exposure: include: "*"
2. redis 配置
@Configuration public class RedisConfig { @Bean(name = {"redisTemplate", "stringRedisTemplate"}) public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) { StringRedisTemplate redisTemplate = new StringRedisTemplate(); redisTemplate.setConnectionFactory(factory); return redisTemplate; } }
3. 將原內(nèi)存路由持久化到 redis
@Component public class RedisRouteDefinitionRepository implements RouteDefinitionRepository { /** * hash存儲(chǔ)的key */ public static final String GATEWAY_ROUTES = "gateway_dynamic_route"; @Resource private StringRedisTemplate redisTemplate; /** * 獲取路由信息 * @return */ @Override public Flux<RouteDefinition> getRouteDefinitions() { List<RouteDefinition> routeDefinitions = new ArrayList<>(); redisTemplate.opsForHash().values(GATEWAY_ROUTES).stream() .forEach(routeDefinition -> routeDefinitions.add(JSON.parseObject(routeDefinition.toString(), RouteDefinition.class))); return Flux.fromIterable(routeDefinitions); } @Override public Mono<Void> save(Mono<RouteDefinition> route) { return route.flatMap(routeDefinition -> { redisTemplate.opsForHash().put(GATEWAY_ROUTES, routeDefinition.getId(), JSONObject.toJSONString(routeDefinition)); return Mono.empty(); }); } @Override public Mono<Void> delete(Mono<String> routeId) { return routeId.flatMap(id -> { if (redisTemplate.opsForHash().hasKey(GATEWAY_ROUTES, id)) { redisTemplate.opsForHash().delete(GATEWAY_ROUTES, id); return Mono.empty(); } return Mono.defer(() -> Mono.error(new NotFoundException("route definition is not found, routeId:" + routeId))); }); } }
4. 重寫動(dòng)態(tài)路由服務(wù)
@Service public class GatewayDynamicRouteService implements ApplicationEventPublisherAware { @Resource private RedisRouteDefinitionRepository redisRouteDefinitionRepository; private ApplicationEventPublisher applicationEventPublisher; /** * 增加路由 * @param routeDefinition * @return */ public int add(RouteDefinition routeDefinition) { redisRouteDefinitionRepository.save(Mono.just(routeDefinition)).subscribe(); applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this)); return 1; } /** * 更新 * @param routeDefinition * @return */ public int update(RouteDefinition routeDefinition) { redisRouteDefinitionRepository.delete(Mono.just(routeDefinition.getId())); redisRouteDefinitionRepository.save(Mono.just(routeDefinition)).subscribe(); applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this)); return 1; } /** * 刪除 * @param id * @return */ public Mono<ResponseEntity<Object>> delete(String id) { return redisRouteDefinitionRepository.delete(Mono.just(id)).then(Mono.defer(() -> Mono.just(ResponseEntity.ok().build()))) .onErrorResume(t -> t instanceof NotFoundException, t -> Mono.just(ResponseEntity.notFound().build())); } @Override public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { this.applicationEventPublisher = applicationEventPublisher; } }
5. 對(duì)外暴露接口
@RestController @RequestMapping("/gateway") public class GatewayDynamicRouteController { @Resource private GatewayDynamicRouteService gatewayDynamicRouteService; @PostMapping("/add") public String create(@RequestBody RouteDefinition entity) { int result = gatewayDynamicRouteService.add(entity); return String.valueOf(result); } @PostMapping("/update") public String update(@RequestBody RouteDefinition entity) { int result = gatewayDynamicRouteService.update(entity); return String.valueOf(result); } @DeleteMapping("/delete/{id}") public Mono<ResponseEntity<Object>> delete(@PathVariable String id) { return gatewayDynamicRouteService.delete(id); } }
測(cè)試
測(cè)試前刪除我們配置的靜態(tài)路由,因?yàn)殪o態(tài)路由和 redis 動(dòng)態(tài)路由同時(shí)存在時(shí)取并集。
訪問(wèn) http://localhost:2000/actuator/gateway/routes , 可以看到只有默認(rèn)路由。
[ { "route_id": "CompositeDiscoveryClient_consul", "route_definition": { "id": "CompositeDiscoveryClient_consul", "predicates": [ { "name": "Path", "args": { "pattern": "/consul/**" } } ], "filters": [ { "name": "RewritePath", "args": { "regexp": "/consul/(?<remaining>.*)", "replacement": "/${remaining}" } } ], "uri": "lb://consul", "order": 0 }, "order": 0 }, { "route_id": "CompositeDiscoveryClient_idc-gateway", "route_definition": { "id": "CompositeDiscoveryClient_idc-gateway", "predicates": [ { "name": "Path", "args": { "pattern": "/idc-gateway/**" } } ], "filters": [ { "name": "RewritePath", "args": { "regexp": "/idc-gateway/(?<remaining>.*)", "replacement": "/${remaining}" } } ], "uri": "lb://idc-gateway", "order": 0 }, "order": 0 }, { "route_id": "CompositeDiscoveryClient_idc-provider1", "route_definition": { "id": "CompositeDiscoveryClient_idc-provider1", "predicates": [ { "name": "Path", "args": { "pattern": "/idc-provider1/**" } } ], "filters": [ { "name": "RewritePath", "args": { "regexp": "/idc-provider1/(?<remaining>.*)", "replacement": "/${remaining}" } } ], "uri": "lb://idc-provider1", "order": 0 }, "order": 0 }, { "route_id": "CompositeDiscoveryClient_idc-provider2", "route_definition": { "id": "CompositeDiscoveryClient_idc-provider2", "predicates": [ { "name": "Path", "args": { "pattern": "/idc-provider2/**" } } ], "filters": [ { "name": "RewritePath", "args": { "regexp": "/idc-provider2/(?<remaining>.*)", "replacement": "/${remaining}" } } ], "uri": "lb://idc-provider2", "order": 0 }, "order": 0 } ]
這個(gè)時(shí)候訪問(wèn) http://192.168.124.5:2000/idc-provider1/provider1/1 根據(jù)結(jié)果可以推測(cè)能正確路由到 provider1
, 測(cè)試結(jié)果一致。
創(chuàng)建 provider1
路由,將路徑設(shè)置為 /p1/**
,測(cè)試是否生效。
POST
請(qǐng)求 http://localhost:2000/gateway/add
{ "id":"provider1", "predicates":[ { "name":"Path", "args":{ "_genkey_0":"/p1/**" } }, { "name":"RemoteAddr", "args":{ "_genkey_0":"192.168.124.5/16" } } ], "filters":[ { "name":"StripPrefix", "args":{ "_genkey_0":"1" } } ], "uri":"lb://idc-provider1", "order":0 }
查看 redis
存儲(chǔ),或者請(qǐng)求 http://localhost:2000/actuator/gateway/routes , 都可以看到配置成功。
訪問(wèn)
curl http://localhost:2000/p1/provider1/1
結(jié)果輸出 2001,與期望一致。
看完上述內(nèi)容,你們對(duì)利用spring-cloud-gateway怎么實(shí)現(xiàn)一個(gè)動(dòng)態(tài)路由功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。