TIL 23.12.14

ํ™ฉ์€ํ•˜ยท2023๋…„ 12์›” 14์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
135/146
post-thumbnail

๐Ÿ“ŒToday I Learned

ElasticSearch

์ง€๋‚œ ์‹œ๊ฐ„ elasticsearch ๊ฒฐ๊ณผ๊ฐ€ Spring์—์„œ ์ž˜ ๋‚˜์˜ค์ง€ ์•Š์€ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์—ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ์œ„ํ•ด ์•„๋ž˜ ์ž‘์—…์„ ์ง„ํ–‰ํ•œ๋‹ค.

WSL ์ดˆ๊ธฐํ™” + Java ์„ค์น˜ + ELK ์„ค์น˜

WSL ์ดˆ๊ธฐํ™”

  1. ์•ฑ ๋ฐ ๊ธฐ๋Šฅ
  2. Ubuntu-22.04
  3. ๊ณ ๊ธ‰ ์˜ต์…˜
  4. ์ดˆ๊ธฐํ™” ํด๋ฆญ

-> ์ปดํ“จํ„ฐ ์žฌ๋ถ€ํŒ…

ubuntu ์—ด๊ณ 
$ wsl -l -v
$ wsl --unregister Ubuntu-22.04

๋‹ค์‹œ ์šฐ๋ถ„ํˆฌ ์‹คํ–‰

username: ubuntu
pw: java1234

ํŒจํ‚ค์ง€ ์—…๋ฐ์ดํŠธ
$ sudo apt-get update

์ž๋ฐ”(JRE, JDK์„ค์น˜)
$ sudo apt-get install default-jre
$ sudo apt-get install default-jdk

Elasticsearch ์„ค์น˜

$ echo "deb [signed-by=/usr/share/keyrings/elastic.gpg] https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list
$ sudo apt-get update

$ sudo apt-get install elasticsearch

Kibana ์„ค์น˜ํ•˜๊ธฐ
$ sudo apt-get install kibana

Logstash ์„ค์น˜ํ•˜๊ธฐ
$ sudo apt-get install logstash

์ž๋™์‹œ์ž‘
$ sudo systemctl enable elasticsearch
$ sudo systemctl enable kibana

์™ธ๋ถ€ ์ ‘์† ํ—ˆ์šฉ
$ sudo vi /etc/elasticsearch/elasticsearch.yml
network.host: 0.0.0.0
cluster.initial_master_nodes: ["node-1"]

$ sudo systemctl restart elasticsearch
$ sudo systemctl start kibana

์‚ฌ์ดํŠธ ๋“ค์–ด๊ฐ€์„œ ํ™•์ธ
http://localhost:9200/
http://localhost:5601/

wsl ์ •์ง€
๋ช…๋ น ํ”„๋กฌํ”„ํŠธ์—์„œ
> wsl --shutdown

$ sudo systemctl stop elasticsearch
$ sudo systemctl stop kibana
$ sudo vi /etc/elasticsearch/elasticsearch.yml

์ฃผ์„์ฒ˜๋ฆฌ

#network.host: 0.0.0.0
#cluster.initial_master_nodes: ["node-1"]
:wq

$ sudo systemctl start elasticsearch
$ sudo systemctl start kibana

์‚ฌ์ดํŠธ ๋“ค์–ด๊ฐ€์„œ ํ™•์ธ
http://localhost:9200/
http://localhost:5601/

$ sudo systemctl stop elasticsearch
$ sudo systemctl stop kibana

$ sudo vi /etc/elasticsearch/elasticsearch.yml

์ฃผ์„ ํ’€๊ธฐ

network.host: 0.0.0.0
cluster.initial_master_nodes: ["node-1"]

:wq

$ sudo systemctl daemon-reload
$ sudo systemctl start elasticsearch
$ sudo systemctl start kibana

์Šคํ”„๋ง ์‹คํ–‰

์•„๋ž˜ ์ฝ”๋“œ ๋„ฃ๊ธฐ

PUT spring
{
  "mappings": {
    "properties": {
      "message": {
        "type": "text"
      }
    }
  }
}

POST _bulk
{ "index" : { "_index" : "spring", "_id" : "1" } }
{"message":"The quick brown fox"}
{ "index" : { "_index" : "spring", "_id" : "2" } }
{"message":"The quick brown fox jumps over the lazy dog"}
{ "index" : { "_index" : "spring", "_id" : "3" } }
{"message":"The quick brown fox jumps over the quick dog"}
{ "index" : { "_index" : "spring", "_id" : "4" } }
{"message":"Brown fox brown dog"}
{ "index" : { "_index" : "spring", "_id" : "5" } }
{"message":"Lazy jumping dog"}
{ "index" : { "_index" : "spring", "_id" : "6" } }
{"message":"์ง€๋ถ• ์œ„์˜ ๊ฐˆ์ƒ‰ ๋‹ญ"}
{ "index" : { "_index" : "spring", "_id" : "7" } }
{"message":"์ง€๋ถ• ์œ„์˜ ๊ฐˆ์ƒ‰ ๋‹ญ ๊ทธ๋ฆฌ๊ณ  ๋ฐ‘์— ๊ฒ€์€์ƒ‰ ๊ฐ•์•„์ง€"}
{ "index" : { "_index" : "spring", "_id" : "8" } }
{"message":"์ง€๋ถ• ์œ„์˜ ๊ฐˆ์ƒ‰ ์šฐ๋Š” ๋‹ญ ๊ทธ๋ฆฌ๊ณ  ๋ฐ‘์— ๊ฐˆ์ƒ‰ ๊ฒŒ์œผ๋ฅธ ๊ฐ•์•„์ง€"}
{ "index" : { "_index" : "spring", "_id" : "9" } }
{"message":"๊ฐˆ์ƒ‰ ๊ฒŒ์œผ๋ฅธ ๋ฐ”๋ณด ๊ฐ•์•„์ง€ ์˜†์— ๋นจ๊ฐ„์ƒ‰ ๋‹ญ"}
{ "index" : { "_index" : "spring", "_id" : "10" } }
{"message":"์กธ๊ณ  ์žˆ๋Š” ๊ฐˆ์ƒ‰ ๊ฐ•์•„์ง€"}

์ƒˆ ํ”„๋กœ์ ํŠธ

New > Spring Legacy Project > Spring MVC Project > "ElasticsearchProject" > "com.text.project" > Finish

์‹œ๋‚˜๋ฆฌ์˜ค

  1. ์Šคํ”„๋ง ํ”„๋กœ์ ํŠธ๊ฐ€ ์™„์„ฑ๋œ ์ƒํƒœ(๋„์„œ ๊ด€๋ฆฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜)
  2. ๋„์„œ ๋ชฉ๋ก๋ณด๊ธฐ, ๋„์„œ ์ƒ์„ธ๋ณด๊ธฐ, ๋„์„œ ์ถ”๊ฐ€ํ•˜๊ธฐ๊ฐ€ ๊ตฌํ˜„๋จ
  3. ๋ชจ๋“  ์—…๋ฌด > ์˜ค๋ผํด ์—ฐ๋™(MyBatis)

  1. ๋„์„œ ๊ฒ€์ƒ‰ํ•˜๊ธฐ > ์—˜๋ผ์Šคํ‹ฑ์„œ์น˜๋กœ ๊ตฌํ˜„
  • ๋ชฉ๋ก๋ณด๊ธฐ > ์˜ค๋ผํด
  • ์ƒ์„ธ๋ณด๊ธฐ > ์˜ค๋ผํด
  • ์ถ”๊ฐ€ํ•˜๊ธฐ > ์˜ค๋ผํด, ์—˜๋ผ์Šคํ‹ฑ์„œ์น˜
  • ๊ฒ€์ƒ‰ํ•˜๊ธฐ > ์—˜๋ผ์Šคํ‹ฑ์„œ์น˜
  • ์‚ญ์ œํ•˜๊ธฐ > ์˜ค๋ผํด, ์—˜๋ผ์Šคํ‹ฑ์„œ์น˜
  • ์ˆ˜์ •ํ•˜๊ธฐ > ์˜ค๋ผํด, ์—˜๋ผ์Šคํ‹ฑ์„œ์น˜

ํ”„๋กœ์ ํŠธ ์„ค์ •

์ผ๊ด„์„ค์ • ๊ฐ€์ ธ์™€์„œ ๋ณต๋ถ™

  1. pom.xml
  2. web.xml
  3. root-context.xml
  4. mapper
  • src/main/resources > "com" > "test" > "mapper" > "ProjectMapper.xml"
  1. log4jdbc.log4j2.properties
  • src/main/resources > log4jdbc.log4j2.properties
  1. log4j.xml

ํŒจํ‚ค์ง€ ์ถ”๊ฐ€ servlet-context.xml

  • "com.test.controller"
  • "com.test.persistence"
  • "com.test.domain"
  • "com.test.mapper"

ํŒŒ์ผ ์ถ”๊ฐ€

  • com.test.controller
    - "BookController.java"

  • com.test.persistence
    - "BookDAO.java"(I) > ์˜ค๋ผํด ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
    - "BookDAOImpl.java"(C)

    • "BookRepository.java"(I) > ์—˜๋ผ์Šคํ‹ฑ์„œ์น˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
    • "BookRepositoryImpl.java"(C)
  • com.test.domain
    - "BookDTO.java"

  • com.test.mapper
    - "ProjectMapper.java"(I)

  • views
    - "list.jsp"

    • "add.jsp"
    • "view.jsp"
  • ElasticsearchProject > "script.sql"

profile
์ฐจ๊ทผ์ฐจ๊ทผ ํ•˜๋‚˜์”ฉ

0๊ฐœ์˜ ๋Œ“๊ธ€