Spring Cloud Gateway 之 服务注册与发现

发布时间:2020-06-15 00:12:09 作者:程序员果果
来源:网络 阅读:414

简介

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

工程介绍

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

工程名 端口 作用
sc-eureka-server 8760 注册中心
sc-service-gateway 8761 路由网关
sc-service-hi 8762 服务提供者

工程详情

注册中心

pom.xml
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.yml
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
<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
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/

配置说明:

服务提供者

pom.xml
<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
server:
  port: 8762

spring:
  application:
    name: sc-service-hi

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8760/eureka/
启动类
@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~,返回如下:

hi zhangsan~ ,i am from port:8762

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

自定义请求路径

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

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~ ,返回如下:

hi zhangsan~ ,i am from port:8762

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

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

欢迎关注我的公众号《程序员果果》,关注有惊喜~~
Spring Cloud Gateway 之 服务注册与发现

推荐阅读:
  1. Spring Cloud 入门教程 - Eureka服务注册与发现
  2. 服务网关 Spring Cloud GateWay 服务化和过滤器

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring cloud gateway te

上一篇:openwrt编译ifb.ko模块问题

下一篇:oracle报错: ORA-03113: end-of-file on communication channel Process

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》