溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

基于Nacos如何通過Spring Cloud Gateway實(shí)現(xiàn)動(dòng)態(tài)路由?

發(fā)布時(shí)間:2020-06-23 18:23:38 來源:億速云 閱讀:926 作者:清晨 欄目:編程語言

不懂基于Nacos如何通過Spring Cloud Gateway實(shí)現(xiàn)動(dòng)態(tài)路由??其實(shí)想解決這個(gè)問題也不難,下面讓小編帶著大家一起學(xué)習(xí)怎么去解決,希望大家閱讀完這篇文章后大所收獲。

簡介

該文檔主要介紹以Nacos為配置中心,實(shí)現(xiàn)Spring Cloud GateWay 實(shí)現(xiàn)動(dòng)態(tài)路由的功能。Spring Cloud Gateway啟動(dòng)時(shí)候,就將路由配置和規(guī)則加載到內(nèi)存里,無法做到不重啟網(wǎng)關(guān)就可以動(dòng)態(tài)的對應(yīng)路由的配置和規(guī)則進(jìn)行增加,修改和刪除。通過nacos的配置下發(fā)的功能可以實(shí)現(xiàn)在不重啟網(wǎng)關(guān)的情況下,實(shí)現(xiàn)動(dòng)態(tài)路由。

集成

Spring Cloud GateWay集成

spring-cloud-starter-gateway:路由轉(zhuǎn)發(fā)、請求過濾(權(quán)限校驗(yàn)、限流以及監(jiān)控等)

spring-boot-starter-webflux:反應(yīng)式Web框架

spring-boot-starter-actuator:監(jiān)控系統(tǒng)健康

  <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-gateway</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-webflux</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

Nacos集成

nacos-client:nacos客戶端,現(xiàn)在用比較新的版本0.9.0

  <dependency>
			<groupId>com.alibaba.nacos</groupId>
			<artifactId>nacos-client</artifactId>
			<version>0.9.0</version>
		</dependency>

動(dòng)態(tài)路由

DynamicRouteServiceImpl:提供動(dòng)態(tài)路由的基礎(chǔ)方法,可通過獲取bean操作該類的方法。該類提供新增路由、更新路由、刪除路由,然后實(shí)現(xiàn)發(fā)布的功能。

@Service
public class DynamicRouteServiceImpl implements ApplicationEventPublisherAware {

  @Autowired
  private RouteDefinitionWriter routeDefinitionWriter;

  private ApplicationEventPublisher publisher;


  /**
   * 增加路由
   * @param definition
   * @return
   */
  public String add(RouteDefinition definition) {
    routeDefinitionWriter.save(Mono.just(definition)).subscribe();
    this.publisher.publishEvent(new RefreshRoutesEvent(this));
    return "success";
  }


  /**
   * 更新路由
   * @param definition
   * @return
   */
  public String update(RouteDefinition definition) {
    try {
      this.routeDefinitionWriter.delete(Mono.just(definition.getId()));
    } catch (Exception e) {
      return "update fail,not find route routeId: "+definition.getId();
    }
    try {
      routeDefinitionWriter.save(Mono.just(definition)).subscribe();
      this.publisher.publishEvent(new RefreshRoutesEvent(this));
      return "success";
    } catch (Exception e) {
      return "update route fail";
    }


  }
  /**
   * 刪除路由
   * @param id
   * @return
   */
  public String delete(String id) {
    try {
      this.routeDefinitionWriter.delete(Mono.just(id));
      return "delete success";
    } catch (Exception e) {
      e.printStackTrace();
      return "delete fail";
    }

  }

  @Override
  public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
    this.publisher = applicationEventPublisher;
  }

}

RouteDefinition: 提供路由實(shí)體類,主要有predicates匹配來自用戶的請求,filters服務(wù)路由

@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 +
				'}';
	}
}

NacosGatewayProperties:自定義屬性綁定值,可通過配置文件配置屬性。

@ConfigurationProperties(prefix="nacos", ignoreUnknownFields = true)
@Configuration
public class NacosGatewayProperties {

	private String address;
	
	private String dataId;
	
	private String groupId;
	
	private Long timeout;

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getDataId() {
		return dataId;
	}

	public void setDataId(String dataId) {
		this.dataId = dataId;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public Long getTimeout() {
		return timeout;
	}

	public void setTimeout(Long timeout) {
		this.timeout = timeout;
	}	
}

DynamicRouteServiceImplByNacos: 實(shí)現(xiàn)runner,通過nacos下發(fā)動(dòng)態(tài)路由配置

@Component
public class DynamicRouteServiceImplByNacos implements CommandLineRunner{

	@Autowired
  private DynamicRouteServiceImpl dynamicRouteService;

	@Autowired
  private NacosGatewayProperties nacosGatewayProperties;

  /**
   * 監(jiān)聽Nacos Server下發(fā)的動(dòng)態(tài)路由配置
   * @param dataId
   * @param group
   */
  public void dynamicRouteByNacosListener (){
    try {
      ConfigService configService=NacosFactory.createConfigService(nacosGatewayProperties.getAddress());
      String content = configService.getConfig(nacosGatewayProperties.getDataId(), nacosGatewayProperties.getGroupId(), nacosGatewayProperties.getTimeout());
      System.out.println(content);
      configService.addListener(nacosGatewayProperties.getDataId(), nacosGatewayProperties.getGroupId(), new Listener() {
        @Override
        public void receiveConfigInfo(String configInfo) {
        	List<RouteDefinition> list = JsonUtils.toList(configInfo, RouteDefinition.class);
        	list.forEach(definition->{
        		dynamicRouteService.update(definition);
        	});
        }
        @Override
        public Executor getExecutor() {
          return null;
        }
      });
    } catch (NacosException e) {
      e.printStackTrace();
    }
  }

	@Override
	public void run(String... args) throws Exception {
    dynamicRouteByNacosListener();
		
	}
  
}

nacos配置下發(fā)

nacos配置

nacos:
 address: 127.0.0.1:8848
 data-id: dhap-gateway #
 group-id: AAA
 timeout: 5000

nacos屬性文件定義

新建dataID為
groupID為AAA

基于Nacos如何通過Spring Cloud Gateway實(shí)現(xiàn)動(dòng)態(tài)路由?

[
	{
	"filters": [],
	"id": "blog1",
	"order": 0,
	"predicates": [{
		"args": {
			"pattern": "/z"
		},
		"name": "Path"
	}],
	"uri": "https://blog.csdn.net/zhangchangbin123"
},
 {
	"filters": [],
	"id": "blog1",
	"order": 0,
	"predicates": [{
		"args": {
			"pattern": "/c"
		},
		"name": "Path"
	}],
	"uri": "https://blog.csdn.net/zhangchangbin123"
}
]

查看路由信息

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享基于Nacos如何通過Spring Cloud Gateway實(shí)現(xiàn)動(dòng)態(tài)路由?內(nèi)容對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細(xì)的解決方法等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI