如果新建gateway网关后,不增加路由配置,默认会有如下的一个转发规则:
- routes:
- - id: common
- uri: lb://{serviceName}
- predicates:
- - Path=/{serviceName}/**
- filters:
- - StripPrefix=1
访问时通过网关(http://127.0.0.1:8080/)来访问微服务应用(SERVICE-PROVIDER)地址如下:
http://127.0.0.1:8080/SERVICE-PROVIDER/provider/1,访问是对应的如下接口
要实现上述步骤很简单,只需要gateway和被访问的微服务路由同时接入eureka即可;
当然如果gateway对默认路由做了调整的话,需要根据调整配置对应微服务应用的路由。
最后贴一下对应的pom文件
gateway的如下:
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-gatewayartifactId>
- <version>3.1.3version>
- dependency>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
- <version>3.1.3version>
- dependency>
- <dependency>
- <groupId>com.google.code.gsongroupId>
- <artifactId>gsonartifactId>
- <version>2.9.1version>
- dependency>
SERVICE-PROVIDER的如下(根据需要配置):
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-zipkinartifactId>
- <version>2.2.8.RELEASEversion>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-actuatorartifactId>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>de.codecentricgroupId>
- <artifactId>spring-boot-admin-starter-clientartifactId>
- <version>2.5.4version>
- dependency>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-configartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-openfeign-coreartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-openfeignartifactId>
- dependency>