서버

[Eureka] Eureka client 만들기

고태광이 2019. 3. 24. 17:14

[Eureka] Eureka client 만들기 


https://start.spring.io/ 여기서 dependency  eureka-discovery, web 추가해서 생성할 경우 1번 건너뛰세요 


1. pom.xml 파일에 dependency 추가


1
2
3
4
5
6
7
8
9
10
<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
 
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
</dependency>
 
cs




2. application.properties 파일 설정


1
2
3
spring.application.name=user-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
cs


1 - application name을 user-service 로 설정, eureka server에 저 이름으로 등록됨

2 - eureka server url 등록


3. @EnableDiscoveryClient 어노테이션 추가 


1
2
3
4
5
6
7
8
9
10
@EnableDiscoveryClient 
//eureka, consul, zookeeper의 implements를 모두 포함. @EnableEurekaClient는 only works for eureka.
@SpringBootApplication
public class Eurekacleint1Application {
 
    public static void main(String[] args) {
        SpringApplication.run(Eurekacleint1Application.class, args);
    }
 
}
cs




4. Controller 추가(안해도 상관없음)


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.example.eurekacleint1.controller;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("/sample")
public class sampleController {
 
    @RequestMapping(method = RequestMethod.GET)
    String sample(){
        return "sample";
    }
 
}
 
cs




5. Eureka Server, Eureka Client  Run  



위의 사진처럼 서버에 등록된 것을 확인 할 수 있다.

또한 {eurekaServerUrl}/eureka/apps  를 확인하면 서비스 내용을 xml 형식으로 볼 수 있다.