Skip to content

Instantly share code, notes, and snippets.

@benelog
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benelog/bb6b23b669a1837ace62 to your computer and use it in GitHub Desktop.
Save benelog/bb6b23b669a1837ace62 to your computer and use it in GitHub Desktop.
Spring XD

1. Spring XD 설치

wget http://repo.spring.io/release/org/springframework/xd/spring-xd/1.0.1.RELEASE/spring-xd-1.0.1.RELEASE-dist.zip
unzip spring-xd-1.0.1.RELEASE-dist.zip
cd spring-xd-1.0.1.RELEASE/

이하 모든 경로는 위의 마지막 디렉토리를 기준으로 함.

2. JDBC Driver 다운로드

쓰고 싶은 DB의 JDBC드라이버를 다운로드해서 xd/lib 폴더에 복사. 여기서는 H2 DB를 사용함.

cd xd/lib
wget http://central.maven.org/maven2/com/h2database/h2/1.4.181/h2-1.4.181.jar

참고로 MySql과 Postgres, H2DB의 jar파일은 XD에서 참조할 수 있는 위치에 이미 포함되어 있음.

3.JDBC url설정

jdbc.properties을 수정함

vi xd/config/modules/sink/jdbc/jdbc.properties

드라이버클래스, JDBC URL등을 원하는 DB로 변경

driverClassName=org.h2.Driver
url=jdbc:h2:~/apps/h2db;AUTO_SERVER=TRUE
username=sa
password=sa

4. Stream 생성 (JSON API 포트 개방)

Spring-XD shell실행

./shell/bin/xd-shell 

8181번 포트로 들어오는 JSON body를 바로 JDBC로 입력하는 Stream을 생성함

stream create --name http-to-jdbc --definition "http --port=8181  | jdbc --tableName=person --columns=name,address --initializeDatabase=true" --deploy

5. 테스트

HTTP 호출

Spring XD Shell에서 아래와 같이 실행하거나

http post --target http://localhost:8181 --data "{\"name\":\"Kim\",\"address\":\"Seoul\"}"

OS Shell에서 curl을 이용해도 됨.

curl -XPOST 'http://localhost:8181/' -d '{"name":"benelog","address":"Seoul"}'

DB에서 결과 확인

JDBC URL로 DB 접속. H2 Console을 이용할 수도 있음. ( https://github.com/benelog/web-db-console/ 참고)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment