SpringCloud-Discovery

이재철·2021년 7월 25일
0

MSA

목록 보기
1/13

1. Eureka 설정

  1. yml
server:
  port: 8761

spring:
  application:
    name: discoveryservice

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
  1. 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. User-service 설정

  1. 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
  1. 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);
  }

}
  1. user-service 여러개 실행하기
  • -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 (target 삭제)
mvn compile package (컴파일 후 패키지 생성)
빌드 성공 후 jar 파일이 만들어짐
jar 실행 : java -jar -Dserver.port=9004 ./target/user-service-0.0.1-SNAPSHOT.jar

0개의 댓글