视频链接:2020最新版SpringCloud框架开发教程-周阳
文章源码:https://github.com/geyiwei-suzhou/cloud2020/
cloud-gateway-gateway9527模块
1. 建module
New --> Module --> Maven[Module SDK:1.8.0_191] --> name[cloud-gateway-gateway9527] --> Finish
2. 改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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.antherd.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>loud-gateway-gateway9527</artifactId>
<dependencies>
<!-- gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 引入公共api包 -->
<dependency>
<groupId>com.antherd.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!-- 一般基础配置类 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3. 写yml
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
routes: # 可配置多组路由
- id: payment_route # payment_route # 路由的ID,没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8001 # 匹配后提供服务的路由地址
predicates:
- Path=/payment/** # 断言,路径相匹配的进行路由
eureka:
instance:
hostname: cloud-gateway-service
client: # 服务提供者provider注册进eureka服务列表内
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka
4. 主启动
新建类:com.antherd.springcloud.GatewayMain9527
package com.antherd.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class GatewayMain9527 {
public static void main(String[] args) {
SpringApplication.run(GatewayMain9527.class, args);
}
}
5. 启动测试
启动:cloud-eureka-server7001,cloud-provider-payment8001,cloud-gateway-gateway9527 三个模块
服务网关搭建之前,我们都是直接访问各个模块的接口
如:http://localhost:8001/payment/1
服务网关搭建之后,就可以使用9527服务网关将所有的微服务模块管理起来
通过暴露9527端口提供给外部访问,隐藏其他微服务实现细节
如:http://localhost:9527/payment/1
6. Gateway网关路由的两种配置方式
- 在配置文件yml中配置(见前面配置)
- 代码注入RouteLocator的Bean
Example 26. Application.java
新建类:com.antherd.springcloud.config.GatewayConfig
package com.antherd.springcloud.config;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class GatewayConfig {
@Bean
public RouteLocator routes(RouteLocatorBuilder builder) {
return builder.routes()
.route("path_route_antherd", r -> r.path("/guonei").uri("https://news.baidu.com/guonei")).build();
}
}