欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

SpringCloudGateway之服務(wù)注冊與發(fā)現(xiàn)

簡介

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

余杭ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

工程介紹

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

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

工程詳情

注冊中心

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

服務(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/
啟動類
@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

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

自定義請求路徑

在上面的例子中,向sc-service-gateway發(fā)送的請求時,url必須帶上服務(wù)名sc-service-hi這個前綴,才能轉(zhuǎn)發(fā)到sc-service-hi上,轉(zhuǎn)發(fā)之前會將sc-service-hi去掉。有時服務(wù)名稱過長,不易使用,需要自定義路徑并轉(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

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

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

hi zhangsan~ ,i am from port:8762

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

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

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

新聞名稱:SpringCloudGateway之服務(wù)注冊與發(fā)現(xiàn)
當(dāng)前地址:http://www.chinadenli.net/article20/piicco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT搜索引擎優(yōu)化網(wǎng)站排名關(guān)鍵詞優(yōu)化靜態(tài)網(wǎng)站自適應(yīng)網(wǎng)站

廣告

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

外貿(mào)網(wǎng)站建設(shè)