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