springboot转单体springcloud使用rocketMQ中间件

单体的springboot想使用springcloud中的一些模块如:nacos,rocketmq等

一、转化 

1、修改application.yml 将其中的配置文件迁入到nacos中去。地址信息写入bootstrap.yml中。

spring:
  profiles:
    # active: prod
    active: dev
  main:
    allow-bean-definition-overriding: true
  application:
    name: centerserver-manager

---

spring:
  profiles: dev
  cloud:
    nacos:
      config:
        server-addr: 10.10.22.145:8848
        file-extension: yaml
        group: CENTER_SERVER_GROUP
        namespace: ebaebe63-7383-44b7-a3df-be923ea4aa17
        shared-configs:
          - dataId: centerserver-manager.yaml
            group: CENTER_SERVER_GROUP
            refresh: true
          - dataId: centerserver-provider-activity.yaml
            group: CENTER_SERVER_GROUP
            refresh: true
        enabled: true
      discovery:
        namespace: ebaebe63-7383-44b7-a3df-be923ea4aa17
        group: CENTER_SERVER_GROUP
        server-addr: 10.10.22.145:8848
        metadata:
          management.context-path: ${server.servlet.context-path:}${spring.mvc.servlet.path:}${management.endpoints.web.base-path:}


server:
  port: 8080


logging:
  level:
    com.ajia.zhengding.api.service: debug
    com.alibaba.nacos.client: error

---
spring:
  profiles: prod
  cloud:
    nacos:
      config:
        server-addr: 172.16.9.121
        file-extension: yaml
        namespace: a327a4eb-e403-4549-845d-0851f10716c2
        shared-configs:
          - dataId: centerserver-manager.yaml
            group: CENTER_SERVER_GROUP
            refresh: true
          - dataId: centerserver-provider-activity.yaml
            group: CENTER_SERVER_GROUP
            refresh: true
        enabled: true
        #group: CENTER_SERVER_GROUP
      discovery:
        namespace: a327a4eb-e403-4549-845d-0851f10716c2
        server-addr: 172.16.9.121
        metadata:
          management.context-path: ${server.servlet.context-path:}${spring.mvc.servlet.path:}${management.endpoints.web.base-path:}


server:
  port: 8080

2、pom.xml中引入springcloud配置

<?xml version="1.0" encoding="UTF-8"?>
<project 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xz</groupId>
    <artifactId>xz-unity-operation</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>xz-unity-operation</name>
    <description>运营平台</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud-alibaba-version>2.2.0.RELEASE</spring-cloud-alibaba-version>
        <mybatis.plus.version>3.3.0</mybatis.plus.version>
        <poi.version>4.1.2</poi.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 排除Tomcat依赖-->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 添加 Undertow依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <!-- Spring Cloud Alibaba -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
            <version>${spring-cloud-alibaba-version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-config</artifactId>
            <version>${spring-cloud-alibaba-version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${spring-cloud-alibaba-version}</version>
        </dependency>
        <!-- rocketmq -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
            <version>${spring-cloud-alibaba-version}</version>
        </dependency>

        <!-- dubbo -->
        <!-- kryo - java高效序列化-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-serialization-kryo</artifactId>
            <version>2.7.5</version>
        </dependency>

        <!-- 依赖的内部项目 -->
        <dependency>
            <groupId>com.xz.centerserver</groupId>
            <artifactId>centerserver-provider-api-credit</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>

        <!-- 使用了shiro的md5工具类 -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.4.1</version>
        </dependency>

        <!-- Apache Commons -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>

        <!-- Aliyun -->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.4.2</version>
        </dependency>

        <!-- mybatis -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis.plus.version}</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>${mybatis.plus.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

        <!-- mongodb -->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

        <!-- Ajia -->
        <dependency>
            <groupId>com.xz.ajiaedu</groupId>
            <artifactId>xz-ajiaedu-common</artifactId>
            <version>3.0-SNAPSHOT</version>
        </dependency>

        <!-- poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>ooxml-schemas</artifactId>
            <version>1.4</version>
        </dependency>

        <!-- 其他 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.66</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20190722</version>
        </dependency>
        <!--阿里excel-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>LATEST</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 二、使用rocketMQ

1、pom.xml中引入mq包

 <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
            <version>${spring-cloud-alibaba-version}</version>
 </dependency>

 2、在nocos中配置mq信息,多个mq应用就写在bindings:层级下,如这里的source_modify。

配置input和output都要

  cloud:
    stream:
      bindings:
        school-modify-input: {destination: school-modify_topic, group: school-modify_group, consumer.max-attempts: 1} 
        source_modify_input: {destination: source_config_topic, group: source_config_group}
        source_modify_output: {destination: source_config_topic, group: source_config_group}
      rocketmq:
        binder:
          name-server: 10.10.22.145:9876
        bindings:
          school-modify-input: {consumer.tags: modify}

3、 output.class写法

package com.xz.unity.mq.source;

import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;


/**
 * @author HuangZheng
 * @Date 2020/8/18 10:11
 */
public interface Source {
    String SOURCE_MODIFY_OUTPUT = "source_modify_output";

    @Output(Source.SOURCE_MODIFY_OUTPUT)
    MessageChannel output();
}

发送的载体

package com.xz.unity.mq.source;

import com.alibaba.fastjson.JSON;
import com.xz.unity.mq.school.SchoolModifyRequest;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import org.springframework.util.MimeTypeUtils;

/**
 * @author HuangZheng
 * @Date 2020/8/18 10:22
 */
@Slf4j
@AllArgsConstructor
@Component
public class Send {
    @Autowired
    private Source source;

    public void sendMessage() {
        SchoolModifyRequest schoolModifyRequest = new SchoolModifyRequest();
        schoolModifyRequest.setId(10086);
        schoolModifyRequest.setName("十方");
        log.info("消息发送:{}", JSON.toJSONString(schoolModifyRequest));
        Message<SchoolModifyRequest> message = MessageBuilder.withPayload(schoolModifyRequest)
                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
                .setHeader(RocketMQHeaders.TAGS, "modify")
                .build();
        source.output().send(message);
    }
}

4、input.class的写法

package com.xz.unity.mq.source;

import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;

/**
 * @author HuangZheng
 * @Date 2020/8/18 10:15
 */
public interface Sink {
    String SOURCE_MODIFY_INPUT = "source_modify_input";

    @Input(Sink.SOURCE_MODIFY_INPUT)
    SubscribableChannel input();
}

接收的载体

 

package com.xz.unity.mq.source;

import com.alibaba.fastjson.JSON;
import com.xz.unity.mq.school.SchoolModifyRequest;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

/**
 * @author HuangZheng
 * @Date 2020/8/18 10:23
 */
@Slf4j
@AllArgsConstructor
@Component
@EnableBinding(Sink.class)
public class Receiver {
    @StreamListener(value = Sink.SOURCE_MODIFY_INPUT)
    public void updateMessageReceive(@Payload SchoolModifyRequest schoolModifyRequest) {
        log.info("消息接收:{}", JSON.toJSONString(schoolModifyRequest));
    }
}

 实体bean

package com.xz.unity.mq.school;

import lombok.Data;

import java.util.Date;

/**
 * 学校变更请求对象
 *
 * @author Sujun on 2020/8/15
 */
@Data
public class SchoolModifyRequest {

    //学校id
    private Integer id;

    //学校名称
    private String name;

    //学校所在区
    private String area;

    //学校所在市
    private String city;

    //学校所在省
    private String province;

    //学校所在乡镇
    private String county;

    //学校状态 delete=删除状态,activity=启用,disable=停用
    private String status;

    //学校所属学段
    private String studyStage;

    //学校包含年级
    private String grades;

    //创建时间
    private Date ctime;

    //学校完整地址
    private String fullAddress;

    //创建学校店铺ID
    private Integer storeId;

    //学校编号
    private String code;

    //集团联校ID
    private Integer groupSchoolId;
}

最后需要在启动类上添加注解 

@EnableBinding({InputSchool.class, Sink.class, Source.class})
package com.xz.unity;

import com.xz.unity.config.prop.CommonProp;
import com.xz.unity.config.prop.MongoResLibProp;
import com.xz.unity.config.prop.FwptApiProp;
import com.xz.unity.mq.school.InputSchool;
import com.xz.unity.mq.source.Sink;
import com.xz.unity.mq.source.Source;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.stream.annotation.EnableBinding;

/**
 * @author
 */
@EnableBinding({InputSchool.class, Sink.class, Source.class})
@SpringBootApplication
@EnableConfigurationProperties({CommonProp.class, FwptApiProp.class, MongoResLibProp.class})
public class XzUnityOperationApplication {

    public static void main(String[] args) {
        SpringApplication.run(XzUnityOperationApplication.class, args);
    }

}

最后可以在需要的地方调用发送方法了

send.sendMessage();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十方天士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值