一 新建项目microservice-gateway-zuul-reg-exp
二 修改ZuulApplication
package com.itmuch.cloud.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.filters.discovery.PatternServiceRouteMapper;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
@Bean
public PatternServiceRouteMapper serviceRouteMapper() {
// 调用构造函数PatternServiceRouteMapper(String servicePattern, String routePattern)
// servicePattern指定微服务的正则
// routePattern指定路由正则
return new PatternServiceRouteMapper("(?<name>^.+)-(?<version>v.+$)", "${version}/${name}");
}
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
三 修改microservice-provider-user微服务的配置
server:
port: 8000
spring:
application:
name: microservice-provider-user-v1
jpa:
generate-ddl: false
show-sql: true
hibernate:
ddl-auto: none
datasource: # 指定数据源
platform: h2 # 指定数据源类型
schema: classpath:schema.sql # 指定h2数据库的建表脚本
data: classpath:data.sql # 指定h2数据库的数据脚本
logging: # 配置日志级别,让hibernate打印出执行的SQL
level:
root: INFO
org.hibernate: INFO
org.hibernate.type.descriptor.sql.BasicBinder: TRACE
org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone:http://localhost:8761/eureka/
#defaultZone: http://user:password123@localhost:8761/eureka/
instance:
prefer-ip-address: true
四 测试
1 启动Eureka
2 启动用户微服务
3 启动zuul
![](https://i-blog.csdnimg.cn/blog_migrate/82ba8499577edd5ef4be7a56433415b5.png)
5 说明
通过这段代码即可实现将诸如microservice-provider-user-v1这个微服务,映射到/v1/microservice-provider-user/** 这个路径。