前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >gateway 网关_小米多功能网关联不上

gateway 网关_小米多功能网关联不上

作者头像
全栈程序员站长
发布2022-09-27 11:15:30
2190
发布2022-09-27 11:15:30
举报
文章被收录于专栏:全栈程序员必看

zuul1.x系列作为网关,基于servlet实现,属于多线程同步阻塞模型;zuul2.x改写netty,属于异步非阻塞模型;gateway属于异步非阻塞模型。

这里基于spring-session+redis+zuul session共享示例,将其中的zuul网关替换为gateway网关技术。

工程改造

pom依赖

引入gateway依赖包

代码语言:javascript
复制
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
	        <groupId>org.springframework.boot</groupId>
	        <artifactId>spring-boot-starter-actuator</artifactId>
	    </dependency>

增加eureka注册中心依赖信息,这里不能使用spring-cloud-starter-netflix-eureka-server,与gateway的jar包有冲突,可以参考Consider defining a bean of type ‘org.springframework.http.codec.ServerCodecConfigurer‘ in your conf

代码语言:javascript
复制
	    <dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

代码启动类

注解该类为一个springboot工程,并且通过@EnableDiscoveryClient将其注册到注册中心

代码语言:javascript
复制
@EnableDiscoveryClient
@SpringBootApplication
public class Gateway {

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

配置文件

代码语言:javascript
复制
server:
  port: 5000  #gateway网关服务端口号
spring:
  application:
    name: gateway  #gateway网关实例名称
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true       #将请求路由至DiscoveryClient发现的注册中心的服务
          lowerCaseServiceId: true #请求url是否大小写敏感,默认false敏感;true表示不敏感
      routes:  #路由信息配置
      - id: um_route #一个id为一组路由信息
        uri: lb://um  #将请求路由到um服务
        predicates:   #请求格式
        - Path=/um/**
      - id: bm_route
        uri: lb://bm
        predicates:
        - Path=/bm/**
management:
  endpoints:
    web:
      exposure:
        include: '*' #暴露所有端点信息给actuator
eureka:             # eureka服务注册中心信息
  instance:
    prefer-ip-address: true 
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

上面配置文件中uri部分内容未 lb://bm ,lb 表示load balance(负载均衡),将请求分发给所有以bm命名的工程。

项目测试

依次启动 servicecenter、gateway、UserManagerA、UserManagerB 和 BuyManager工程,启动完毕后,打开注册中心 http://localhost:8761/ 可以看到各实例注册成功

gateway 网关_小米多功能网关联不上
gateway 网关_小米多功能网关联不上

功能测试易正常。

gateway网关测试还有另一种方式,在gateway的actuator浏览器信息查看 http://localhost:5000/actuator/gateway/routes

gateway 网关_小米多功能网关联不上
gateway 网关_小米多功能网关联不上

这里,说明gateway负责分发两个路由配置信息。

至此,一个简单的gateway小demo完成。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/183830.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 工程改造
    • pom依赖
      • 代码启动类
        • 配置文件
        • 项目测试
        相关产品与服务
        微服务引擎 TSE
        微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档