spring boot rabbitMQ 的 hello world

  今天公司有一個需求,是實現多個服務器中的運維信息的集中管理。因爲須要實現運維信息的收集不影響各服務器上服務的開銷,而且能快速開發,因此選擇了消息隊列這種技術方式。 消息隊列有一個好處,是能夠將消息異步傳遞,不對主服務形成開銷,運維信息,是能夠異步的在運維服務器中處理,並不影響到主服務。 如今java中,使用spring boot開發,方便高效,因此,選擇了spring boot支持的rabbit MQ。java

       搞開發,學技術,最好的方式是從最簡單的例子出發,就是常說的hello world,因此有了如下實現最簡單例子的筆記.spring

 一,安裝 rabbitMQ 服務windows

  因爲rabbitMQ須要Erlang的慮擬機,因此須要先安裝Erlang,安裝完Erlang,再安裝rabbitMQ 服務,兩個服務都是安裝在windows server 2106上面服務器

二,開始進行編碼app

  1. 打開start.spring.io, 新建一個hello world 項目

      

  2. 將項目導入 Eclipse,如下是代碼結構運維

  

  3. RabbitAmqpTutorialsApplication.java異步

package org.springframework.amqp; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Profile; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.amqp.tutorials.RabbitAmqpTutorialsRunner; @SpringBootApplication @EnableScheduling public class RabbitAmqpTutorialsApplication { @Profile("usage_message") @Bean public CommandLineRunner usage() { return args -> { System.out.println("This app uses Spring Profiles to control its behavior.\n"); System.out.println("Sample usage: java -jar rabbit-tutorials.jar --spring.profiles.active=hello-world,sender"); }; } @Profile("!usage_message") @Bean public CommandLineRunner tutorial() { return new RabbitAmqpTutorialsRunner(); } public static void main(String[] args) throws Exception { SpringApplication.run(RabbitAmqpTutorialsApplication.class, args); } }

4. RabbitAmqpTutorialsRunner.javaide

package org.springframework.amqp.tutorials; import java.util.Scanner; import org.springframework.amqp.core.Queue; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; import org.springframework.context.ConfigurableApplicationContext; public class RabbitAmqpTutorialsRunner implements CommandLineRunner { @Value("${tutorial.client.duration:0}") private int duration; @Autowired private ConfigurableApplicationContext ctx; @Autowired private RabbitTemplate template; @Autowired private Queue queue; @Override public void run(String... arg0) throws Exception { Scanner scan = new Scanner(System.in); while(true) { // System.out.println("Ready ... running for " + duration + "ms"); String threadId = String.valueOf(Thread.currentThread().getId()); System.out.println("Thead["+threadId+"] is running"); //Thread.sleep(duration);  String cmd = scan.nextLine(); if(cmd.equals("exit")) { scan.close(); break; }else { this.template.convertAndSend(queue.getName(), cmd); System.out.println(" [x] Sent '" + cmd + "'"); } } ctx.close(); } }

5. Tut1Config.javathis

package org.springframework.amqp.tutorials.tut1; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Profile({"tut1","hello-world"}) @Configuration public class Tut1Config { @Bean public Queue hello() { System.out.println("hello queue created"); return new Queue("hello"); } @Profile("receiver") @Bean public Tut1Receiver receiver() { System.out.println("receiver created"); return new Tut1Receiver(); } /** @Profile("sender") @Bean public Tut1Sender sender() { System.out.println("sender created"); return new Tut1Sender(); }**/ }

6.Tut1Receiver編碼

package org.springframework.amqp.tutorials.tut1; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; @RabbitListener(queues = "hello") public class Tut1Receiver { @RabbitHandler public void receive(String in) { String threadId = String.valueOf(Thread.currentThread().getId()); System.out.println("Thead["+threadId+"] is running"); System.out.println(" [x] Received '" + in + "'"); } }

7.Tut1Sender.java

// Sender package org.springframework.amqp.tutorials.tut1; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.amqp.core.Queue; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; public class Tut1Sender { @Autowired private RabbitTemplate template; @Autowired private Queue queue; @Scheduled(fixedDelay = 1000, initialDelay = 500) public void send() { Date now = new Date(); SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String message = f.format(now); this.template.convertAndSend(queue.getName(), message); System.out.println(" [x] Sent '" + message + "'"); String threadId = String.valueOf(Thread.currentThread().getId()); System.out.println("Thead["+threadId+"] is running"); } }

 

8. application.peroperties

 

spring.profiles.active = usage_message logging.level.org = ERROR tutorial.client.duration = 1000 spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.username=test spring.rabbitmq.password=test1234
相關文章
相關標籤/搜索