SpringCloudGateway之服務(wù)注冊(cè)與發(fā)現(xiàn)-創(chuàng)新互聯(lián)

簡(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ù),下面就具體講解下。

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

工程介紹

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

工程名端口作用
sc-eureka-server8760注冊(cè)中心
sc-service-gateway8761路由網(wǎng)關(guān)
sc-service-hi8762服務(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)

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

新聞名稱:SpringCloudGateway之服務(wù)注冊(cè)與發(fā)現(xiàn)-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://www.muchs.cn/article20/ceeojo.html

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

廣告

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

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)