Spring Cloud Eureka를 연결할 때 아이피가 2개인 경우는 어느 아이피로 연결할까?
이를 위해 IP가 2개인 PC에서 테스트 해보았다.
로컬 IP
무선 (10.10.xxx.xxx)
유선 (172.16.xxx.xxx)
eureka client에서 연결할 때 위의 2개 중 어느 아이피로 연결을 할까? 이를 구성하기 위해서 Eureka Server, Eureka Client 2개를 만들어서 테스트 해보았다.
Eureka Server 구성
pom.xml
<dependencies>
<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-server</artifactId>
</dependency>
</dependencies>
application.yml
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
client:
register-with-eureka: false
fetch-registry: false
Application
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApp {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApp.class, args);
}
}
Eureka Client 구성
pom.xml
<dependencies>
<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>
</dependencies>
application.yml
server:
port: 8080
spring:
application:
name: clientApp
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://127.0.0.1:8761/eureka
Application
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApp {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApp.class, args);
}
}
실행
eureka-server와 eureka-client를 실행하고 서비스 대시보드 화면(http://localhost:8761)으로 접속해보면 아래와 같이 표시된다.
status의 아이피에 링크를 걸면 해당 client의 actuator 링크로 연결되는데 무선 아이피(10.10.xxx.xxx)로 된다.
맥에서 네트워크 카드 우선순위를 변경으로 해도 항상 우선 무선으로 연결이 되는 듯 하다.
그러면 무선이 아닌 유선으로 우선 연결할 수 있는 방법이 없을까?
preferred-networks, ignored-interfaces을 사용하면 된다.
preferred-networks 설정
spring:
cloud:
inetutils:
preferred-networks: 172.16
위와 같이 172.16으로 preferred-networks을 설정한 다음 다시 접속해보니 서비스 대시보드에 표시이름은 10.10으로 표시가 되지만 실제 연결은 172.16으로 연결이 된다.
표시되는 이름을 변경하려면 VM 매개변수로 값을 넣으면 정상적으로 표시가 된다.
-Dspring.cloud.inetutils.preferred-networks: 172.16
혹은 command line property로 주입해도 된다.
--spring.cloud.inetutils.preferred-networks=172.16
ignored-interfaces 설정
그럼 ignored-interfaces으로 설정을 해보자
- 172.16.xxx.xxx : en0 (유선)
- 10.10.xxx.xxx:en1 (무선)
유선(172.16.xxx.xxx)으로 연결하기 위해 무선 네트워크 카드인 en1
을 무시하는 설정으로 추가해보자.
spring:
cloud:
inetutils:
ignored-interfaces: en1
동일하게 유선(172.16.xxx.xxx)으로 접속이 잘된다.
그러면 두 개 다 설정되어 있으면 어떻게 될까?
spring:
cloud:
inetutils: # en0: 172.16, en1: 10.10
preferred-networks: 172.16 # 172.16으로 접속하도록
ignored-interfaces: en0 # 172.16은 무시하도록
이런 경우는 두 개의 인터페이스 모두 접속 안되고 localhost로 접속이 된다. 유선이 연결이 안되면 무선으로 연결될 것 같았는데 localhost로 연결되는 것이 좀 이상하다.
그래서 preferred-networks에 잘못된 ip를 넣어봤다. 예: 123.123
동일하게 localhost로 연결이 된다. preferred-networks에 연결할 수 없는 ip로 설정이 되어 있으면 localhost로 접속이 되는 듯 하다.