资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

SpringCloudGateway如何构建

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

我们提供的服务有:网站制作、成都做网站、微信公众号开发、网站优化、网站认证、利州ssl等。为上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的利州网站制作公司

构建服务端

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

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.2.RELEASE
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
    

        编写启动类与控制器,提供一个“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内容如下:

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.0.RELEASE
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.RC1
                pom
                import
            
        
    

    
        
            org.springframework.cloud
            spring-cloud-starter-gateway
        
    

        为网关项目加入配置文件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 fn = new Function() {

            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如何构建”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注创新互联行业资讯频道,小编每天都会为大家更新不同的知识点。


文章标题:SpringCloudGateway如何构建
网页URL:http://cdkjz.cn/article/jhcddh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220