Spring Boot Admin Server, Client config 설정하기

이미지
 Spring Boot로 많은 프로젝트를 진행하게 됩니다. 많은 모니터링 도구가 있지만, Spring Boot 어플리케이션을 쉽게 모니터링 할 수 있는 방법을 소개하려고 합니다.   코드 중심으로 살펴보겠습니다. 1. 어드민 서버 구축 1-1. 디펜던시 추가 dependencies { // https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server implementation 'de.codecentric:spring-boot-admin-starter-server:2.5.4' } 1-2. 어드민 서버 설정 활성화 @SpringBootApplication @EnableAdminServer public class ServerApplication { public static void main (String[] args) { SpringApplication. run (ServerApplication. class, args) ; } } EnableAdminServer를 하면 됩니다. 2. 클라이언트 서버 설정  예제는 book-client, member-client 2가지 클라이언트, member-client가 2개의 인스턴스 실행으로 작성했습니다.  2-1 디펜던시 추가 dependencies { // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web implementation 'org.springframework.boot:spring-boot-starter-web:2.5.4' // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-actuator implementation 'org.spring...

자바스크립트 Array 를 map 으로 변환, 일부만 변경, 합치기

javascript array to map

Array(배열, 리스트, List)를 map으로 변환하여 특정키의 값이 존재하는지 여부를 찾고 싶을 때가 있습니다.

(저는 자바를 주로 사용하기 때문에 자바 stream의 collect  사용하고 싶었습니다.)


Array to map, list to map

const books = [
{id: 'a', name: '1', author: 'q'},
{id: 'b', name: '2', author: 'w'},
{id: 'c', name: '3', author: 'e'}
];

// array to map , list to map
const bookMap = books.reduce((acc, currentValue) => {
acc[currentValue.id] = currentValue;
return acc;
}, {});
console.log(bookMap);

const newBooks = [
{id: 'b', name: 'n2', author: 'w'},
{id: 'c', name: 'n3', author: 'e'},
{id: 'd', name: 'n4', author: 'r'},
{id: 'f', name: 'n5', author: 't'}
]

// 이전에 등록된 데이터 확인하기
const oldBooks = newBooks.filter(each => !bookMap[each.id])
console.log(oldBooks)

acc의 이전 결과 내용 부분에 현재 변수 currentValue의 id값으로 데이터를 할당합니다.


출력 결과

  1. {a: {…}, b: {…}, c: {…}}

  1. (2) [{…}, {…}]
    1. length2


부가 설명으로 자료형 중에서 map을 사용하여 데이터를 조회할 경우 시간복잡도는 O(1)가 됩니다.


배열 일부만 변경

// 두 배열에서 이전값이 있는경우 이전 데이터, 없으면 신규 데이터 할당하기
const mixedBooks = newBooks.map(each => bookMap[each.id] ? bookMap[each.id] : each)
console.log(mixedBooks)

출력 결과

    1. (4) [{…}, {…}, {…}, {…}]
      1. length4

newBooks 배열에서 id가 b,c는 bookMap의 데이터로 변경되어 할당된 것을 확인할 수 있습니다.


두 배열 합치기

// 두 배열 합치기
const arr1 = [1,2,3]
const arr2 = [4,5,6]
const mergedArr = [...arr1, ...arr2]
console.log(mergedArr)

출력 결과

  1. [1, 2, 3, 4, 5, 6]


...은 전개 구문이라고 합니다.


전개 구문을 사용하면 배열이나 문자열과 같이 반복 가능한 문자를 0개 이상의 인수 (함수로 호출할 경우) 또는 요소 (배열 리터럴의 경우)로 확장하여, 0개 이상의 키-값의 쌍으로 객체로 확장시킬 수 있습니다.

출처

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Spread_syntax


댓글

이 블로그의 인기 게시물

Spring boot redis cache config

Spring boot redis RedisTemplate

MySQL PK 중복 (duplicate key ) 해결 방법