JHipster를 활용한 MSA 구축 실습 :: 소림사의 홍반장!

JHipster를 활용한 MSA 구축 실습

2017. 6. 7. 15:48 - 삘쏘굿

slipp study를 통해 정리했던 내용들 다시 옮겨 놓음. ( SLiPP wiki 바로가기 )




1. 사전 세팅.


git repository : https://github.com/slipp-hipster/jhipster_msa_exam.git


  • msa 폴더로 클론 :

    git clone https://github.com/slipp-hipster/jhipster_msa_exam.git msa
  • submodule 초기화 및 업데이트

    cd msa && git submodule init && git submodule update
  • gateway node_module 추가 및 rebuild ( 이 과정을 생략하면 localhost:8080 접근 시 빈 화면만 보여집니다. )

    gateway> yarn install && yarn start
  • jhipster-registry 서버 배포 ( docker-compose 이용 )

    uaa> docker-compose -f ./src/main/docker/jhipster-registry.yml up -d
  • uaa, gateway, app1, app2 배포

    각 서비스별로 root> ./gradlew

gradlew 실행시 node/ npm 관련 오류 발생 시 삭제 후 재설치 하는것을 추천함. (ex. A problem occurred starting process 'command 'npm'' )

-> brew update | brew uninstall node | brew install node



 

2. generator-jhipster 업그레이드.

yarn global upgrade generator-jhipster



3. jhipster msa 설치 ( uaa, gateway, app1, app2 )
http://www.jhipster.tech/microservices-architecture/
http://www.jhipster.tech/using-uaa/


각 서버를 세팅할 디렉토리를 생성하고 각 디렉토리에서 jhipster를 실행하여 서버를 세팅한다



msa> mkdir uaa gateway app1 app2
 
uaa> jhipster
Welcome to the JHipster Generator v4.10.0
Documentation for creating an application: http://www.jhipster.tech/creating-an-app/
Application files will be generated in folder: /Users/hkp/study/slipp/13_jhipster/msa/uaa
? (1/16) Which *type* of application would you like to create? JHipster UAA server (for microservice OAuth2 authentication)
? (2/16) What is the base name of your application? uaa
? (3/16) As you are running in a microservice architecture, on which port would like your server to run? It should be unique to avoid port conflicts. 9999
? (4/16) What is your default Java package name? net.slipp.jhipster
? (5/16) Which service discovery server do you want to use? JHipster Registry (uses Eureka, provides Spring Cloud Config support and monitoring dashboards)
? (6/16) Which *type* of database would you like to use? SQL (H2, MySQL, MariaDB, PostgreSQL, Oracle, MSSQL)
? (7/16) Which *production* database would you like to use? MySQL
? (8/16) Which *development* database would you like to use? H2 with disk-based persistence
? (9/16) Do you want to use Hibernate 2nd level cache? Yes, with HazelCast (distributed cache, for multiple nodes)
? (10/16) Would you like to use Maven or Gradle for building the backend? Gradle
? (11/16) Which other technologies would you like to use?
? (12/16) Would you like to enable internationalization support? Yes
? Please choose the native language of the application Korean
? Please choose additional languages to install English
? (13/16) Besides JUnit and Karma, which testing frameworks would you like to use? Gatling, Cucumber
? (14/16) Would you like to install other generators from the JHipster Marketplace? No
 
gateway> jhipster
 Welcome to the JHipster Generator v4.10.0
Documentation for creating an application: http://www.jhipster.tech/creating-an-app/
Application files will be generated in folder: /Users/hkp/study/slipp/13_jhipster/msa/gateway
? (1/16) Which *type* of application would you like to create? Microservice gateway
? (2/16) What is the base name of your application? gateway
? (3/16) As you are running in a microservice architecture, on which port would like your server to run? It should be unique to avoid port conflicts. 8080
? (4/16) What is your default Java package name? net.slipp.jhipster
? (5/16) Which service discovery server do you want to use? JHipster Registry (uses Eureka, provides Spring Cloud Config support and monitoring dashboards)
? (6/16) Which *type* of authentication would you like to use? Authentication with JHipster UAA server (the server must be generated separately)
? (7/16) What is the folder path of your UAA application? ../uaa
? (8/16) Which *type* of database would you like to use? SQL (H2, MySQL, MariaDB, PostgreSQL, Oracle)
? (9/16) Which *production* database would you like to use? MySQL
? (10/16) Which *development* database would you like to use? H2 with disk-based persistence
? (11/16) Would you like to use Maven or Gradle for building the backend? Gradle
? (12/16) Which other technologies would you like to use? Clustered HTTP sessions using Hazelcast
? (13/16) Which *Framework* would you like to use for the client? Angular 4
? (14/16) Would you like to use the LibSass stylesheet preprocessor for your CSS? No
? (15/16) Would you like to enable internationalization support? Yes
? Please choose the native language of the application Korean
? Please choose additional languages to install English
? (16/16) Besides JUnit and Karma, which testing frameworks would you like to use? Gatling, Cucumber, Protractor
? (17/16) Would you like to install other generators from the JHipster Marketplace? No
 
app1> jhipster
 Welcome to the JHipster Generator v4.10.0
Documentation for creating an application: http://www.jhipster.tech/creating-an-app/
Application files will be generated in folder: /Users/hkp/study/slipp/13_jhipster/msa/app1
? (1/16) Which *type* of application would you like to create? Microservice application
? (2/16) What is the base name of your application? app1
? (3/16) As you are running in a microservice architecture, on which port would like your server to run? It should be unique to avoid port conflicts. 8081
? (4/16) What is your default Java package name? net.slipp.jhipster
? (5/16) Which service discovery server do you want to use? JHipster Registry (uses Eureka, provides Spring Cloud Config support and monitoring dashboards)
? (6/16) Which *type* of authentication would you like to use? Authentication with JHipster UAA server (the server must be generated separately)
? (7/16) What is the folder path of your UAA application? ../uaa
? (8/16) Which *type* of database would you like to use? SQL (H2, MySQL, MariaDB, PostgreSQL, Oracle)
? (9/16) Which *production* database would you like to use? MySQL
? (10/16) Which *development* database would you like to use? H2 with disk-based persistence
? (11/16) Do you want to use Hibernate 2nd level cache? Yes, with HazelCast (distributed cache, for multiple nodes)
? (12/16) Would you like to use Maven or Gradle for building the backend? Gradle
? (13/16) Which other technologies would you like to use?
? (14/16) Would you like to enable internationalization support? Yes
? Please choose the native language of the application Korean
? Please choose additional languages to install English
? (15/16) Besides JUnit and Karma, which testing frameworks would you like to use? Gatling, Cucumber
? (16/16) Would you like to install other generators from the JHipster Marketplace? No
 
app2> jhipster
 Welcome to the JHipster Generator v4.10.0
Documentation for creating an application: http://www.jhipster.tech/creating-an-app/
Application files will be generated in folder: /Users/hkpking/study/slipp_msa_uaa/slipp-app2
? (1/16) Which *type* of application would you like to create? Microservice application
? (2/16) What is the base name of your application? app2
? (3/16) As you are running in a microservice architecture, on which port would like your server to run? It should be unique to avoid port conflicts. 8082
? (4/16) What is your default Java package name? net.slipp.jhipster
? (5/16) Which service discovery server do you want to use? JHipster Registry (uses Eureka, provides Spring Cloud Config support and monitoring dashboards)
? (6/16) Which *type* of authentication would you like to use? Authentication with JHipster UAA server (the server must be generated separately)
? (7/16) What is the folder path of your UAA application? ../slipp-uaa
? (8/16) Which *type* of database would you like to use? SQL (H2, MySQL, MariaDB, PostgreSQL, Oracle)
? (9/16) Which *production* database would you like to use? MySQL
? (10/16) Which *development* database would you like to use? H2 with disk-based persistence
? (11/16) Do you want to use Hibernate 2nd level cache? Yes, with HazelCast (distributed cache, for multiple nodes)
? (12/16) Would you like to use Maven or Gradle for building the backend? Gradle
? (13/16) Which other technologies would you like to use?
? (14/16) Would you like to enable internationalization support? Yes
? Please choose the native language of the application Korean
? Please choose additional languages to install English
? (15/16) Besides JUnit and Karma, which testing frameworks would you like to use? Gatling, Cucumber
? (16/16) Would you like to install other generators from the JHipster Marketplace? No


3. Entity 추가


   * app1 : Owner, Contents

   * app2 : Product

   * gateway : Owner, Contents, Product


각 서버에 해당 Entity를 추가한다. 각 서버의 디렉토리 위치에서 다음 명령어를 실행.



app1> jhipster entity owner



app1> jhipster entity contents



app2> jhipster entity product



동일한 방법으로 gateway에도 product, owner, contents entity 추가


jhipster entity owner
jhipster entity contents
jhipster entity product


4. app1(owner) - uaa(user) entity 연동

UserClient
1
2
3
4
5
@AuthorizedFeignClient(name = "uaa")
public interface UserClient {
    @GetMapping("/api/users")
    List<Owner> getOwners();
OwnerResource
1
2
@Inject
private UserClient userClient;


5. jhipster ci-cd 사용하여 jenkins 설치 (http://www.jhipster.tech/setting-up-ci/)


  1. 1. jhipster ci-cd 실행

    ➜ uaa jhipster ci-cd
    Using JHipster version installed locally in current project’s node_modules
    Executing jhipster:ci-cd
    Options:
    [Beta] Welcome to the JHipster CI/CD Sub-Generator
    ? What CI/CD pipeline do you want to generate? Jenkins pipeline
    ? Jenkins pipeline: what tasks/integrations do you want to include? Analyze code with Sonar
    ? What is the name of the Sonar server? Sonar
    ? Deploy to heroku?
     create Jenkinsfile
     create src/main/docker/jenkins.yml
     create src/main/resources/idea.gdsl
    Execution complete

     

    2. 생성된 jenkins.yml 내용 수정 (image 최신버전으로 변경(jenkins -> jenkins/jenkins), 기본 포트 49001 -> 18080으로 변경)

    version: '2'
    services:
      jenkins:
        image: jenkins/jenkins
        ports:
          18080:8080
          50000:50000
        # uncomment for docker in docker
        #privileged: true
        #volumes:
            # enable persistent volume (warning: make sure that the local jenkins_home folder is created)
            #- ~/volumes/jenkins_home:/var/jenkins_home
            # mount docker sock and binary for docker in docker (only works on linux)
            #- /var/run/docker.sock:/var/run/docker.sock
            #- /usr/bin/docker:/usr/bin/docker

     

    3. jenkins 실행

    ➜ uaa docker-compose -f src/main/docker/jenkins.yml up -d


    4. jenkins 접속 
    1) 브라우저에서 localhost:18080 로 접속
    2) Adminastrator password 입력 : 아래의 명령어로 jenkins 도커 컨테이너 내부에 접근하여 해당 경로의 파일에서 비밀번호를 출력한다.

    docker exec docker_jenkins_1 bash -c 'cat /var/jenkins_home/secrets/initialAdminPassword'

    5. jenkins 설치 : Install suggested plugins 를 선택하여 간단하게 설치를 시작한다.

     

    6. pipeline 작업 생성

    : 설치 완료 후 새작업을 클릭하여 작업을 생성한다. (참고 : http://www.jhipster.tech/setting-up-ci-jenkins2/ )

    7. sonarqube 연동

    : 빌드단계에서 자동으로 코드 품질을 테스트 할 수 있도록 sonarqube 연동

    - sonar 서버 시작 : docker-compose -f src/main/docker/sonar.yml up -d

    sonarqube 세팅

     

    - sonarqube token 발급 및 프로젝트 세팅 : localhost:9000 접속 > login (admin/admin) > tutorial 입력

    jenkins 세팅

    - plugin 설치

    : Jenkins 관리 > 플러그인 관리 > 설치 가능 > SonarQube Scanner for Jenkins 설치

     

    - sonar 연동
    : Jenkins 관리 > 시스템 설정 > SonarQube servers
- Add SonarQube click
- Enable injection of SonarQube … check
- name : Sonar
- Server Url : http://{server ip}:9000
- Server authentication token : SonarQube에서 발급 받은 토큰
 (SonarQube > My Account > Security : Generate Tokens)

     

    8. 최초 작업 실행

    Jenkins project > Build Project 메뉴를 눌러 최초 pipeline 연결을 확인 한다.

     

    9. ngrok 실행

    : 로컬에서 구동중인 jenkins를 외부에서 접근 가능하도록 ngrok을 통해 도메인을 생성한다.
    : 다운로드는 여기서 -> https://ngrok.com/download

    ngrok http 18080

     

    10. github 연동

    : git commit 발생시 자동으로 빌드하도록 연동

    github 세팅

     repository > settings > Integrations & services > Add service > Jenkins (GitHub plugin)

    Jenkins hook url : http://b4863c42.ngrok.io/github-webhook/

    jenkins 세팅

    작업 설정 > GitHub hook trigger 선택



6. docker-compose 활용하여 prod 배포 (http://www.jhipster.tech/docker-compose/)

  1. 1. 각 서비스별로 도커 이미지로 빌드

    ➜ uaa git:(master) ✗ ./gradlew bootRepackage -Pprod buildDocker
    ➜ gateway git:(master) ✗ ./gradlew bootRepackage -Pprod buildDocker
    ➜ app1 git:(master) ✗ ./gradlew bootRepackage -Pprod buildDocker
    ➜ app2 git:(master) ✗ ./gradlew bootRepackage -Pprod buildDocker

     

    2. msa root 폴더에 docker compose용 디렉토리 생성

    ➜  msa_sample git:(guide) mkdir docker && cd docker

     

    3. jhipster docker-compose 실행

    ➜ docker git:(master) ✗ jhipster docker-compose
    Using JHipster version installed globally
    Executing jhipster:docker-compose
    Options:
    Welcome to the JHipster Docker Compose Sub-Generator
    Files will be generated in folder: /Users/hkp/study/slipp/13_jhipster/msa_sample/docker
    Found .yo-rc.json config file...
    ? Which *type* of application would you like to deploy? Microservice application
    ? Which *type* of gateway would you like to use? JHipster gateway based on Netflix Zuul
    ? Enter the root directory where your gateway(s) and microservices are located ../
    4 applications found at /Users/hkp/study/slipp/13_jhipster/msa_sample/
    ? Which applications do you want to include in your configuration? app1, app2, gateway, uaa
    ? Do you want to setup monitoring for your applications ? Yes, for logs and metrics with the JHipster Console (based on ELK and Zipkin)
    ? You have selected the JHipster Console which is based on the ELK stack and additional technologies, which one do you want to use ?
    JHipster registry detected as the service discovery and configuration provider used by your apps
    ? Enter the admin password used to secure the JHipster Registry admin
    Checking Docker images in applications' directories...
    identical docker-compose.yml
    identical README-DOCKER-COMPOSE.md
    identical jhipster-registry.yml
    identical central-server-config/application.yml
    identical jhipster-console.yml
    identical log-conf/logstash.conf
    identical log-data/.gitignore
    Docker Compose configuration successfully generated!
    You can launch all your infrastructure by running : docker-compose up -d
    Congratulations, JHipster execution is complete!


7. jhipster-console 모니터링 확인(http://www.jhipster.tech/monitoring/)

  1. 1. jhipster-console.yml 수정

    jhipster-elasticsearch:
            image: jhipster/jhipster-elasticsearch:v2.2.1
            ports:
                9200:9200
            environment:
                "ES_JAVA_OPTS=-Xms512m -Xmx512m"
            ulimits:
                memlock:
                    soft: -1
                    hard: -1
            mem_limit: 1g
     
    ELASTICSEARCH_URL=http://{local ip}:9200 으로 변경

     

    2. application-prod.yml > log 활성화

    jhipster.metrics.logs.enabled : true
    jhipster.logging.logstash.enabled : true
    jhipster.logging.logstash.host : {local ip}






















다른 카테고리의 글 목록

Dev. 자바/JHipster 카테고리의 포스트를 톺아봅니다