跟我学---五、Spring Cloud GateWay

Spring Cloud Gateway介绍
前段时间刚刚发布了Spring Boot 2正式版,Spring Cloud Gateway基于Spring Boot 2,是Spring Cloud的全新项目,该项目提供了一个构建在Spring 生态之上的API网关,包括:Spring 5,Spring Boot 2和Project Reactor。 Spring Cloud Gateway旨在提供一种简单而有效的途径来发送API,并为他们提供横切关注点,例如:安全性,监控/指标和弹性。当前最新的版本是v2.0.0.M8,正式版最近也会到来。

Spring Cloud Gateway的特征:

  • 基于Java 8编码
  • 支持Spring Framework 5
  • 支持Spring Boot 2
  • 支持动态路由
  • 支持内置到Spring Handler映射中的路由匹配
  • 基于HTTP请求的路由匹配 (Path, Method, Header, Host, etc…​)
  • 过滤器作用于匹配的路由
  • 过滤器可以修改下游HTTP请求和HTTP响应 (增加/修改头部, 增加/修改请求参数, 改 写请求路径等​)
  • 通过API或配置驱动
  • 支持Spring Cloud DiscoveryClient配置路由,与服务发现与注册配合使用

1.搭建Eureka 服务注册中心
在idea创建SpringBoot 项目,主要依赖如下:

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</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>

在启动类中添加注释@EnableEurekaServer,代码如下所示:

//会为项目自动配置必须得配置类,标识该服务为注册中心
@EnableEurekaServer
@SpringBootApplication
public class EurakeServerApplication {

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

}

在application.yml 配置文件中添加一下配置,配置注册中心得端口和标识:

server:
  port: 8761
eureka:
  instance:
    hostname: standalone
    instance-id: ${spring.application.name}:${vcap.application.instance-id:${spring.application.instance-id:${random.value}}}
  client:
    register-with-eureka: false #表明该服务不会向Eureka Server注册自己得信息
    fetch-registry: false  #表明该服务不会向Eureka Server获取注册信息
    service-url:    #Eureka Server注册中心得地址,用于Client与Server交流
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: eureka-service

2.搭建Euraka服务提供者
在idea创建SpringBoot 项目,主要依赖如下:

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>

启动类如下:

@SpringBootApplication
public class EurakeClient01Application {

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

}

application.yml 配置如下:

server:
  port: 8762
eureka:
  instance:
    hostname: client
    instance-id: ${spring.application.name}:${vcap.application.instance-id:${spring.application.instance-id:${random.value}}}
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/  #服务注册中心访问地址

spring:
  application:
    name: user-service

新建一个controller包,添加一个提供服务的接口,代码如下:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/*
 * @Author chencundeng
 * @Description //TODO
 * @Date 2019/3/6
 **/
@RestController
public class FeignServiceController {

    @GetMapping("/say-hello/{name}")
    public String sayHello(@PathVariable("name") String name){
        return "hello ".concat(name).concat("!");
    }

}

3.服务网关搭建
在idea创建SpringBoot 项目,主要依赖如下:

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</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>

启动类如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
@RestController
public class GatewayApplication {

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

在 resources 路径下添加配置文件 application.yml

server:
  port: 8080
spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: http://127.0.0.1:8762
        filters:
        - AddResponseHeader=X-Response-Foo, Bar
        predicates:
        - Path=/say-hello/**

id:固定,不同 id 对应不同的功能,可参考 官方文档
uri:目标服务地址
predicates:路由条件
filters:过滤规则

4.测试
访问 http://localhost:8080/say-hello 路由到 服务A http://localhost:8762/say-hello

5.例子源码地址如下:
https://gitee.com/mingzhishuyuan/spring-cloud.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值