溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring Cloud Gateway 之 服務(wù)注冊(cè)與發(fā)現(xiàn)

發(fā)布時(shí)間:2020-06-15 00:12:09 來源:網(wǎng)絡(luò) 閱讀:425 作者:程序員果果 欄目:編程語言

簡(jiǎn)介

上幾篇主要講解了網(wǎng)關(guān)在單個(gè)服務(wù)的使用,在實(shí)際的工作中,服務(wù)的相互調(diào)用都是依賴于服務(wù)中心提供的入口來使用,服務(wù)中心往往注冊(cè)了很多服務(wù),如果每個(gè)服務(wù)都需要單獨(dú)配置的話,非常麻煩。Spring Cloud Gateway 提供了一種默認(rèn)轉(zhuǎn)發(fā)的能力,只要將 Spring Cloud Gateway 注冊(cè)到服務(wù)中心,Spring Cloud Gateway 默認(rèn)就會(huì)代理服務(wù)中心的所有服務(wù),下面就具體講解下。

工程介紹

本節(jié)案例中一共有四個(gè)工程,如下:

工程名 端口 作用
sc-eureka-server 8760 注冊(cè)中心
sc-service-gateway 8761 路由網(wǎng)關(guān)
sc-service-hi 8762 服務(wù)提供者

工程詳情

注冊(cè)中心

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

路由網(wǎng)關(guān)

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/

配置說明:

  • spring.cloud.gateway.discovery.locator.enabled:是否與服務(wù)注冊(cè)于發(fā)現(xiàn)組件進(jìn)行結(jié)合,通過 serviceId 轉(zhuǎn)發(fā)到具體的服務(wù)實(shí)例。默認(rèn)為 false,設(shè)為 true 便開啟通過服務(wù)中心的自動(dòng)根據(jù) serviceId 創(chuàng)建路由的功能。
  • pring.cloud.gateway.discovery.locator.lowerCaseServiceId:是將請(qǐng)求路徑上的服務(wù)名配置為小寫(因?yàn)榉?wù)注冊(cè)的時(shí)候,向注冊(cè)中心注冊(cè)時(shí)將服務(wù)名轉(zhuǎn)成大寫的了)。
  • eureka.client.service-url.defaultZone:指定注冊(cè)中心的地址,以便使用服務(wù)發(fā)現(xiàn)功能。
  • logging.level.org.springframework.cloud.gateway:調(diào)整相 gateway 包的 log 級(jí)別,以便排查問題。

服務(wù)提供者

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/
啟動(dòng)類
@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;
    }

}

啟動(dòng)三個(gè)項(xiàng)目后,訪問 http://localhost:8761/sc-service-hi/hi?name=zhangsan~,返回如下:

hi zhangsan~ ,i am from port:8762

說明服務(wù)網(wǎng)關(guān)轉(zhuǎn)發(fā)成功了。

自定義請(qǐng)求路徑

在上面的例子中,向sc-service-gateway發(fā)送的請(qǐng)求時(shí),url必須帶上服務(wù)名sc-service-hi這個(gè)前綴,才能轉(zhuǎn)發(fā)到sc-service-hi上,轉(zhuǎn)發(fā)之前會(huì)將sc-service-hi去掉。有時(shí)服務(wù)名稱過長(zhǎng),不易使用,需要自定義路徑并轉(zhuǎn)發(fā)到具體的服務(wù)上。配置如下:

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

在上面的配置中,配置了一個(gè)Path 的 predict,將以/demo/**開頭的請(qǐng)求都會(huì)轉(zhuǎn)發(fā)到uri為lb://SC-SERVICE-HI的地址上,lb://SC-SERVICE-HI即sc-service-hi服務(wù)的負(fù)載均衡地址,并用StripPrefix的filter 在轉(zhuǎn)發(fā)之前將/demo去掉。同時(shí)將spring.cloud.gateway.discovery.locator.enabled改為false,如果不改的話,之前的localhost:8761/sc-service-hi/hi?name=zhangsan~這樣的請(qǐng)求地址也能正常訪問,因?yàn)檫@時(shí)為每個(gè)服務(wù)創(chuàng)建了2個(gè)router。

重啟sc-service-gateway項(xiàng)目后,訪問 http://localhost:8761/demo/hi?name=zhangsan~ ,返回如下:

hi zhangsan~ ,i am from port:8762

服務(wù)網(wǎng)關(guān)轉(zhuǎn)發(fā)成功,說明自定義請(qǐng)求路徑生效了。

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

歡迎關(guān)注我的公眾號(hào)《程序員果果》,關(guān)注有驚喜~~
Spring Cloud Gateway 之 服務(wù)注冊(cè)與發(fā)現(xiàn)

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI