SpringCloud-Discovery


1.Eurekaの設定

  • yml
  • server:
      port: 8761
    
    spring:
      application:
        name: discoveryservice
    
    eureka:
      client:
        register-with-eureka: false
        fetch-registry: false
    
  • application.Javaに@EnableEurekaServer
  • を追加
    package com.example.discoveryservice;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer
    public class DiscoveryserviceApplication {
    
      public static void main(String[] args) {
        SpringApplication.run(DiscoveryserviceApplication.class, args);
      }
    
    }
    

    2.ユーザー-サービス設定

  • yml構成
  • server:
      port: 8081
    spring:
      application:
        name: user-service
    
    eureka:
      client:
        register-with-eureka: true # 유레카에 등록할 할거다
        fetch-registry: true # 외부에서 검색 가능하게 할거다
        service-url: # 유레카 서버 위치가 어디인지 지정
          defaultZone: http://127.0.0.1:8761/eureka
  • application.Javaに@EnableDiscoveryClient
  • を追加
    package com.example.userservice;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    public class UserServiceApplication {
    
      public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
      }
    
    }
    
  • user-複数のサービスを実行する

  • -Dserver.port=8082


  • mvn spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=9003'

  • cmd
  • ランダムポートランタイム
  • server:
      port: 0 # 랜덤 포트로 하겠다.
    
    spring:
      application:
        name: user-service
    
    eureka:
      instance:
        instance-id: ${spring.cloud.client.hostname}:${spring.cloud.instance_id:${random.value}}
      client:
        register-with-eureka: true # 유레카에 등록할 할거다
        fetch-registry: true # 외부에서 검색 가능하게 할거다
        service-url: # 유레카 서버 위치가 어디인지 지정
          defaultZone: http://127.0.0.1:8761/eureka
    
    mvn clean(ターゲットを削除)
    mvn compile package(コンパイル後にパッケージを生成)
    構築に成功したらjarファイルを作成
    JAr実行:java-jar-DServer.port=9004 ./target/user-service-0.0.1-SNAPSHOT.jar