學習路線
- 名字解釋
- 架構設計
- 快速啓動-轉發案例:集成Eureka;http轉發
- 基礎功能一覽
一、名詞解釋
- Route:路由是網關的基本構成,定義了ID、URI、predicates、filters;請求URL經過predicates後返回爲true,即校驗通過才能正確路由轉發
- predicates:JDK8出的函數編程,返回boolean,就是判斷輸入條件是否返回true
- Filter:進入網關後,正式業務前後執行,我們的安全校驗、限流等都在這做
二、架構設計
官網架構圖
解釋:
- Gateway Client處理外部請求
- 客戶端信息與路由規則進行匹配,匹配成功進入下一步
- 執行Pre過濾器,安全校驗等
- 執行匹配成功的下游業務邏輯
- 執行Post過濾器,設置一些特殊返回結果
- 響應客戶端
是不是就是:匹配路由規則->執行過濾器->執行下游業務->執行過濾器->返回結果
三、快速啓動-轉發案例:集成Eureka;http轉發
項目源碼:https://gitee.com/carpentor/spring-cloud-example
我們先看一下項目結構
1、下游不依賴於Eureka,直接是web服務Http請求:demo1
application.properties
server.port=8003
spring.application.name=user
server.servlet.context-path=/user-info/
UserController
@RestController
@Log4j2
public class UserController {
@GetMapping("user")
public String get(String userId){
log.info("userController========================={}=userId={}",this.getClass().getSimpleName(),userId);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
return this.getClass().getSimpleName();
}
}
我們來看住程序
首先引入jar
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
修改application.yml文件
server:
port: 8001
spring:
application:
name: gateway-demo
cloud:
gateway:
routes:
- id: user-server
uri: http://localhost:8003
predicates:
## 攔截帶有user-info的url
- Path=/user-info/**
這樣就可以將服務轉發到下游,啓動兩個服務訪問http://localhost:8001/user-server/user
2、集成Eureka:demo2
先啓動Eureka Server(見git項目)將deme2和主程序分別引入jar,支持eureka
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
</dependency>
如何使用Eureka:微服務-Eureka實戰配置
修改application.yml文件
spring:
application:
name: message-server
server:
port: 8002
servlet:
context-path: /message-info/
eureka:
client:
serviceUrl:
defaultZone: http://127.0.0.1:9999/eureka
instance:
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
prefer-ip-address: true
注意spring.application.name=message-server配置
demo2:MessageController
@RestController
@Log4j2
public class MessageController {
@GetMapping("message")
public String get(String username){
log.info("messageController========================={}=",this.getClass().getSimpleName());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
return this.getClass().getSimpleName();
}
}
修改主程序application.yml
server:
port: 8001
spring:
application:
name: gateway-demo
cloud:
gateway:
routes:
- id: user-server
uri: http://localhost:8003
predicates:
## 攔截帶有user-info的url
- Path=/user-info/**
- id: message-server
uri: lb://message-server
order: 1000
predicates:
- Path=/message-info/**
uri: lb://message-server:其中message-server就是demo2中的spring.application.name
predicates:-Path=/message-info/**:這個就是通過Url匹配路由規則
訪問http://localhost:8001/message-info/message即可看到效果
四、過濾器
第三部分就是路由轉發,我們再來一覽一下過濾器怎麼配置
@org.springframework.context.annotation.Configuration
@Log4j2
public class Configuration {
/**
* 過濾器
* order越大優先級越低
* 跟LoginFiter功能一樣,兩種實現方式
* @return
*/
@Bean
@Order(3)
public GlobalFilter login(){
log.info(this.getClass().getSimpleName()+"===login");
return ((exchange, chain) -> {
//pre filter:在業務執行前執行
log.info("pre filter");
return chain.filter(exchange).then(Mono.fromRunnable(()->{
//業務執行後執行
log.info("post filter");
}));
});
}
}
看註釋,這裏先有個大概印象
公衆號主要記錄各種源碼、面試題、微服務技術棧,幫忙關注一波,非常感謝