라벨이 MySQL인 게시물 표시

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...

Spring boot webflux + mysql

스프링 부트의 webflux의 rest api 예제 입니다. 샘플 코드 https://github.com/withccm/spring-webflux-study/tree/feature/webflux-mysql-sample 북 테이블 CREATE TABLE `book` ( `id` int ( 11 ) NOT NULL AUTO_INCREMENT , `bookname` varchar ( 200 ) DEFAULT NULL , PRIMARY KEY (`id`) ) ; 1. 의존성 추가 implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc' runtimeOnly 'mysql:mysql-connector-java' implementation 'dev.miku:r2dbc-mysql' 2. spring data r2dbc bean 등록 @Configuration @EnableTransactionManagement @EnableR2dbcRepositories public class DataSourceR2DBCConfig extends AbstractR2dbcConfiguration { @Bean @Primary public ConnectionFactory connectionFactory () { ConnectionFactoryOptions options = ConnectionFactoryOptions. builder () .option(ConnectionFactoryOptions. DRIVER , "pool" ) .option(ConnectionFactoryOptions. PROTOCOL , "mysql" ) .option(ConnectionFactoryOptions. HOST , "localhost" ...

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

데이터베이스에 데이터를 입력하다보면 pk가 중복되어 오류(duplicate key)가 발생하는 경우가 있습니다. 이런 경우를 방지하기 위해서 조회를 해보고 없으면 신규 인서트(삽입), 있으면 무시하거나 업데이트 하도록 종종 개발할 때가 있습니다. 어플리케이션에서 조회하고 다음 쿼리를 호출하게 되는 경우 동시성 문제가 발생할 수 있어요. 그리고 조회해서 쿼리를 또 사용할 경우 네트워크 비용이 발생하는 단점이 있습니다. (성능이 좋지 못함) 동시성 문제란 여러 스레드나 프로세스에서 동일한 데이터를 접근할때 발생할 수 있고, 이때 데이터가 실제 데이터와 다른현상이 발생합니다. 예를들어 a, b 스레드에서 데이터를 조회하여 빈 값을 획득한경우 신규 데이터 인서트를 a와 b에서 실행하게 될때(순사적으로 실행되었다고 가정), b의 인서트를 실행할때 이미 a가 인서트 완료했기 때문에 오류가 발생합니다.  위의 문제를 쿼리를 통해 간단히 해결할 수 있습니다. user_log : 사용자로그 테이블  - userNo : 사용자번호 (PK) - lastLogAt : 마지막활동기록 PK중복인 경우 업데이트 (단건) INSERT INTO user_log SET userNo = 123, lastLogAt = 202101201043 ON DUPLICATE KEY UPDATE lastLogAt = 202101201043 INSERT INTO user_log (userNo, lastLogAt) VALUES (123, 202101201043) ON DUPLICATE KEY UPDATE lastLogAt = 202101201043 mybatis <insert id ="insert" parameterType ="map" > INSERT INTO user_log SET userNo = #{userNo}, lastLogAt = #{lastLogAt} ON DUPLICATE ...