有些情况下,我们希望不直接访问后端地址,这个时候可以通过springgateway网关进行处理。下面只是一个简单的例子。

 

至于URL,变化,我们可以通过编写程序逻辑来实现。

 

实现步骤:

 

1.新建项目

 pom.xml 文件如下 。

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>gatewaydemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <spring-boot-dependencies.version>2.3.12.RELEASE</spring-boot-dependencies.version>
        <spring-cloud-dependencies.version>Hoxton.SR12</spring-cloud-dependencies.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>



    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot-dependencies.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud-dependencies.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

 

2.新建主程序

package com.redxun;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class JpaasGatewayServer {

    public static void main(String[] args) {
        SpringApplication.run(JpaasGatewayServer.class, args);
    }
}

 

3.增加路由配置

这个我们可以通过java程序或者路由配置来实现。

 

package com.redxun.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 customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
         // 路由构造器
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
         // 设置路径
        routes.route("baidu_news_guonei_routh", r -> {
             return r.path("/guonei").uri("https://news.baidu.com/guonei");
         });
         routes.route("baidu_news_guoji_routh", r -> {
             return r.path("/guoji").uri("http://news.baidu.com/guoji");
         });

        return routes.build();
     }
 }

这个代码配置了 当 通过网关 访问 /guonei 地址时,请求会转发到 https://news.baidu.com/guonei

 

4.增加属性配置

application.yml

server:
  port: 9908

spring:
  application:
    name: jpaas-gateway-server



management:
  endpoints:
    web:
      exposure:
        include: '*'
  metrics:
    tags:
      application: ${spring.application.name}

 

5.效果如下

 

posted on 2021-10-17 17:14  自由港  阅读(993)  评论(0编辑  收藏  举报