前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Cloud Gateway 之 服务注册与发现

Spring Cloud Gateway 之 服务注册与发现

原创
作者头像
程序员果果
修改2019-05-21 18:18:49
1.2K0
修改2019-05-21 18:18:49
举报
文章被收录于专栏:程序员果果程序员果果

文章首发于公众号《程序员果果》

地址:https://mp.weixin.qq.com/s/T1mxjy_IwbnMhubio_8VLQ

简介

上几篇主要讲解了网关在单个服务的使用,在实际的工作中,服务的相互调用都是依赖于服务中心提供的入口来使用,服务中心往往注册了很多服务,如果每个服务都需要单独配置的话,非常麻烦。Spring Cloud Gateway 提供了一种默认转发的能力,只要将 Spring Cloud Gateway 注册到服务中心,Spring Cloud Gateway 默认就会代理服务中心的所有服务,下面就具体讲解下。

工程介绍

本节案例中一共有四个工程,如下:

工程名

端口

作用

sc-eureka-server

8760

注册中心

sc-service-gateway

8761

路由网关

sc-service-hi

8762

服务提供者

工程详情

注册中心

pom.xml
代码语言:txt
复制
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.yml
代码语言:txt
复制
server:
  port: 8760

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: sc-eurka-server

路由网关

pom.xml
代码语言:txt
复制
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
application.yml
代码语言:txt
复制
server:
  port: 8761

spring:
  application:
    name: sc-service-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lowerCaseServiceId: true

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8760/eureka/

配置说明:

  • spring.cloud.gateway.discovery.locator.enabled:是否与服务注册于发现组件进行结合,通过 serviceId 转发到具体的服务实例。默认为 false,设为 true 便开启通过服务中心的自动根据 serviceId 创建路由的功能。
  • pring.cloud.gateway.discovery.locator.lowerCaseServiceId:是将请求路径上的服务名配置为小写(因为服务注册的时候,向注册中心注册时将服务名转成大写的了)。
  • eureka.client.service-url.defaultZone:指定注册中心的地址,以便使用服务发现功能。
  • logging.level.org.springframework.cloud.gateway:调整相 gateway 包的 log 级别,以便排查问题。

服务提供者

pom.xml
代码语言:txt
复制
<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>
application.yml
代码语言:txt
复制
server:
  port: 8762

spring:
  application:
    name: sc-service-hi

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8760/eureka/
启动类
代码语言:txt
复制
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ScServiceHiApplication {

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

    @Value("${server.port}")
    String port;

    @GetMapping("/hi")
    public String home(@RequestParam(value = "name", defaultValue = "zhangsan") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }


}

启动三个项目后,访问 http://localhost:8761/sc-service-hi/hi?name=zhangsan~,返回如下:

代码语言:txt
复制
hi zhangsan~ ,i am from port:8762

说明服务网关转发成功了。

自定义请求路径

在上面的例子中,向sc-service-gateway发送的请求时,url必须带上服务名sc-service-hi这个前缀,才能转发到sc-service-hi上,转发之前会将sc-service-hi去掉。有时服务名称过长,不易使用,需要自定义路径并转发到具体的服务上。配置如下:

代码语言:txt
复制
server:
  port: 8761

spring:
  application:
    name: sc-service-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: false
          lowerCaseServiceId: true
      routes:
        - id: sc-service-hi
          uri: lb://SC-SERVICE-HI
          predicates:
          - Path=/demo/**
          filters:
          - StripPrefix=1

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8760/eureka/

logging:
  level:
    org.springframework.cloud.gateway: debug

在上面的配置中,配置了一个Path 的 predict,将以/demo/**开头的请求都会转发到uri为lb://SC-SERVICE-HI的地址上,lb://SC-SERVICE-HI即sc-service-hi服务的负载均衡地址,并用StripPrefix的filter 在转发之前将/demo去掉。同时将spring.cloud.gateway.discovery.locator.enabled改为false,如果不改的话,之前的localhost:8761/sc-service-hi/hi?name=zhangsan~这样的请求地址也能正常访问,因为这时为每个服务创建了2个router。

重启sc-service-gateway项目后,访问 http://localhost:8761/demo/hi?name=zhangsan~ ,返回如下:

代码语言:txt
复制
hi zhangsan~ ,i am from port:8762

服务网关转发成功,说明自定义请求路径生效了。

源码

https://github.com/gf-huanchupk/SpringCloudLearning/tree/master/chapter13

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

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

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