Eureka Server+APIゲートウェイ+クライアント設定の検出

10412 ワード

  • 既存サービス発見クライアント変換
  • pom.xml
  • eurekaクライアント依存項目の追加
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    依存管理の有効化
    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-dependencies</artifactId>
          <version>${spring-cloud.version}</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
    spring-boot version = 2.5.5
    spring-cloud version = 2020.0.4
  • Application.java
  • @EnableDiscoveryClient
    public class TaxApplication {
    	...
    }
    @EnableDiscoveryClient 어노테이션 추가
  • application.yml
  • server:
        port: 0
                
    eureka:
        client:
            register-with-eureka: true
            fetch-registry: true
            service-url:
                defaultZone: http://localhost:8761/eureka
        instance:
            instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
            
    spring:
        application:
            name: etaxbill-service
    server.port = 0 랜덤포트 설정
    instance-id를 설정하여 client 인스턴스 구분 표시
  • APIゲートウェイ設定
  • server:
        port: 8443
    
    eureka:
        client:
            register-with-eureka: true
            fetch-registry: true
            service-url:
                defaultZone: http://localhost:8761/eureka
    
    spring:
        application:
            name: apigateway-service
        cloud:
            gateway:
                default-filters:
                    -   name: GlobalFilter
                        args:
                            baseMessage: Spring Cloud Gateway Global filter
                            preLogger: true
                            postLogger: true
                routes:
                    -   id: tax-invoice
                        # uri: http://tax.blueport.co.kr:8443/
                        uri: lb://etaxbill-service
                        predicates:
                            - Path=/v1/**
                        filters:
                            # - AddRequestHeader=headerName, headerValue
                            # - AddResponseHeader=headerName2, headerValue2
                            - CustomFilter
    uri주소를 'lb://서비스명'으로 찾음. (spring.application.name)
    서비스명은 자동 uppercase됨.
    API要求->APIゲートウェイ->Eurekaサーバ:サービス発見、登録(位置情報とルーティング、ロードバランシング)->APIゲートウェイ->クライアントの発見(対応するサービスの実行)