네이버클라우드플랫폼(Ncloud)

NCP 3티어 아키텍처 - WEB, WAS, DB연동

bo20cy 2024. 3. 22. 09:34

이전 글 참조
https://bo20cy.tistory.com/10

 

NCP 3티어 아키텍처 - WEB, WAS설정

테라폼 글 참조 https://bo20cy.tistory.com/5 테라폼 서버 생성(+nic) 이전 포스트와 연결됩니다. 테라폼 서버 생성하기 resource "ncloud_login_key" "loginkey" { key_name = "test-key" } resource "ncloud_vpc" "test" { ipv4_cidr_blo

bo20cy.tistory.com

 

설명

3티어 구성 서버 내부에 AP들을 심어서 구성을 했습니다.

 

WEB에는 NGINX, WAS에는 Tomcat을 연동시키고

 

WAS에는 Tomcat에 ,DB에는 Cloud for MySQL을 연동시켜보겠습니다.

 

하지만 편의성을 위해 한서버에서 작동시키고 나중에 따로 연동 시키는 걸 보여드리겠습니다.

 

방법

Centos 7.8 기준

NGINX , Tomcat 연동

Web서버에서 reverse proxy를 사용하여 conf값을 변동하여 연동해줍니다.

sed -i '10i\    proxy_pass http://localhost:8080;'  /etc/nginx/conf.d/default.conf

 

/etc/nginx/conf.d/default.conf 파일 아래에

 

sed -i 명령어로 10번째 줄에proxy_pass http://localhost:8080; 을 추가해 줍니다.

끝났습니다. 한번 재시작 해주겠습니다.

 

완료가 되었고

공인ip 에 들어가 보면

톰캣이 받겨줍니다.

 

 

MySQL 연동

 

WAS서버에서 MySQL 연동를 위해 mysql-connector가 필요하니 다운해 줍니다.


mysql-connector 공식사이트
https://downloads.mysql.com/archives/c-j/

 

MySQL :: Download MySQL Connector/J (Archived Versions)

Please note that these are old versions. New releases will have recent bug fixes and features! To download the latest release of MySQL Connector/J, please visit MySQL Downloads. MySQL open source software is provided under the GPL License.

downloads.mysql.com

wget -P /usr/local/ https://downloads.mysql.com/archives/get/p/3/file/mysql-connector-j-8.2.0.tar.gz
tar -xvf /usr/local/mysql-connector-j-8.2.0.tar.gz -C /usr/local/
mv /usr/local/mysql-connector-j-8.2.0/mysql-connector-j-8.2.0.jar /usr/local/tomcat/lib

 

mysql-connector을 tomcat library에 집어 넣어줍니다.

 

 

확인 페이지를 작성합니다.

cat << EOF >> /usr/local/tomcat/webapps/ROOT/mysql.jsp
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=utf-8" %>
<%
         String DB_URL = "jdbc:mysql://##Private 도메인##:3306/##db-name##";
         String DB_USER = "##user-name##";
         String DB_PASSWORD= "##password##";
         Connection conn;
         Statement stmt;

         try {
              Class.forName("com.mysql.jdbc.Driver");
              conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
              stmt = conn.createStatement();
              conn.close();
              out.println("MySQL Connection Success!");
         }
         catch(Exception e){
              out.println(e);
         }
%>
EOF

 

##Private 도메인##
##db-name##
##user-name##
##password##
부분을 수정해 줍니다.

잘 작동되면

"MySQL Connection Success!"

가 뜹니다.

 

 

 

bash /usr/local/tomcat/bin/shutdown.sh
bash /usr/local/tomcat/bin/startup.sh

그리고 톰캣을 재시작하고

 

공인IP/mysql.jsp 에 들어가서 확인 할 수 있습니다.

 

이로써 연동 확인이 끝났습니다!