1 准备项目
1-1 eureka注册中心
pom依赖:
<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/
启动类:
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
1-2 zuul网关
pom依赖:
<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.yml
server:
port: 8080
spring:
application:
name: gateway
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
启动类,添加 @EnableZuulProxy 注解
@SpringBootApplication
@EnableZuulProxy
public class GateWayApplication {
public static void main(String[] args) {
SpringApplication.run(GateWayApplication.class, args);
}
}
1-3 测试项目
准备两个测试项目: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.yml
server:
port: 8082
spring:
application:
name: user-hello
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
2 测试路由
项目接口信息
项目名称 | spring.application.name | 包含API接口 |
user | user | /hello/sayHello |
user-hello | user-hello | /sayHello |
2-1 默认路由
我们在zuul网关的服务中没有进行特殊配置,则此时的路由转发规则为:项目名称 + 具体接口,则上面接口的路由为:
http://127.0.0.1:8080/user/hello/sayHello
http://127.0.0.1:8080/user-hello/sayHello
2-2 指定路由
下面 user 项目的访问路径为:
http://127.0.0.1:8080/gate01/hello/sayHello
zuul:
routes:
user:
path: /gate01/**
serviceId: user
或者:
zuul:
routes:
user: /gate01/**
2-3 路由优先级
路由匹配规则:“/?” 会匹配一个位置字符,“/*” 可以匹配多个字符,但是不能匹配子级路径,“/**” 可以匹配子级路径,示例如下:
/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 中。
2-4 统一前缀
http://127.0.0.1:8080/api/gate01/hello/sayHello
zuul:
prefix: /api
routes:
user: /gate01/**
2-5 忽略特定路由
ignoredServices : 忽略 user-hello 服务的转发 , 但是如果配置文件中同时定义了 serviceId 指向该服务 , 那么该忽略无效
ignored-patterns : 忽略 API 地址包括 "hello" 的路由转发
zuul:
ignoredServices: user-hello
ignored-patterns: /**/hello/**
2-6 代码配置路由
新建 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");
}
}
3 代码
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