GateWay入门

本文采用的技术: springcloud 、consul、 gateway

网关的作用

网关功能:

    1.身份认证和权限校验
    2.服务路由、负载均衡
    3.请求限流

网关的技术实现 :

    1.gateway:SpringCloudGateway则是基于Spring5中提供的WebFlux,属于响应式编程的实现,具备更好的性能。
    2.zuul:是基于Servlet的实现,属于阻塞式编程

Spring Cloud Gateway 是 Spring 官方基于 Spring 5.x,Spring Boot 2.0 和 Project Reactor 等技术开发的网关,旨在为微服务架构提供一种简单而有效的统一的 API 路由管理方式,目标是替代 Netflix Zuul,底层是Netty网络编程框架-ServerSocket,其不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,可用于服务安全,统一服务入口管理,负载均衡,限流,鉴权

项目Demo搭建

项目结构
在这里插入图片描述
父工程pom.xml

<dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.7.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <!--编译版本-->
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

服务提供方project-dao

pom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--consul-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <!--       <dependency>
                   <groupId>org.springframework.cloud</groupId>
                   <artifactId>spring-cloud-starter-consul-config</artifactId>
               </dependency>
               这个consul-config我暂时还不清楚什么用,但如果引入这个jar,application.yml文件名就要改名(bootstrap.yml),否则就找不到对应的配置内容
               -->
    </dependencies>

application.xml

server:
  port: 81
spring:
  application:
    name: project-dao
  cloud:
    consul:
      host: 192.168.233.144
      port: 8500
      discovery:
        instance-id: ${spring.application.name}:${server.port} #这个id作为唯一识别的id必填
        service-name: consul-dao
        heartbeat:
          enabled: true  #不打开心跳机制,控制台会显示红叉
        #开启ip地址注册
        prefer-ip-address: true
        #实例的请求ip
        ip-address: ${spring.cloud.client.ip-address}

启动类

@SpringBootApplication
@EnableDiscoveryClient
public class DaoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DaoApplication.class  , args);
    }
}

controller

@RestController
public class UserController {
    @GetMapping("/getDao")
    public String getDao(){
        return "hello dao";
    }
}

这里写好以后,启动项目,是可以直接用localhost:81/getDao 访问的
在这里插入图片描述

Gateway网关搭建

pom.xml

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

application.yml

server:
  port: 82
spring:
  application:
    name: project-gateway
  cloud:
    gateway:
      routes: #配置网关路由规则
        - id: route01  #路由id,自己指定一个唯一值即可
          uri: http://localhost:81 # 网关帮我们转发的url,即访问provider 只能一个实例,不能负载均衡
          predicates: #匹配请求规则 断言(谓词)
            - Path=/gateway/**  #请求路径定义,此路径对应uri中的资源
          filters: #网关过滤器,对谓词中的内容进行判断分析及处理
            - StripPrefix=1 #转发之前去掉path中的第一层路径,例如nacos

# 开发阶段打开gateway日志
logging:
  level:
    org.springframework.cloud.gateway: debug

其中:路由(routes) 是 gateway 中最基本的组件之一,表示一个具体的路由信息载体。主要定义了下面的几个信息:

id,路由标识符,区别于其他 Route
uri,路由指向的目的地 uri,即客户端请求最终被转发到的微服务
predicate,断言(谓词)的作用是进行条件判断,只有断言都返回真,才会执行路由。
filter,过滤器用于修改请求和响应信息

启动类

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

这里启动后,就可以通过http://localhost:82/gateway/getDao 访问到project-dao,通过了gateway网关。但application.xml中配置的uri是写死的路径,我们要改造一下,通过consul调用服务

改造gateway项目,使用consul调用服务

pom.xml添加consul

        <!--consul-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

修改application.xml

server:
  port: 82
spring:
  application:
    name: project-gateway
  cloud:
    consul:
      host: 192.168.233.144
      port: 8500
      discovery:
        register: false
    gateway:
      routes: #配置网关路由规则
        - id: route01  #路由id,自己指定一个唯一值即可
          ## uri: http://localhost:8080 # 网关帮我们转发的url,即访问provider 只能一个实例,不能负载均衡
          uri: lb://consul-dao # gateway将使用 LoadBalancerClient把consul-dao通过consul解析为实际的主机和端口,并进行ribbon负载均衡
          predicates: #匹配请求规则 断言(谓词)
            - Path=/gateway/**  #请求路径定义,此路径对应uri中的资源
          filters: #网关过滤器,对谓词中的内容进行判断分析及处理
            - StripPrefix=1 #转发之前去掉path中的第一层路径,例如nacos

# 开发阶段打开gateway日志
logging:
  level:
    org.springframework.cloud.gateway: debug

启动类添加@EnableDiscoveryClient 注解

@SpringBootApplication
@EnableDiscoveryClient
public class GateWayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GateWayApplication.class  , args);
    }
}

启动,访问http://localhost:82/gateway/getDao,查看日志输出
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值