Browse Source

[MQ][修改]:rocketmq demo

release_toaliyun_real
Stormeye Wu 7 years ago
parent
commit
bf4bae1107
12 changed files with 144 additions and 1 deletions
  1. +2
    -0
      mallinkAdmin/src/main/java/com/iformall/UserApplication.java
  2. +9
    -0
      mallinkAdmin/src/main/resources/application.yml
  3. +2
    -0
      mallinkBApi/src/main/java/com/iformall/BApplication.java
  4. +9
    -0
      mallinkBApi/src/main/resources/application.yml
  5. +2
    -0
      mallinkCApi/src/main/java/com/iformall/CApplication.java
  6. +9
    -0
      mallinkCApi/src/main/resources/application.yml
  7. +2
    -0
      mallinkSchedule/src/main/java/com/iformall/ScheduleApplication.java
  8. +11
    -0
      mallinkSchedule/src/main/java/com/iformall/controller/HomeController.java
  9. +9
    -1
      mallinkSchedule/src/main/resources/application.yml
  10. +60
    -0
      mallinkService/src/main/java/com/iformall/mq/MessageProducer.java
  11. +17
    -0
      mallinkService/src/main/java/com/iformall/mq/MqConsumer.java
  12. +12
    -0
      pom.xml

+ 2
- 0
mallinkAdmin/src/main/java/com/iformall/UserApplication.java View File

@@ -1,6 +1,7 @@
package com.iformall; package com.iformall;


import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties; import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.rocketmq.starter.annotation.EnableRocketMQ;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -18,6 +19,7 @@ import tk.mybatis.spring.annotation.MapperScan;
@EnableSwagger2 @EnableSwagger2
@EnableEncryptableProperties @EnableEncryptableProperties
@EnableAsync @EnableAsync
@EnableRocketMQ
public class UserApplication { public class UserApplication {


@Value("${fm.exception}") @Value("${fm.exception}")


+ 9
- 0
mallinkAdmin/src/main/resources/application.yml View File

@@ -21,6 +21,15 @@ spring:
cache-names: redis_cache #缓存的名字(可以不指定) cache-names: redis_cache #缓存的名字(可以不指定)
redis: redis:
time-to-live: 60000ms #很重要,缓存的有效时间,以便缓存的过期(单位为毫秒) time-to-live: 60000ms #很重要,缓存的有效时间,以便缓存的过期(单位为毫秒)
rocketmq:
nameServer: 127.0.0.1:9876
producer:
retry-times-when-send-async-failed: 0
send-msg-timeout: 300000
compress-msg-body-over-howmuch: 4096
max-message-size: 4194304
retry-another-broker-when-not-store-ok: false
retry-times-when-send-failed: 2


# @{link} https://github.com/abel533 # @{link} https://github.com/abel533
#Mybatis #Mybatis


+ 2
- 0
mallinkBApi/src/main/java/com/iformall/BApplication.java View File

@@ -1,6 +1,7 @@
package com.iformall; package com.iformall;


import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties; import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.rocketmq.starter.annotation.EnableRocketMQ;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -14,6 +15,7 @@ import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication @SpringBootApplication
@MapperScan(basePackages = {"com.iformall.mapper"}) @MapperScan(basePackages = {"com.iformall.mapper"})
@EnableEncryptableProperties @EnableEncryptableProperties
@EnableRocketMQ
public class BApplication { public class BApplication {


@Value("${fm.exception}") @Value("${fm.exception}")


+ 9
- 0
mallinkBApi/src/main/resources/application.yml View File

@@ -12,6 +12,15 @@ spring:
date-format: yyyy-MM-dd HH:mm:ss date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8 time-zone: GMT+8
default-property-inclusion: non_null default-property-inclusion: non_null
rocketmq:
nameServer: 127.0.0.1:9876
producer:
retry-times-when-send-async-failed: 0
send-msg-timeout: 300000
compress-msg-body-over-howmuch: 4096
max-message-size: 4194304
retry-another-broker-when-not-store-ok: false
retry-times-when-send-failed: 2


# @{link} https://github.com/abel533 # @{link} https://github.com/abel533
#Mybatis #Mybatis


+ 2
- 0
mallinkCApi/src/main/java/com/iformall/CApplication.java View File

@@ -1,6 +1,7 @@
package com.iformall; package com.iformall;


import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties; import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.rocketmq.starter.annotation.EnableRocketMQ;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -14,6 +15,7 @@ import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication @SpringBootApplication
@MapperScan(basePackages = {"com.iformall.mapper"}) @MapperScan(basePackages = {"com.iformall.mapper"})
@EnableEncryptableProperties @EnableEncryptableProperties
@EnableRocketMQ
public class CApplication { public class CApplication {


@Value("${fm.exception}") @Value("${fm.exception}")


+ 9
- 0
mallinkCApi/src/main/resources/application.yml View File

@@ -12,6 +12,15 @@ spring:
date-format: yyyy-MM-dd HH:mm:ss date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8 time-zone: GMT+8
default-property-inclusion: non_null default-property-inclusion: non_null
rocketmq:
nameServer: 127.0.0.1:9876
producer:
retry-times-when-send-async-failed: 0
send-msg-timeout: 300000
compress-msg-body-over-howmuch: 4096
max-message-size: 4194304
retry-another-broker-when-not-store-ok: false
retry-times-when-send-failed: 2


# @{link} https://github.com/abel533 # @{link} https://github.com/abel533
#Mybatis #Mybatis


+ 2
- 0
mallinkSchedule/src/main/java/com/iformall/ScheduleApplication.java View File

@@ -1,6 +1,7 @@
package com.iformall; package com.iformall;


import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties; import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.rocketmq.starter.annotation.EnableRocketMQ;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -15,6 +16,7 @@ import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication @SpringBootApplication
@MapperScan(basePackages = {"com.iformall.mapper"}) @MapperScan(basePackages = {"com.iformall.mapper"})
@EnableSwagger2 @EnableSwagger2
@EnableRocketMQ
@EnableEncryptableProperties @EnableEncryptableProperties
public class ScheduleApplication { public class ScheduleApplication {




+ 11
- 0
mallinkSchedule/src/main/java/com/iformall/controller/HomeController.java View File

@@ -1,10 +1,12 @@
package com.iformall.controller; package com.iformall.controller;


import com.iformall.common.ResultData; import com.iformall.common.ResultData;
import com.iformall.mq.MessageProducer;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@@ -18,10 +20,19 @@ public class HomeController {
@Value("${version}") @Value("${version}")
private String version; private String version;


@Autowired
private MessageProducer messageProducer;



@ApiOperation("获取后端版本号") @ApiOperation("获取后端版本号")
@GetMapping("/version") @GetMapping("/version")
public ResultData version() { public ResultData version() {
return new ResultData(version); return new ResultData(version);
} }

@GetMapping("/mqtest")
public ResultData mqtest() {
messageProducer.sendMessage("mq test 1", "topic-1", "tag-1", "key-1");
return new ResultData();
}
} }

+ 9
- 1
mallinkSchedule/src/main/resources/application.yml View File

@@ -21,7 +21,15 @@ spring:
cache-names: redis_cache #缓存的名字(可以不指定) cache-names: redis_cache #缓存的名字(可以不指定)
redis: redis:
time-to-live: 60000ms #很重要,缓存的有效时间,以便缓存的过期(单位为毫秒) time-to-live: 60000ms #很重要,缓存的有效时间,以便缓存的过期(单位为毫秒)

rocketmq:
nameServer: 127.0.0.1:9876
producer:
retry-times-when-send-async-failed: 0
send-msg-timeout: 300000
compress-msg-body-over-howmuch: 4096
max-message-size: 4194304
retry-another-broker-when-not-store-ok: false
retry-times-when-send-failed: 2




# @{link} https://github.com/abel533 # @{link} https://github.com/abel533


+ 60
- 0
mallinkService/src/main/java/com/iformall/mq/MessageProducer.java View File

@@ -0,0 +1,60 @@
package com.iformall.mq;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
* @Auther: Stormeye
* @Date: 2019-01-31
* @Description: RocketMQ消息生产者
*/
@Service
@Slf4j
public class MessageProducer {
@Value("${spring.rocketmq.nameServer}")
private String namesrvAddr;

private static final DefaultMQProducer producer = new DefaultMQProducer("DefaultProducer");

@PostConstruct
public void start(){
try {
producer.setNamesrvAddr(namesrvAddr);
producer.start();
log.info("Message Producer Start...");
}catch (Exception e){
log.error("Message Producer Start Error!!",e);
}
}

public void sendMessage(String data, String topic, String tags, String keys) {
try {
byte[] messageBody = data.getBytes(RemotingHelper.DEFAULT_CHARSET);

Message mqMsg = new Message(topic, tags, keys, messageBody);

producer.send(mqMsg, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
log.info("Message Producer: Send Message Success {}", sendResult);
}

@Override
public void onException(Throwable throwable) {
log.error("Message Producer: Send Message Error ", throwable);
}
});
} catch (Exception e) {
log.error("Message Producer: Send Message Error ", e);
}

}
}

+ 17
- 0
mallinkService/src/main/java/com/iformall/mq/MqConsumer.java View File

@@ -0,0 +1,17 @@
package com.iformall.mq;

import lombok.extern.slf4j.Slf4j;
import org.rocketmq.starter.annotation.RocketMQListener;
import org.rocketmq.starter.annotation.RocketMQMessage;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RocketMQListener(topic = "topic-1")
public class MqConsumer {

@RocketMQMessage(messageClass = String.class, tag = "tag-1")
public void onMessage(String message) {
log.info("received message: {}", message);
}
}

+ 12
- 0
pom.xml View File

@@ -118,6 +118,18 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>


<!-- rocketmq -->
<dependency>
<groupId>org.rocketmq.spring.boot</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.4.0</version>
</dependency>

<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>


Loading…
Cancel
Save