Springcloud config + zuul 搭建動態網關

把zuul項目當成configClient端,在zuul項目的pom文件中新增依賴

<!-- SpringBoot整合Web組件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-client</artifactId>
		</dependency>
		<!-- actuator監控中心 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

配置文件

###註冊 中心
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8100/eureka/
server:
  port: 80
###git上配置文件的名稱
spring:
  application:
    name: zuulConfig
  cloud:
    config:
    ####環境名稱(後綴)
      profile: dev
      ####讀取config-server註冊地址
      discovery:
        service-id: config-server
        ###開啓讀取
        enabled: true

#手動刷新
management:
  endpoints:
    web:
      exposure:
        include: "*"

碼雲上的配置文件

在zuul項目的啓動類中添加如下方法

	@RefreshScope
	@ConfigurationProperties("zuul")
	public ZuulProperties zuulProperties() {
		return new ZuulProperties();
	}

啓動Eureka、order項目、member項目、zuul項目。

訪問http://localhost/api-order/toMember?name=22&userToken=111

然後把碼雲裏面的配置文件改一下,把api-order改成order

手動刷新,使用postman訪問http://127.0.0.1/actuator/refresh

然後訪問http://localhost/order/toMember?name=22&userToken=111

實現不重啓網關服務實時刷新配置。

zuul網關的搭建和springcloudConfig配置中心的搭建前面的博客已經說過了,如下

SpringcloudConfig中configClient端不重啓項目,手動刷新同步

springcloud中使用zuul網關實現反向代理和zuul過濾器

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章