ActiveMQ Zookeeper-based master-slave (LevelDB master/slave) construction and Spring-boot use

Source: Internet
Author: User
Tags failover message queue zookeeper amq

0: Description

ActiveMQ 5.9.0 The new master-slave implementation, based on zookeeper to elect a master, the other nodes automatically as slave real-time synchronization messages. Because there is real-time synchronization of data slave, Master does not have to worry about data loss, so LEVELDB will take priority to memory storage messages, asynchronous synchronization to disk, so this way of ACTIVEMQ read and write performance is best because the election mechanism to more than half, so the minimum required 3 nodes, To achieve high availability. If the cluster is two, the master fails after the slave does not work, so the cluster is at least three. This method only realizes the primary and standby function, avoids the single point of failure, does not have the load balancing function.

1: Environment Preparation

Ip
192.168.3.10 Server1
192.168.3.11 Server2
192.168.3.12 Server3

Install software information:

Apache-activemq-5.13.0-bin.tar.gz

Zookeeper-3.5.2-alpha.tar.gz

Zooinspector.zip

2: Build Zookeeper Cluster

(1) Extract the zookeeper-3.5.2-alpha.tar.gz files to the/HOME/WZH/ZK directory;

(2) Copy zoo_sample.cfg to zoo.cfg and modify its configuration information

[Email protected]:~/zk/zookeeper-3.5.2-alpha/conf$ cp zoo_sample.cfg ZOO.CFG

[Email Protected]:~/zk/zookeeper-3.5.2-alpha/conf$vim zoo.cfg

Ticktime= -Initlimit=TenSynclimit=5DataDir=/tmp/Zookeeperclientport=2181server.1=192.168.3.10:2888:3888server.2=192.168.3.11:2888:3888server.3=192.168.3.11:2888:3888

(3) Create/tmp/zookeeper directory

In this directory, create a file named myID with the content 1 (this value changes with the server)

(4) Copy the/home/wzh/zk/zookeeper-3.5.2-alpha folder on Server1 to Server2,server3 and create the/tmp/zookeeper directory

Under this directory, create a file named myID with the content 2

(5) Start zookeeper

[192.168.3.10]

[Email protected]:~/zk/zookeeper-3.5. 2-alpha/bin$./zkserver. SH  /home/wzh/zk/zookeeper-3.5. 2-alpha/bin/. /conf/zoo.cfgstarting Zookeeper ... STARTED

[192.168.3.11]

[Email protected]:~/zk/zookeeper-3.5. 2-alpha/bin$./zkserver. SH  /home/wzh/zk/zookeeper-3.5. 2-alpha/bin/. /conf/zoo.cfgstarting Zookeeper ... STARTED

[192.168.3.12]

[Email protected]:~/zk/zookeeper-3.5. 2-alpha/bin$./zkserver. SH  /home/wzh/zk/zookeeper-3.5. 2-alpha/bin/. /conf/zoo.cfgstarting Zookeeper ... STARTED

3: Build ACTIVEMQ Cluster

(1) Extract apache-activemq-5.13.0-bin.tar.gz to/HOME/WZH/AMQ

(2) Modify the Activemq.xml configuration file

"1" Sets the brokername of the broker node to WZHAMQ

<xmlns= "Http://activemq.apache.org/schema/core"  brokername= " Wzhamq "  dataDirectory=" ${activemq.data} ">

"2" persistenceadapter the persistence of replicatedleveldb, the kahadb way to comment out

 <Persistenceadapter>         <!--<kahadb directory= "${activemq.data}/kahadb"/> -        <ReplicatedleveldbDirectory= "${activemq.data}/leveldb"Replicas= "3"Bind= "tcp://0.0.0.0:0"zkaddress= "192.168.3.10:2181,192.168.3.11:2181"hostname= "192.168.3.10"Sync= "Local_disk"Zkpath= "/activemq/leveldb-stores"/>        </Persistenceadapter>

Copy the apache-activemq-5.13. To 11, 12 machines

[Email protected]:~/amq$ scp-r apache-activemq-5.13.0/ [email protected]:/tmp

Modify the Hostname= "192.168.3.11" in the configuration file

Modify the Hostname= "192.168.3.12" in the configuration file

(3) Start ACTIVEMQ

[Email protected]:~/amq$./apache-activemq-5.13. 0/bin/'/home/wzh/amq/apache-activemq-5.13.0//bin/env'  ' /opt/java/jdk1.8.0_91/bin/java '  '2031') [email protected]-master:~/amq$

Start the 192.168.3.11,192.168.3.12 machine in turn

4: Cluster Management

(1) Viewing zookeeper clusters by using the Zooinspector tool

(2) http://192.168.3.10:8161/admin/Default user name and password for admin login activemq management side

5: Activemq JMS via Spring-boot operation

(1) Build Spring-boot application through Gradle, add in Gradle file

Dependencies {    compile ('org.springframework.boot:spring-boot-starter-activemq ' )    compile ('org.springframework.boot:spring-boot-starter-web')    testcompile ('org.springframework.boot:spring-boot-starter-test' )}

(2) Add the following configuration in application

Spring.activemq.broker-url=failover: (tcp://192.168.3.10:61616,tcp://192.168.3.11:61616,tcp:// 192.168.3.12:61616) spring.activemq. in-memory=truespring.activemq.pool.enabled=Falsespring.activemq.user=  Adminspring.activemq.password=admin

(3) JMS message sending

@Service  Public class Producer {    @Autowired    private  jmsmessagingtemplate jmstemplate;      Public void Final String message) {        jmstemplate.convertandsend (destination, message);}    }

(4) JMS Message Reception

@Component  Public class Consumer {    = "Test.queue")    publicvoid  receivequeue (String text) {        System.out.println ("Consumer received the message:" +text);}    }

(5) test

@RestController @requestmapping (value= "/test", Headers= "Accept=application/json", produces= "Application/json;charset=utf-8") Public classTestctrl {@Autowired Producer Producer; Destination Destination=NewActivemqqueue ("Test.queue"); @RequestMapping (Value= "/say/{msg}/to/{name}", Method=requestmethod.get) PublicMap<string, object>Say (@PathVariable string msg, @PathVariable string name) {Map<string, object> map =NewHashmap<>(); Map.put ("MSG", MSG); Map.put ("Name", name);        Producer.sendmessage (destination, msg); returnmap; }}

(6) Enter the ACTIVEMQ management console to create a message queue

Test.queue

(7) Test by postman

2017-08-03 08:10:44.928 INFO 12820---[ActiveMQ Task-3] o.a.a.t.failover.failovertransport:successfully reconnected to tcp://192.168.3.10:61616
2017-08-03 08:11:08.854 INFO 12820---[ActiveMQ Task-1] o.a.a.t.failover.failovertransport:successfully connected to TC p://192.168.3.10:61616
The message received by consumer is: Hello
2017-08-03 08:43:39.464 INFO 12820---[ActiveMQ Task-1] o.a.a.t.failover.failovertransport:successfully connected to TC p://192.168.3.10:61616
The message received by consumer is: Hello

(8) The current system is connected to 10, if the 10 cluster down, the system will understand the choice of a slave as master to provide services, so as to initiate the case of the main preparation of the effect.



ActiveMQ Zookeeper-based master-slave (LevelDB master/slave) construction and Spring-boot use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.