Eureka Server / Client 설치하기

By | 2022년 8월 11일
Table of Contents

Eureka Server / Client 설치하기

참조

Eureka

Eureka 는 Netflix 에서 공개한 OSS Service Registry 이다.

별도의 프로그램이 아니라,
Spring Boot 상에서 작동하므로 설치가 간편하다.

Eureka Server 설치

신규 Spring Boot 프로젝트를 생성한다.

의존성을 추가해 준다.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

@EnableEurekaServer 를 추가하는 것만으로 서버구성이 끝난다.

@EnableEurekaServer
@SpringBootApplication
public class EurecaServerTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurecaServerTestApplication.class, args);
    }

}
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false

server:
  port: 8761

일반적인 Spring Boot 앱을 실행하는 것과 동일하게 실행한다.

http://localhost:8761/ 에 접속하면 Eureca 가 정상적으로 실행되는 것을 볼 수 있다.

Spring Security 활성화

의존성을 추가한다.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
}

WebSecurityConfigurerAdapter 가 Deprecated 되었지만,
소스를 간단히 하기위해 그냥 사용한다.

@EnableWebSecurity
class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    // WebSecurityConfigurerAdapter Deprecated

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf()
                .disable()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .httpBasic();
    }
}
spring:
  security:
    user:
      name: admin
      password: admin

Eureka Client 설치

신규 Spring Boot 프로젝트를 생성한다.

의존성을 추가해 준다.
테스트를 위해 feign 도 같이 설치한다.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class EurecaClientTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurecaClientTestApplication.class, args);
    }

}
@Getter
@Setter
@NoArgsConstructor
public class StatusResponse {

    private String status;
}
@RestController
@RequestMapping("/actuator")
public class ActuatorController {

    @GetMapping("/info")
    public StatusResponse info() {
        StatusResponse response = new StatusResponse();
        response.setStatus("OK");

        return response;
    }
}
server:
  port: 8888

spring:
  application:
    name: eureka-client

eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone : http://admin:admin@localhost:8761/eureka/
  instance:
    hostname: localhost

앱을 실행시킨다.

http://localhost:8761/ 에 접속하면 Eureca Client 가 정상적으로 등록된 것을 볼 수 있다.

feign 테스트

@FeignClient(name = "EUREKA-CLIENT")
public interface FeignTestService {

    @GetMapping("/actuator/info")
    StatusResponse feignTest();
}
@RestController
@RequiredArgsConstructor
@RequestMapping("/test")
public class FeignTestController {

    private final FeignTestService service;

    @GetMapping("/feign")
    public String feignTest() {

        StatusResponse response = service.feignTest();
        return response.getStatus();
    }
}

http://localhost:8888/test/feign 에 접속해 보면,
feign 이 서비스명만으로 eureca 에서,
url 과 포트를 받아와 API 를 호출하는 것을 볼수 있다.

하나의 앱에 Eureka Client 와 Feign Client 를 모두 생성한 경우,
5초 정도 지나야 Eureka Server 가 Eureka Client 를 정상적으로 서비스하므로,
5초 지난 이후 호출해야 정상 작동한다.

하지만 nomad 가 등장한다면?

https://www.skyer9.pe.kr/wordpress/?p=3192

Eureka Server 가 여러대 인것에 대응하는지 의문이고,
Eureka Client 가 죽었을 때 자동복구가 되는지 의문이고,
auto-scaling 이 되는지도 의문이고 …

소규모 클러스터에 적당해 보이는데,
그것도 서버 장애시 사람이 손대야 하는 것으로 보인다.

반면에,
nomad + consul 은 100만대의 서버에서도 작동하는것이 확인되었다.
개인적으로도 N개의 consul 서버(dns), M개의 클라이언트,
자동복구, auto-scaling 등을 확인했다.

뭐…
간편한 것은 eureca.
안정적인 것은 nomad 를 생각한다.

답글 남기기