pom依賴:git
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies>
application.yml正則表達式
server: port: 8761 spring: application: name: eureka eureka: client: register-with-eureka: false fetch-registry: false server: waitTimeInMsWhenSyncEmpty: 0 serviceUrl: defaultZone: http://localhost:${server.port}/eureka/
啓動類:spring
@SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
pom依賴:api
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> </dependencies>
application.ymlbash
server: port: 8080 spring: application: name: gateway eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true
啓動類,添加 @EnableZuulProxy 註解app
@SpringBootApplication @EnableZuulProxy public class GateWayApplication { public static void main(String[] args) { SpringApplication.run(GateWayApplication.class, args); } }
準備兩個測試項目:user 和 user-hello :微服務
項目名稱 | spring.application.name | 包含API接口 |
user | user | /hello/sayHello |
user-hello | user-hello | /sayHello |
pom依賴,無特殊依賴,僅加入 eureka 依賴便可:測試
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies>
application.ymlfetch
server: port: 8082 spring: application: name: user-hello eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true
項目接口信息spa
項目名稱 | spring.application.name | 包含API接口 |
user | user | /hello/sayHello |
user-hello | user-hello | /sayHello |
咱們在zuul網關的服務中沒有進行特殊配置,則此時的路由轉發規則爲:項目名稱 + 具體接口,則上面接口的路由爲:
http://127.0.0.1:8080/user/hello/sayHello http://127.0.0.1:8080/user-hello/sayHello
下面 user 項目的訪問路徑爲:
http://127.0.0.1:8080/gate01/hello/sayHello
zuul: routes: user: path: /gate01/** serviceId: user
或者:
zuul: routes: user: /gate01/**
路由匹配規則:「/?」 會匹配一個位置字符,「/*」 能夠匹配多個字符,可是不能匹配子級路徑,「/**」 能夠匹配子級路徑,示例以下:
/user/? ---> /user/a /user/b /user/* ---> /user/aa /user/bbb /user/** ---> /user/aa/bb /user/b/c
這樣的話,咱們的兩個項目,若是路由配置以下:
zuul: routes: user-hello: path: /gate03/hello/** serviceId: user-hello user: path: /gate03/** serviceId: user
假如請求接口爲:http://127.0.0.1:8080/gate03/hello/sayHello,那麼 user 和 user-hello 都能匹配到該路由,這個時候由 yml 配置文件中的順序決定,會訪問到上邊的 user-hello 中。
http://127.0.0.1:8080/api/gate01/hello/sayHello
zuul: prefix: /api routes: user: /gate01/**
ignoredServices : 忽略 user-hello 服務的轉發 , 可是若是配置文件中同時定義了 serviceId 指向該服務 , 那麼該忽略無效
ignored-patterns : 忽略 API 地址包括 "hello" 的路由轉發
zuul: ignoredServices: user-hello ignored-patterns: /**/hello/**
新建 GatewayConfig 類配置文件 , 其中 PatternServiceRouteMapper 的兩個參數分別爲匹配微服務以及匹配路由的正則表達式
PatternServiceRouteMapper(String servicePattern, String routePattern)
以下 , 假如咱們有個微服務名稱爲 user-hello , 那麼 下面的 (?<name>^.+)-hello 剛好能匹配到咱們的微服務 , 因而該微服務的轉發路由爲 /user/test
@Configuration public class GatewayConfig { /** * 路由匹配規則 */ @Bean public PatternServiceRouteMapper serviceRouteMapper() { return new PatternServiceRouteMapper("(?<name>^.+)-hello", "${name}/test"); } }
core-simple : https://code.aliyun.com/995586041/core-simple.git
gateway : https://code.aliyun.com/995586041/gateway.git
gateway-user : https://code.aliyun.com/995586041/gateway-user.git
gateway-user-hello : https://code.aliyun.com/995586041/gateway-user-hello.git