pom文件:
<?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.7.7</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.itheima</groupId> <artifactId>cold-gateway</artifactId> <version>0.0.1-SNAPSHOT</version> <name>cold-gateway</name> <description>网关服务</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>2021.0.5</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-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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>
配置文件:
eureka: client: service-url: defaultZone: http://eureka.itheima.com:8001/eureka spring: application: name: cold-gateway cloud: gateway: discovery: locator: enabled: true # 路由规则 routes: - id: cold-user-router # 路由到哪去 uri: lb://cold-user predicates: # 路由API(前端请求API) - Path=/cold/sys/user/** # 可设置多个 ## - id: cold-user-router ## uri: lb://cold-user ## predicates: ## - Path=/cold/sys/user/** server: port: 8080 tomcat: uri-encoding: utf-8 max-connections: 200 min-spare-threads: 30 connection-timeout: 50000ms error: path: /cold
启动类:
@SpringBootApplication @EnableDiscoveryClient public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
启动后访问http://eureka.itheima.com:8001: