Yeji's Tech Notes
article thumbnail
반응형

문제상황

사이드 프로젝트에서 MSA로 전환중에 kafka 토픽 발행 작업 중에서 약간의 문제가 있었습니다.

알고보니 회원가입시 mail-service에서 전송된 mail정보를 객체로 생성해 토픽을 전송하는데, 토픽을 받은 서비스가 deSerializing하는 과정에서 다른 객체가 넘어와서 생긴 문제였습니다.

 

현재, 인터페이스를 만들지 않아 의사소통의 문제가 생겨 서로 다른 객체를 가지고 있는 점, 토픽 발행 및 전달시 서비스마다 객체를 가져야되는 점 때문에 별도의 common-service를 만들어 개발시 공통으로 사용하는 부분들은 라이브러리로 사용하기로 결정지었습니다.

 

Nexus 세팅하기

Docker 실행하기

docker 커맨드를 활용하여 데이터 볼륨 연동 후 nexus를 실행하였습니다.

$ docker volume create --name nexus-data
$ docker run -d -p 8081:8081 --name nexus -v nexus-data:/nexus-data sonatype/nexus3

 

로그인하기

우선 오른쪽 위 Sign in 버튼을 클릭해 admin계정으로 로그인해줍니다. pasword는 팝업창에 위치가 명시되어 있습니다.

ID : admin

Password : nexus설치 디렉토리의 admin.password파일에 위치

접근성 설정

- Enable anonymous access

자격증명이 없이 접속 가능하도록

- Disable anonymous access

자격증명이 있는 사람만 접근허용

저는 자격증명을 하도록 설정해줬습니다.

 

레포지토리 생성

레포지토리는 Release,Snapshot, Group 총 3개을 생성할 것입니다.

 

라이브러리 관리를 위한 레포지토리를 생성해야합니다. 레포지토리는 크게 2가지 타입을 가집니다.

- SNAPSHOT

개발 과정 중에 사용되는 레포지토리

- RELEASE

개발 완료 후 실제 배포 버전이 존재하는 레포지토리

 

maven (hosted) 클릭

release 레포 생성

 

위의 작업과 똑같이 Snapshot 레포 생성

- maven2 (hosted) 클릭

- tgather-snapshot, Version policy (SNAPSHOT)

 

maven2 (group) 클릭

group 레포 생성

 

group레포에 Members 추가하기 (release,snapshot 레포)

 

Security Role, User 생성

Create Role 선택

레포지토리명을 포함하고 있는 모든 repo 추가

User 생성

user에 전에 생성한 Role 추가

이제 nexus의 레포지토리 권한 사용자 등 필요한 세팅은 완료했습니다. 이제 프로젝트를 nexus 레포지토리에 올리기 위해 gradle 을 수정해주겠습니다.

 

build.gradle 설정

 

플러그인 추가

plugins {
	...
    id 'maven-publish'
    id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
}

 

배포할 모듈 설정

jar { // 배포할 jar 이름
    archiveName("${project.name}-${version}.jar")
}


publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId group
            artifactId project.name
            version version

            artifact("build/libs/$project.name-$version" + ".jar") {
                extension 'jar'
            }
        }
    }
    repositories {
        maven {
            credentials {
                username = repoUsername
                password = repoPassword
            }
            def releasesRepoUrl = nexusBaseUrl + "/tgather-release"
            def snapshotsRepoUrl = nexusBaseUrl + "/tgather-snapshot"
            url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
            allowInsecureProtocol = true    // http 허용, https로 변경하면 필요없음.
        }
    }
}

repositories {
    mavenCentral()
    maven {
        credentials {
            username = repoUsername    
            password = repoPassword  
        }
        url "${nexusBaseUrl}/tgather-public/"
        allowInsecureProtocol = true    
    }
}

- username : repoUsername

- password : repoPassword

해당 값은 개인정보를 담고 있기 때문에 gradle.properties에서 별도로 관리하도록 합니다.

 

gradle로 배포하기

gradle publish

 

확인하기

이제 최종적으로 nexus에 올라간 common-service 프로젝트가 잘 갖고오는지 확인해보겠습니다.

 

common-service에 EmailMessage 생성

@Data
@Builder
public class EmailMessage {

    private String accountId;

    private String to;

    private MailSubject mailSubject;

    private String message;

    public String getSubject() {
        return mailSubject.getSubject();
    }

    public String getHtmlCode() {
        return mailSubject.getHtmlFileName();
    }

    public EmailType getEmailType() {
        return mailSubject.getEmailType();
    }

}

 

pull받을 서비스 gradle 설정 추가

 

gradle.properties에 아래 설정 분리

- ${nexusBaseUrl} , repoUsername, repoPassword

세개의 설정은 개인정보가 포함되어있으므로 properties에 별도로 분리하였습니다.

repositories {
    mavenCentral()
    maven {
        credentials {
            username = repoUsername   
            password = repoPassword  
        }
        url "${nexusBaseUrl}/tgather-public/"
        allowInsecureProtocol = true   
    }
}

 

dependency 추가

dependencies{
	
     implementation 'com.sgyj:common-service:1.0.8'
 
 }

 

서비스에서 객체 호출해보기

import com.sgyj.commonservice.dto.mail.EmailMessage;
import com.sgyj.commonservice.dto.mail.MailSubject;
import com.sgyj.mailservice.modules.entity.Email;
import com.sgyj.mailservice.modules.repository.MailRepository;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class HtmlEmailServiceTest {

    @Autowired
    private EmailService emailService;

    @Autowired
    private MailRepository mailRepository;

    @Test
    @DisplayName("emailMessage 가져오기 확인")
    void test_case_1() {
        EmailMessage emailMessage = EmailMessage.builder().accountId("ACCOUNTID").message("AUTHCODE").mailSubject(MailSubject.VALID_AUTHENTICATION_ACCOUNT)
            .build();

        assertInstanceOf(EmailMessage.class, emailMessage);
    }
    
    
}

 

정상적으로 가져오는 것을 확인했습니다

 

소스코드 첨부

 

https://github.com/ys-developer/common-service

 

GitHub - ys-developer/common-service

Contribute to ys-developer/common-service development by creating an account on GitHub.

github.com

 

https://github.com/ys-developer/mail-service

 

GitHub - ys-developer/mail-service

Contribute to ys-developer/mail-service development by creating an account on GitHub.

github.com

 

반응형
profile

Yeji's Tech Notes

@Jop

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!