SpringBoot 使用RocketMQ踩坑经历

一、使用JDK11需要修改配置

runbroker.cmd


if not exist "%JAVA_HOME%\bin\java.exe" echo Please set the JAVA_HOME variable in your environment, We need java(x64)! & EXIT /B 1
set "JAVA=%JAVA_HOME%\bin\java.exe"

setlocal

set BASE_DIR=%~dp0
set BASE_DIR=%BASE_DIR:~0,-1%
for %%d in (%BASE_DIR%) do set BASE_DIR=%%~dpd

set CLASSPATH=%BASE_DIR%/lib/rocketmq-broker-4.9.2.jar;%BASE_DIR%/lib/*;%BASE_DIR%/conf;%CLASSPATH%

rem ===========================================================================================
rem  JVM Configuration
rem ===========================================================================================
set "JAVA_OPT=%JAVA_OPT% -server -Xms2g -Xmx2g"
set "JAVA_OPT=%JAVA_OPT% -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:SurvivorRatio=8"
set "JAVA_OPT=%JAVA_OPT% -XX:-OmitStackTraceInFastThrow"
set "JAVA_OPT=%JAVA_OPT% -XX:+AlwaysPreTouch"
set "JAVA_OPT=%JAVA_OPT% -XX:MaxDirectMemorySize=15g"
set "JAVA_OPT=%JAVA_OPT% -XX:-UseLargePages -XX:-UseBiasedLocking"
set "JAVA_OPT=%JAVA_OPT% -cp %CLASSPATH%"

"%JAVA%" %JAVA_OPT% %*

2、runserver.cmd,主要修改CLASSPATH

if not exist "%JAVA_HOME%\bin\java.exe" echo Please set the JAVA_HOME variable in your environment, We need java(x64)! & EXIT /B 1
set "JAVA=%JAVA_HOME%\bin\java.exe"

setlocal

set BASE_DIR=%~dp0
set BASE_DIR=%BASE_DIR:~0,-1%
for %%d in (%BASE_DIR%) do set BASE_DIR=%%~dpd

set CLASSPATH=.;%BASE_DIR%/lib/rocketmq-broker-4.9.2.jar;%BASE_DIR%/lib/*;%BASE_DIR%/conf;%CLASSPATH%

set "JAVA_OPT=%JAVA_OPT% -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
set "JAVA_OPT=%JAVA_OPT% -XX:CMSInitiatingOccupancyFraction=70 -XX:+CMSParallelRemarkEnabled -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+CMSClassUnloadingEnabled -XX:SurvivorRatio=8"
set "JAVA_OPT=%JAVA_OPT% -XX:-OmitStackTraceInFastThrow"
set "JAVA_OPT=%JAVA_OPT% -XX:-UseLargePages"
set "JAVA_OPT=%JAVA_OPT% -cp "%CLASSPATH%""
echo "%JAVA%" %JAVA_OPT% %*
"%JAVA%" %JAVA_OPT% %*

3、tool.cmd


if not exist "%JAVA_HOME%\bin\java.exe" echo Please set the JAVA_HOME variable in your environment, We need java(x64)! & EXIT /B 1

set "JAVA=%JAVA_HOME%\bin\java.exe"

setlocal
set BASE_DIR=%~dp0
set BASE_DIR=%BASE_DIR:~0,-1%
for %%d in (%BASE_DIR%) do set BASE_DIR=%%~dpd

set CLASSPATH=%BASE_DIR%/lib/*;%BASE_DIR%/conf;%CLASSPATH%

rem ===========================================================================================
rem JVM Configuration
rem ===========================================================================================
set "JAVA_OPT=%JAVA_OPT% -server -Xms1g -Xmx1g -Xmn256m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m"
set "JAVA_OPT=%JAVA_OPT% -cp "%CLASSPATH%""

"%JAVA%" %JAVA_OPT% %*

-------------------------------------------------------------------------

二、启动服务

启动broker: mqbroker -c ../conf/broker.conf -n 127.0.0.1:9876

启动nameserver: mqnamesrv

手动创建topic:  mqadmin updateTopic -n 127.0.0.1:9876  -b 127.0.0.1:10911  -t topicxxx

-b一定要IP:port,不能用brokername的方式,否则报错

三、springboot引入 rocketmq-spring-boot-starter和配置

1、pom

    org.apache.rocketmq
    rocketmq-spring-boot-starter
    ${rocketmq-spring-boot-starter-version}

2、配置property

 group一定要加,否则报错。

rocketmq:
  name-server: 127.0.0.1:9876
  producer:
    group: ${spring.application.name}

3、使用:

生产端,注意,OrderVo必须有无参构建函数

@RequestMapping("order")
public class OrderController {
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping("/index")
    public String index(){
//        rocketMQTemplate.convertAndSend("test-topic", "hello");
        rocketMQTemplate.send("order-topic", MessageBuilder.withPayload(new OrderVo(89793831L, 99999L, new BigDecimal("989"))).build());
        return "index";
    }
}

消费端服务:

@Service
@RocketMQMessageListener(topic = "order-topic", consumerGroup = "order-consumer")
public class OrderListenerService implements RocketMQListener {

    @Override
    public void onMessage(OrderVo message) {

        System.out.print("------- OrderPaidEventConsumer received:"+ JSON.toJSONString(message));
    }
}

收工!

你可能感兴趣的:(java,开发语言,后端)