Caij nplooj ntoos hlav Tutorial – Messaging nrog Redis siv lub caij nplooj ntoos hlav

Messaging with Redis and Spring framework

Caij nplooj ntoos hlav Tutorial – Messaging nrog Redis siv lub caij nplooj ntoos hlav

Txheej txheem cej luam

Redis yog ib tug datastor uas pub koj siv ntau yam kev siv ntau zaus – messaging thov yog ib tug ntawm lawv. Thaum koj tsim ib daim ntawv thov kev pab uas muaj Redis, Koj yuav leveraging tus menyuam ntxaib redis thiab lub caij nplooj ntoos hlav Spring Framework nqa. Redis muaj kev high speed haujlwm, Kev them nyiaj yug rau ntau hom ntaub ntawv thiab atomic ntaub ntawv thaum lub caij nplooj ntoos hlav Framework yog raws li tus txheej txheem ntawm portability, productivity thiab li qub tas li. Lub cwj pwm ntawm Lub caij nplooj ntoos hlav Framework thiab Redis hauv lub tsev ib messaging daim ntawv thov yog hais meej messaged. Thaum Redis ua tus datastore uas tsim nyog rau lub tsev messaging, tus Caij nplooj ntoos hlav moj khaum pub koj mus saib tau lub Redis API programmatically tsis ntau dhau lawm. Yuav tsis muaj kev kawm tshiab los yog boilerplate chaws.








Redis yog dab tsi?

Tej Thaj Chaw Deb Haus Dej, nrov hu ua Redis, yog ib tug datastore uas yuav sau tau rau disk rau durability. Nws koomtxoos raws li ib tug neeg-nyiaj qauv pab. Redis muaj ob txoj kev mus rau persist cov ntaub ntawv. Twg txoj kev uas nws tus cwjpwm cov ntaub ntawv haujlwm, ceev heev. Nws txhawb ntau ntau cov ntaub ntawv thiab pub ntau tus neeg tau kev pab rau tib lub sijhawm.

Redis muaj ob txoj kev mus rau persist cov ntaub ntawv: RDB thiab AOF. Lub tsev khaws puav pheej, tab sis yog sai heev, kuj tsis txhim khu kev qha tshaj AOF. Cov RDB captures cov ntaub ntawv snapshots nyob specified intervals. TUS AFO, rau lwm cov tes, Teev cov ntaub ntawv haujlwm thiab executes cov ntaub ntawv haujlwm txhua lub sij hawm cov kev soj ntsuam thiab reconstructs cov ntaub ntawv thawj txheej. Thaum twg koj query Redis, Nws ib txwm teb los ntawm kev nco tau yooj yim.

Redis raws li tus neeg tau txais kev pab cov qauv pab thiab thiaj li, nws mloog ib TCP chaw nres nkoj thiab txais commands. Vim Redis commands yog atomic, Koj yuav ua tau haujlwm rau tib yam los ntawm ntau tus neeg tau ntau tus neeg.

Redis cov ntaub ntawv qauv zoo li cov ntaub ntawv txaus siab txaus siab los ntawm cov lus programming uas yog suitable rau cov software developers. Li ntawd mas, nws txawv ntawm tej hom dawb NoSQL Tus nqi tseem ceeb-tus nqi datastore los yog ib tug relational database tswj lawv (RDBMS). Redis txhawb tau cov ntaub ntawv nram qab no:

  • txoj hlua
  • Teev npe
  • Poob lawm
  • Teev tseg
  • Hashes

Qhov zoo dua yog cov zoo dua yog dab tsi?

Lub ntsiab kom zoo dua ntawm Redis yog piav hauv qab no:

Exceptionally ceev haujlwm

Redis yuav teeb mind-boggling speeds rau cov ntaub ntawv haujlwm. Nws yuav ua tau txog 11000 SETS ib pliag thiab 81000 GETs ib pliag. Koj kuj teev tau cov benchmarks rau cov kev pab kho mob uas siv cov redis-benchmark dej. Tus redis-benchmark simulates tag nrho cov SETs/GETs tau los ntawm tej neeg tau thaum nws xa tag nrho cov queries.








Nplua nuj cov ntaub ntawv txhawb

Software developers hlub Redis vim nws txhawb cov ntaub ntawv types lawv xav tau rau lawv lub chaw ua hauj lwm hnub. Redis ua rau nws yooj yim thiab yooj yim rau software developers kom daws tau ntau yam teeb meem vim ntau yam ntawm cov ntaub ntawv nws txhawb.

Atomic haujlwm

Tag nrho redis haujlwm yog atomic. Qhov no txhais tau tias yog ntau tshaj ib tus neeg tau kev saibxyuas Redis concurrently, Lawv tag nrho lawv tau txais qhov tseem ceeb tib yam tshiab.

Ntau yam cuab yeej siv

Vim Redis yog ib tug cuab yeej multiutility, Nws pub koj teb ntau yam kev siv xws li messaging-queues, caching, web zaug thiab web sab ntaus suav thiab ntau.

Lub caij nplooj ntoos hlav Redis?

Redis, li tej zaum twb yuav tseeb, Yog ib tug nrov datastore thiab ntau lub caij nplooj ntoos hlav-raws li kev siv nws. Qab tej daim ntaub ntawv yog lub caij nplooj ntoos hlav ntoos hlav qhib peb tes num uas ua rau nws yooj yim kom lawv muaj cov ntaub ntawv. Lub caij nplooj ntoos hlav Redis yog ib txoj kev rau developers mus saib redis programmatically thiab mus txog lawv txoj hauj lwm. Ntawm cov software developers txiv foupective, Yog tsis muaj mus nqis peev pab rau lub sij hawm kawm tshiab txawj vim hais tias cov qub txhooj ntawm lub tiaj nras Java Object (POJ NIAM) xws li productivity, li qub tas li, thiab chaw ntiav pw, promoted los ntawm lub caij nplooj ntoos hlav Framework, ncua ntxiv rau lub caij nplooj ntoos hlav Redis. Developers tsis yuav ua hauj lwm nrog boilerplate chaws yuav tsum tau interacting nrog Redis. Lawv yuav yooj yim ua hauj lwm nrog Redis-tus nqi datastores tsis yuav tsum tau ua hauj lwm nrog cov qib uas qis APIs uas Redis muaj. Nrog Redis, Lub caij nplooj ntoos hlav Redis muaj cov hoob generified hoob kawm hu ua RedisTemplate, uas yog ib yam li cov JDBC Template los yog Hibernate Template. Ua tus tsim tawm, Koj muab tau cov ntaub ntawv rau redisTemplate. Kom ua tau raws li cov kev sib cuam tshuam nrog Redis, Koj yuav tsum tau mus saib rau redisTemplate class.

Coj daim ntawv & Configuration

Cov txheej txheem no yuav pab kom koj tsim cov ntaub ntawv siv Cov Ntaub Ntawv Redis. Nws yuav qha tau cov messaging capabilities ntawm Redis. Peb yuav ua raws li cov kauj ruam hauv qab no kom mus dhia.

Ib cheeb tsam chaw nres nkoj:

Peb yuav tsum muaj tej yam uas npaj kom tiav daim ntawv thov.

Components kom lawv muaj:

Maven tsim tsab ntawv yog qhov 1me nyuam tivthaiv. Lwm cov cuab yeej uas tsim nyog siv tau los ua daim ntawv thov kev pab no. Tab sis npog, Ntawm no peb yuav mloog zoo rau maven tsim kom muaj ntaub ntawv (pas dej ua .xml).

Qhia 1: Qauv xauj tsev.xml

[chaws]

<?xml version =”1.0″ encoding =”UTF-8″?>

<picture of Kathy xmlns =”http://maven.apache.org/POM/4.0.0″ xmlns:xsi =”http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation =”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>

<artifactId>ex-messaging-redis</artifactId>

<Version>0.1.0</Version>

<niam txiv>

<groupId>org.springframework.boot</groupId>

<artifactId>caij nplooj ntoos hlav-starter-starter</artifactId>

<Version>1.5.9.TSO TAWM</Version>

</niam txiv>

<khoom>

<java.Version>1.8</java.Version>

</khoom>

<dependencies>

<quav dej caws>

<groupId>org.springframework.boot</groupId>

<artifactId>khau raj-oob khaub-starter</artifactId>

</quav dej caws>

<quav dej caws>

<groupId>org.springframework.boot</groupId>

<artifactId>caij nplooj ntoos hlav-starter-starter-ntaub ntawv redis</artifactId>

</quav dej caws>

<quav dej caws>

<groupId>org.springframework.boot</groupId>

<artifactId>caij nplooj ntoos hlav-devotols</artifactId>

<kev xaiv>tseeb</kev xaiv>

</quav dej caws>

</dependencies>

<lawv muaj>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>khau raj-maven-plugin</artifactId>

</plugin>

</plugins>

</lawv muaj>

<repositories>

<ntxeev siab>

<daim>caij nplooj ntoos hlav</daim>

<lub npe>Caij nplooj ntoos hlav</lub npe>

<url>https://repo.spring.io/libs-release</url>

</ntxeev siab>

</repositories>

</yog ib qhov>

[/chaws]

Tam sim no, Peb yuav tsum tau pib cov Redis pab. Redis yog ib qhov qhib tseem ceeb-tus nqi cov ntaub ntawv khw. Nws tseem muaj ib lub cev messaging. Koj yuav tsum tau download thiab unpack nws. Ces pib tus pab raws li cov commands.

Qhia 2: Tsis them nyiaj thiab start Redis pab

[chaws]

$ hlau nplaum://download.redis.io/releases/redis-4.0.7.tar.gz

$ tar xzf redis-4.0.7.tar.gz

$ cd redis-4.0.7

$ Ua [/chaws]

[chaws]

$ scr/redis-server

[/chaws]

Tom qab qhov no peb yuav tsum tsim tau ib tug receiver rau lub messaging lawv. Txhua yam messaging lawv yuav tsum muaj tsawg kawg yog ib tug publisher thiab ib receiver.

Qhia 3: Qauv lus receiver

[chaws]

pob.com.techalpine.redis;

chaw nres nkoj java.concurrent.CountDownLatch;

import org.slf4j.slf4j.Logger;

import org.slf4j.LoggerFactory;

import.springframework.taum... annotation.annotation.Autowired;

Pej xeem hoob kawm MsGReceiver {

Qhov tseeb static kawg Logger = LoggerFactory.getlogger(Receiver.class);

ntiav CountDownLatch msglatch;

@Autowired

Pej xeem Receiver(CountDownLatch msglatch) {

no.msglatch = msglatch = msglatch;

}

pej xeem yuav tsum tau txais(Txoj hlua msg) {

LOGGER.info(“Tau Txais xov xwm <” + msg + “>”);

msglatch.countDown();

}

} [/chaws]

Tam sim no, Tom qab daim ntawv thov yuav tsum khiav daim ntawv thov. No qauv muaj lub ntsiab lus tseem ceeb components sau qhov chaws.

Qhia 4: Daim ntawv thov kev pab

[chaws]

pej xeem tsis muaj dabtsis loj zoo li qub(Txoj hlua[] args) Pov tseg InterruptedException {

Application Application redisctx = SpringApplication.khiav(Redisaplication.class, args);

StringRedisTemplate redistemplate = redisctx.getBean(StringTemplate.class);

CountdownLatch redislatch = redisctx.getBean(CountdownLatch.class);

LOGGER.info(“Xa Redis lus…”);

redistlate.convertAndSend(“Nyob zoo”, “Kuv yog los ntawm Redis”);

redislatch.await();

System.Exit(0);

}

[/chaws]

Koj yuav khiav daim ntawv thov los ntawm kev hais kom ua tom qab lub tsev siv Maven.








Xaus:

Muab txoj kev loj hlob ntawm txoj kev loj hlob software, Redis muab raws nraim li cas. Thaum lub sij hawm thaum Agile software kev loj hlob yog gaining prominence, cov ntaub ntawv ceev ceev, chaw ntiav pw, li qub tas li, tsim tawm productivity thiab ntxhee software yob-outs yog ib qho tseem ceeb. Caij nplooj ntoos hlav Redis fulfils tej zov. tej zaum, Tsim kom muaj productivity thiab txhawb rau cov neeg mob sib txawv yog nws cov highlights. Koom haum tsis xav siv ntau lub sij hawm rau developers kev kawm tshiab, yog vim li cas, Lub caij nplooj ntoos hlav Redis pub rau cov developers pib tsis xav paub Redis hauv-depth. Nws txhawb thiab software software kev loj hlob. Redis thiab caij nplooj ntoos hlav Framework constitutes ib haib.

 

============================================= ============================================== Yuav zoo TechAlpine phau ntawv rau Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Txaus siab rau qhov blog? Tshaj tawm lus thov :)

Follow by Email
LinkedIn
LinkedIn
Share