栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

(八)Alian 的 Spring Cloud Gateway 网关中心

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

(八)Alian 的 Spring Cloud Gateway 网关中心

目录
    • 一、简介
    • 二、配置
      • 2.1、pom文件
    • 三、配置文件
      • 3.1、application.properties
    • 四、主类

一、简介

  在微服务架构中,我们的服务往往由多个微服务组成,而这些服务可能部署在不同机房、不同地区、不同域名下。这种情况下,客户端(例如浏览器、手机、软件工具等)想要直接请求这些服务(比如我们请求: http://10.130.3.88:7001/test/getPort ),就需要知道它们具体的地址信息,例如 IP 地址、端口号等这种方式存在以下问题:

  • 客户端需要维护大量的服务地址,这对于客户端来说,是非常繁琐复杂的
  • 当服务地址更改时,客户端维护更麻烦,每个使用到的都要更改
  • 身份认证的难度大,每个微服务需要独立认证
  • 可能会存在跨域请求的问题。

这个时候我们的Spring Cloud Gateway 就很好的解决这些问题了,它的主要功能特征如下:

  • 基于 Spring Framework 5、Project Reactor 和 Spring Boot 2.0 构建
  • 能够在任意请求属性上匹配路由
  • predicates(断言) 和 filters(过滤器)是特定于路由的
  • 集成了 Hystrix 熔断器
  • 集成了 Spring Cloud DiscoveryClient(服务发现客户端)
  • 易于编写断言和过滤器
  • 能够限制请求频率
  • 能够重写请求路径

  其实说这么多,还是很懵,直接说,怎么用吧?来来来,干起来!!!

二、配置 2.1、pom文件

pom.xml



    4.0.0
    
        cn.alian.microservice
        parent
        1.0.0-SNAPSHOT
    

    gateway-service
    0.0.1-SNAPSHOT
    gateway-service
    网关服务

    
   		
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
        
            org.springframework.cloud
            spring-cloud-starter-gateway
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        
        
        
            com.squareup.okhttp3
            okhttp
        
    

  为了使其简单,我这里使用okhttp。其他的依赖相信大家都耳熟能详了,就不过多的解释了。

三、配置文件 3.1、application.properties
#服务名
spring.application.name=gateway-service
#端口
server.port=9999

#gateway开启服务注册和发现的功能
spring.cloud.gateway.discovery.locator.enabled=true
#将请求路径上的服务名配置为小写(因为服务注册的时候,向注册中心注册时将服务名转成大写的了)
spring.cloud.gateway.discovery.locator.lower-case-service-id=true
#与Eureka注册服务中心的通信zone和url地址
eureka.client.serviceUrl.defaultZone=http://10.130.3.222:8761/eureka
#实例是否在eureka服务器上注册自己的信息以供其他服务发现,默认为true
eureka.client.register-with-eureka=false
#该实例,相较于hostname是否优先使用IP
eureka.instance.prefer-ip-address=true

#跨域问题
spring.cloud.gateway.globalcors.cors-configurations.[/**].allowedOrigins=*
spring.cloud.gateway.globalcors.cors-configurations.[/**].allowedHeaders=*
spring.cloud.gateway.globalcors.cors-configurations.[/**].allowedMethods=*

#相同header多个值时的处理方式,三种规则可选(RETAIN_FIRST|RETAIN_UNIQUE|RETAIN_LAST)
#spring.cloud.gateway.default-filters[0]=DedupeResponseHeader=Access-Control-Allow-Origin Access-Control-Allow-Credentials, RETAIN_FIRST
#actuator健康检查
management.endpoints.web.exposure.include=*
management.endpoints.web.cors.allowed-origins=localhost

#部署时配置加上
#logging.config=config/logback.xml

  感觉配置已经说得很明白了就不再解释了,这里稍微说明下,配置中心和网关中心都可以注册到Eureka,但是网关服务没有连接数据库的情况下,就没必要弄成配置中心的客户端了,它主要还是做路由的作用,并且它的地址是需要配置到配置中心,让其他服务获取的。

四、主类

GatewayServiceApplication.java

package com.alian.microservice.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.cloud.netflix.ribbon.okhttp.OkHttpRibbonConfiguration;

@RibbonClients(defaultConfiguration = {OkHttpRibbonConfiguration.class})
@EnableEurekaClient
@SpringBootApplication
public class GatewayServiceApplication {

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

}

  全局配置负载均衡算法: @RibbonClients(defaultConfiguration = {OkHttpRibbonConfiguration.class})

  • RibbonClients设置的必须为@Configuration类
  • 该@Configuration类不能被@ComponentScan引用
  • 不能与@SpringBootApplication类在同一个包下(默认扫描的位置是当前启动类所在的包以及启动类所在包下面的所有可扫描注解),否则将作为共享方法使用,无法针对单个服务配置。

  到这里我们的网关中心也可以使用了,启动完成后提供服务的地址是:

  • 网关中心的地址是: http://10.130.3.222:9999
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1020481.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号