## 0. Eureka 서버 프로젝트 생성 ![[Pasted image 20231221142621.png]] - [스프링 부트 프로젝트 생성 페이지](https://start.spring.io/)에서 프로젝트를 생성한다. ```java @EnableEurekaServer @SpringBootApplication public class EurekatestApplication { public static void main(String[] args) { SpringApplication.run(EurekatestApplication.class, args); } } ``` - 애플리케이션 메인 파일에 `@EnableEurekaServer` 어노테이션을 추가한다. ```yaml server:   port: 8002   spring:   application:     name: eurekatest     eureka:   client:     register-with-eureka: false     fetch-registry: false ``` - `application.properties`를 `application.yml`로 확장자를 변경한 다음 위의 코드를 입력한다. - 위 코드의 의미는 다음과 같다. - `server.port`: 포트를 설정한다. 여기서는 8002로 설정되어 있다. - `spring.application.name`: 애플리케이션의 이름을 설정한다. 여기서는 "eurekatest"로 설정되어 있다. - `eureka.client.register-with-eureka`: Eureka 서버에 자동으로 등록할지 여부를 설정한다. 이 설정은 Eureka 서버가 모니터링하는 서버에 해야 하는 설정이다. 따라서 false로 지정한다. - `eureka.client.fetch-registry`: Eureka 서버에서 등록 정보를 가져올지 여부를 설정한다. 마찬가지로 false로 지정한다. ```java @Configuration @EnableWebSecurity public class SecurityConfig { @Bean public BCryptPasswordEncoder bCryptPasswordEncoder(){ return new BCryptPasswordEncoder(); } @Bean public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception { httpSecurity.csrf(auth->auth.disable()); httpSecurity.authorizeHttpRequests(auth -> auth.anyRequest().authenticated()); httpSecurity.httpBasic(Customizer.withDefaults()); return httpSecurity.build(); } @Bean public UserDetailsService userDetailsService(){ UserDetails userDetails = User.builder() .username("eureka-manager") .password(bCryptPasswordEncoder().encode("qwerty123")) .roles("ADMIN") .build(); return new InMemoryUserDetailsManager(userDetails); } } ``` - [[7. Spring Cloud Config를 통한 Config 관리|Config 서버]]와 똑같이 Security 설정을 하자. 코드는 이전 Config 서버에서 사용된 코드를 다시 사용했다. ![[Pasted image 20231221152344.png]] - 애플리케이션을 기동한 다음 http://localhost:8002/ 에 접속하면 위와 같은 로그인 창이 뜬다. 위 Security Config에 적은 username과 password를 입력하면 된다. ![[Pasted image 20231221152357.png]] - 이런 화면이 뜬다면 정상적으로 애플리케이션이 생성된 것이다. ## 1. 기존 서버에 Eureka 클라이언트 추가 ```groovy implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' ``` - Eureka를 통해 모니터링할 서버에 Eureka 클라이언트 의존성을 추가한다. 여기서는 이전에 만든 [[7. Spring Cloud Config를 통한 Config 관리|Config 서버]]에 Eureka 클라이언트를 추가할 것이다. ```yml eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://eureka-manager:qwerty123@localhost:8002/eureka ``` - 다음으로는 설정 파일에 Eureka 클라이언트 설정을 추가하자. ![[Pasted image 20231221154259.png]] - 애플리케이션을 재기동하고 Eureka 페이지에 들어가면 위와 같이 Config 서버 애플리케이션이 추가된 걸 볼 수 있다. ![[Pasted image 20231221154710.png]] ```yml server:   port: 8001   spring:   application:     name: configservertest     eureka:   client:     register-with-eureka: true     fetch-registry: true     service-url:       defaultZone: http://eureka-manager:qwerty123@localhost:8002/eureka ``` - 이번에는 [[7. Spring Cloud Config를 통한 Config 관리#5. Config 서버 테스트|Config 서버 테스트 서버]]에 Eureka 클라이언트를 추가해보자. Config 리포지터리에 있는 `configservertest-dev.yml` 파일을 위와 같이 수정한다. ```groovy implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' ``` - 위에서 Config 서버에서 했던 것처럼 Eureka 클라이언트 의존성을 추가해준다. ![[Pasted image 20231221155313.png]] - 애플리케이션을 재기동하고 Eureka 페이지에 들어가면 위와 같이 Config 서버 테스트 애플리케이션도 추가된 걸 볼 수 있다.