Spring Cloud Gateway如何构建

发布时间:2022-01-12 09:14:32 作者:iii
来源:亿速云 阅读:154

这篇文章主要介绍“Spring Cloud Gateway如何构建”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Spring Cloud Gateway如何构建”文章能帮助大家解决问题。

构建服务端

        使用Spring Boot构建一个简单的Web应用,外界向网关发送请求后,会转发到该应用,pom.xml文件内容如下:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

        编写启动类与控制器,提供一个“hello”服务:

@SpringBootApplication
@RestController
public class ServerApp {

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

    @GetMapping("/hello")
    public String hello() {
        System.out.println("调用 hello 方法");
        return "hello";
    }
}

        ServerApp中的hello方法,会返回hello字符串,启动ServerApp,默认使用8080端口,浏览器中访问http://localhost:8080/hello,可看到浏览器输出结果。

构建网关

        新建一个普通的Maven项目,加入Spring Cloud Gateway的依赖,pom.xml内容如下:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RC1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

        为网关项目加入配置文件application.yml,修改服务器端口为9000,配置文件内容如下:

server:
  port: 9000

        添加启动类,配置一个路由定位器的bean,代码如下:

@SpringBootApplication
public class RouterApp {

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

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        Function<PredicateSpec, Route.Builder> fn = new Function<PredicateSpec, Route.Builder>() {

            public Route.Builder apply(PredicateSpec t) {
                t.path("/hello");
                return t.uri("http://localhost:8080");
            }
        };
        return builder.routes().route(fn).build();
    }
}

        以上代码中,使用Spring容器中的RouteLocatorBuilder bean来创建路由定位器,调用Builder的route方法时,传入java.util.function.Function实例,这是Java8加入的其中一个函数式接口,我们可以使用函数式编程来实现以上的代码,下面的代码等价于前面的代码:

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
        .route(t -> t.path("/hello")
          .and()
          .uri("http://localhost:8080"))
        .build();
    }

        以上的两段代码设定了一个路由规则,当浏览器访问网关的http://localhost:9000/hello地址后,就会路由到http://localhost:8080/hello。

        除了可以路由到我们本例的8080端口外,还可以路由到其他网站,只需要改变一下PredicateSpec的uri即可,例如将.uri("http://localhost:8080")改为.uri(“http://www.163.com”)。

关于“Spring Cloud Gateway如何构建”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

推荐阅读:
  1. Spring Cloud Gateway - 扩展
  2. 基于Spring cloud gateway定制的微服务网关

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

gateway spring cloud

上一篇:java中事务的特性有哪几种

下一篇:MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决方法是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》