SpringCloudGateway之服務注冊與發(fā)現-創(chuàng)新互聯

簡介

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

創(chuàng)新互聯服務項目包括石門網站建設、石門網站制作、石門網頁制作以及石門網絡營銷策劃等。多年來,我們專注于互聯網行業(yè),利用自身積累的技術優(yōu)勢、行業(yè)經驗、深度合作伙伴關系等,向廣大中小型企業(yè)、政府機構等提供互聯網行業(yè)的解決方案,石門網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到石門省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!

工程介紹

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

工程名端口作用
sc-eureka-server8760注冊中心
sc-service-gateway8761路由網關
sc-service-hi8762服務提供者

工程詳情

注冊中心

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/

配置說明:

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

服務提供者

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

說明服務網關轉發(fā)成功了。

自定義請求路徑

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

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/**開頭的請求都會轉發(fā)到uri為lb://SC-SERVICE-HI的地址上,lb://SC-SERVICE-HI即sc-service-hi服務的負載均衡地址,并用StripPrefix的filter 在轉發(fā)之前將/demo去掉。同時將spring.cloud.gateway.discovery.locator.enabled改為false,如果不改的話,之前的localhost:8761/sc-service-hi/hi?name=zhangsan~這樣的請求地址也能正常訪問,因為這時為每個服務創(chuàng)建了2個router。

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

hi zhangsan~ ,i am from port:8762

服務網關轉發(fā)成功,說明自定義請求路徑生效了。

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

歡迎關注我的公眾號《程序員果果》,關注有驚喜~~
Spring Cloud Gateway 之 服務注冊與發(fā)現

創(chuàng)新互聯www.cdcxhl.cn,專業(yè)提供香港、美國云服務器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網絡助力業(yè)務部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準確進行流量調度,確保服務器高可用性。佳節(jié)活動現已開啟,新人活動云服務器買多久送多久。

新聞名稱:SpringCloudGateway之服務注冊與發(fā)現-創(chuàng)新互聯
轉載源于:http://www.bm7419.com/article20/ceeojo.html

成都網站建設公司_創(chuàng)新互聯,為您提供服務器托管品牌網站建設、企業(yè)網站制作、自適應網站、手機網站建設、域名注冊

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

成都定制網站網頁設計