GateWay微服务网关的搭建

服务网关

没有服务网关

问题:地址太多|安全性|管理问题

访问商品服务

http://ip地址:9001/goods/findAll

访问广告服务

http://ip地址:9002/brand/findAll

访问用户服务

http://ip地址:9003/user/findAll

在有网关的情况下,我们配置网关端口号为4006,在访问服务是我们可以通过访问网关进而访问服务,此时端口号访问时都为4006,方便了很多,尤其是前端做跨域配置

访问商品服务

http://ip地址:4006/goods/findAll

访问广告服务

http://ip地址:4006/brand/findAll

访问用户服务

http://ip地址:4006/user/findAll

Spring Cloud Gateway

Spring Cloud Gateway

Spring Cloud Gateway 是 Spring Cloud生态系统中的网关,它是基于Spring 5.0、SpringBoot 2.0和Project Reactor等技术开发的,旨在为微服务架构提供一种简单有效的、统一的API路由管理方式,并为微服务架构提供安全、监控、指标和弹性等功能。其目标是替代Zuul

路由(Route)

是网关的基本构建块。由一个ID一个目标URI一组断言和一组过滤器定义。断言为真,则路由匹配

断言(predicate)

输入类型是一个ServerWebExchange。我们可以使用它来匹配来自HTTP请求的任何内容

过滤(filter)

可以在请求被路由前或者之后对请求进行修改。

搭建网关

在父项目中,创建网关模块,创建的是maven项目

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">
    <parent>
        <artifactId>cloud-my</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-gateway</artifactId>

    <dependencies>
        <!-- Gateway-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!-- Eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>
    </dependencies>
</project>

主启动类

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

bootstartp.yml

server:
  port: 9527
eureka:
  client:
    service-url:
      # Eureka server 地址
      defaultZone: http://localhost:7001/eureka/
spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: false    #不开启服务注册和发现的功能
          lower-case-service-id: true #请求路径上的服务名称转换为小写
      #路由配置
      #id:路由id,没有固定规则但要求唯一,,不重复即可
      #uri:提供服务的路由地址,如http://localhost:80|协议lb,后面名称为应用名,表示启用Gateway的负载均衡的功能,如lb://cloud-eureka-pro
      #predicates:断言
      routes:
        #cloud-eureka-pro
        - id: cloud-eureka-pro80
          uri: http://localhost:80
          predicates:
            - Path=/eureka80/**
      globalcors:
        # 解决options请求被拦截问题
        add-to-simple-url-handler-mapping: true
        cors-configurations:
          # 拦截的请求
          '[/**]':
            # 是否允许携带cookie
            allowCredentials: true
            #允许哪些网站的跨域请求 "*"允许所有网站
            #allowedOrigins: "*" # spring boot2.4以前的配置
            allowedOriginPatterns: "*" # spring boot2.4以后的配置
            #允许跨域的ajax的请求方式
            allowedMethods: "*"
            # 允许请求中携带的头信息
            allowedHeaders: "*"
            # 跨域检测的有效期,单位s
            maxAge: 36000

logging:
  pattern:
    console: '%d{MM/dd HH:mm:ss.SSS} %clr(%-5level) ---  [%-15thread] %cyan(%-50logger{50}):%msg%n'

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月木@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值