创建路由中心:
选择Spring Initializr模板
选择Spring Cloud Routing-->Gateway
pom文件
(这里吐槽一下,记得是spring-cloud-starter-netflix-eureka-client)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>cloud-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloud-gateway</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- 配置中心的客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<!-- 这里是starter!!! 之前引用了没有starter的,死活没到注册中心,花了两天时间,终于查到这个包问题!!坑爹!!!-->
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
添加服务注解:@EnableDiscoveryClient
application.yml
(不配置routes的话,访问
http://localhost:9030/service1/test/hello 成功
http://localhost:9030/s1/test/hello 是无法访问的
配置后,两种方式都能访问)
server:
port: 9030
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
# 不配置routes,则默认使用服务名
routes:
- id: service1
uri: lb://service1
predicates:
- Path=/s1/**
filters:
- StripPrefix=1
- id: service2
uri: lb://service2
predicates:
- Path=/s2/**
filters:
- StripPrefix=1
eureka:
client:
service-url:
defaultZone: ${registry.url}
logging:
level:
org.springframework.cloud.gateway: trace
org.springframework.http.server.reactive: debug
org.springframework.web.reactive: debug
reactor.ipc.netty: debug
bootstrap.yml
spring:
http:
encoding:
charset: UTF-8
enabled: true
force: true
cloud:
config:
uri: http://${host:localhost}:9020
name: config
profile: ${active:dev}
然后,我们按照顺序启动。
1 注册中心,配置中心
2 路由中心, service1,service2
eureka
我们访问服务(两种访问都可以)
由于service1访问的是dev,
service2访问的是prod,
所以他们显示的信息不一样,这说明我们切换配置文件成功了。
顺带一说:gateway负载均衡。
那么只要 微服务名称 设置成一样就好了。
spring:
application:
name: service1
其他不用修改。
访问页面http://localhost:9030/service1/test/test2
结果: