Create a route center:
Select a template Spring Initializr
Select Spring Cloud Routing -> Gateway
pom file
(Tucao look here, remember 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 > < the groupId > org.springframework.cloud </ the groupId > <-! This is the Starter! ! ! Before cites no starter, and not to the life and death registries, spent two days, finally found the problem this package! ! Pit father! ! ! -> < the artifactId > Spring-Cloud-Starter-Netflix-Eureka-Client </ the artifactId > </ dependency > </ Dependencies > < the 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>
Add Service Notes: @EnableDiscoveryClient
application.yml
(If not configured routes, visit
http://localhost:9030/service1/test/hello 成功
http: // localhost: 9030 / s1 / test / hello is not accessible
Once configured, it can be accessed in two ways)
Server:
Port: 9030
the Spring:
the Application:
name: Cloud-Gateway
Cloud:
Gateway:
Discovery:
Locator:
Enabled: to true
Lower-Case-Service-the above mentioned id: to true
# do not configure routes, the default service name
routes:
- the above mentioned id: service1
URI: LB: // Service1
predicates:
- the Path = / S1 / **
Filters:
- = StripPrefix. 1
- ID: Service2
URI: LB: // Service2
predicates:
- the Path = / S2 / **
Filters:
- =. 1 StripPrefix
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}
Then, we start in order.
1 registration center, distribution center
2 routing center, service1, service2
eureka
Our access services (two types of access can be)
Because service1 visit is dev,
service2 visit was to prod,
So the information they show is not the same, suggesting we switch profile success.