目錄
- zuul
- 作用:
- 簡單示例:
- 0.創建模塊
- 1.匯入依賴:
- 2.主程式增加注解:
- 3.配置application.yml:
- 4.測驗
- 配置語法:
- 路由
- 補充:
??上一篇介紹一個新的組件Hystrix,Hystrix是一個熔斷器,可以用于解決微服務呼叫中發送的服務熔斷和服務降級問題, Spring Cloud認知學習(四):熔斷器Hystrix的使用
??這一篇介紹一個新的組件Zuul,Zuul是網關組件,對Api請求進行了統一的接收,基于網關,我們可以對所有的請求來進行處理,進行日志記錄,請求授權等操作,
zuul
- zuul可以作為微服務的網關,所謂網關,其實就是服務消費者通過網關來訪問服務消費者,就好像網路環境中我們都是把請求丟給網關,網關再向外請求的,
- zuul是Netfilx OSS的一員,可以與其他的Netfilx OSS的兄弟,如Eureka,Ribbon,Hystrix很好的配合,
- 為了應對zuul的更新問題,spring 團隊也自己開發了一個網關組件--Spring Cloud Gateway,但還是像我之前說的,學這個一方面是為了了解多種知識,一方面是為了避免需要接手zuul專案或者把zuul專案改造成gateway的卻缺少zuul知識的問題,而且zuul還沒涼不是嗎
作用:
??使用網關來隱藏了具體的服務的URL,外部呼叫API介面的時候都是使用網關的URL,避免了服務的敏感資訊的泄露,
??由于所有的請求都經過網關,所以可以在網關做身份認證和權限認證,
??可以在網關做統一的日志記錄,
??可以在網關做統一的監控處理,
??可以與eureka、ribbon等結合,可以實作對請求的負載均衡,
簡單示例:
下面的代碼可以參考zuul整合使用實驗
0.創建模塊
0.創建新模塊spring-cloud-zuul-10001
1.匯入依賴:
<dependencies>
<!--引入公共依賴包 start-->
<dependency>
<groupId>com.progor.study</groupId>
<artifactId>spring-cloud-common-data</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--引入公共依賴包 end-->
<!--匯入zuul-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!--匯入eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--匯入actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--匯入hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--匯入web相關-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2.主程式增加注解:
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient // 需要加eureka,因為他也需要拉取服務串列
public class SpringCloudZuul10001Application {
public static void main(String[] args) {
SpringApplication.run(SpringCloudZuul10001Application.class, args);
}
}
3.配置application.yml:
server:
port: 10001
spring:
application:
name: spring-cloud-zuul
# 為了負載均衡之類的和拉取服務之類的,也需要配置eureka
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
instance:
instance-id: zuul-10001.com
prefer-ip-address: true
# 配置zuul
zuul:
routes: # 配置路由
UserService.serviceId: USERSERIVE # 服務名
UserService.path: /myuser/** # 在本地用什么來映射,比如/user/list會映射成本地的/myuser/user/list
4.測驗
??啟動spring-cloud-user-service-8001,spring-cloud-zuul-10001,spring-cloud-eureka-server-7001,
??訪問http://localhost:10001/myuser/user/list,你會發現與呼叫http://localhost:8001/user/list的效果一致,所以上面的配置成功了,我們通過zuul的10001作為一個網關呼叫到了8001的服務,那么以后的消費者都可以呼叫網關的介面來間接呼叫服務介面,
配置語法:
路由
zuul:
routes: # 配置路由
UserService.serviceId: USERSERIVE # serviceId用來服務名,UserService.前綴是用來分組映射的,可以是自定義的前綴,
UserService.path: /myuser/** # 在本地用什么來映射,比如/user/list會映射成本地的/myuser/user/list
也可以是:
zuul:
routes: # 配置路由
UserService:
serviceId: USERSERIVE # 服務名
path: /myuser/** # 在本地用什么來映射,比如/user/list會映射成本地的/myuser/user/list
MessageService:
serviceId: MESSAGESERIVE # 服務名
path: /mymsg/** #
??ignored-services:默認情況下,zuul會對能拉取到的服務都進行映像,在eureka有MessageService,但我們沒有配置的時候,你訪問http://localhost:10001/messageserive/msg/list也可以訪問到MessageService,因為默認會采取http://ip:port/服務名來映射,如果你不需要默認映射,只采用你手動配置的映射,那么需要配置zuul.ignored-services = '*',
??prefix:用來指定一個全域的前綴,比如說上面的是/mymsg/** ,如果你配置了zuul.prefix=/api,那么你以后需要呼叫/api/mymsg/**才能訪問到MESSAGESERIVE的介面,
# 配置zuul
zuul:
prefix: /api
routes: # 配置路由
UserService:
serviceId: USERSERIVE # 服務名
path: /myuser/** # 在本地用什么來映射,比如/user/list會映射成本地的/myuser/user/list
ignored-services: '*'
補充:
- 作為網關,可以在網關層做很多功能,更多內容將在單獨章節zuul中講解,這里只是簡單的講解作為一個網關的最基礎的作用而已,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/194134.html
標籤:Java
