Hello Spring Cloud Alibaba(七)之使用RocketMq

RocketMq介绍

RocketMQ 是一款开源的分布式消息系统,基于高可用分布式集群技术,提供低延时的、高可靠的消息发布与订阅服务。官网地址:https://rocketmq.apache.org/

RocketMq设置

添加topic
在这里插入图片描述

rocketMq提供者

导入包

主要导入消息队列依赖,完整pom如下:

<?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>top.musuixin</groupId>
        <artifactId>hello-alibaba</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>top.musuixin</groupId>
    <artifactId>hello-alibaba-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello-alibaba-provider</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--        表明这是一个spring-cloud应用-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
        </dependency>
<!--        消息队列依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rocketmq</artifactId>
        </dependency>
<!--        dubbo-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>top.musuixin</groupId>
            <artifactId>hello-alibaba-common-api</artifactId>
<!--            版本可以在通用依赖中设置-->
            <version>0.0.1-SNAPSHOT</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>

设置bootstrap.yml

添加Rocketmq的设置

spring:
  cloud:
    stream:
      rocketmq:
        binder:
          # RocketMq的地址
          name-server: 192.168.0.100:9876
      bindings:
        # 设置输出到的topic与消息格式
        output: {destination: test-topic, content-type: application/json}
    nacos:
      config:
        server-addr: 192.168.0.100:8848
        file-extension: yaml
      discovery:
        server-addr: 192.168.0.100:8848
  application:
    name: hello-alibaba-provider
server:
  port: 8090
dubbo:
  protocol:
    # 协议名称
    name: dubbo
    #服务端口
    port: 7100
  registry:
    # nacos注册中心
    address: nacos://192.168.0.100:8848
  application:
    name: hello-alibaba-provider

设置发送消息接口

注入接口

package top.musuixin.provider;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
@SpringBootApplication
@EnableDubbo
@EnableBinding({Source.class})
public class HelloAlibabaProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloAlibabaProviderApplication.class, args);
    }
}

接口类

package top.musuixin.provider.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author musuixin
 * @date 2020-09-11 20:57
 */
@RestController
@RequestMapping("/rocketmq")
public class RocketMqController {
    @Autowired
    private MessageChannel output;
    @GetMapping("/send")
    public String send() {
        Message message = MessageBuilder.withPayload("hello world").build();
        output.send(message);
        return "success";
    }
}

RocketMq消费者

导入包

与提供者导入的包一样

<?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>top.musuixin</groupId>
        <artifactId>hello-alibaba</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>top.musuixin</groupId>
    <artifactId>hello-alibaba-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello-alibaba-consumer</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rocketmq</artifactId>
        </dependency>
        <dependency>
            <groupId>top.musuixin</groupId>
            <artifactId>hello-alibaba-common-api</artifactId>
            <!--            版本可以在通用依赖中设置-->
            <version>0.0.1-SNAPSHOT</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>

设置bootstrap.yml

spring:
  cloud:
    nacos:
        # 远程配置文件
      config:
        server-addr: 192.168.0.100:8848
        file-extension: yaml
        # 服务发现
      discovery:
        server-addr: 192.168.0.100:8848
    sentinel:
      transport:
        dashboard: 192.168.0.100:8858
        client-ip: 192.168.0.1
    stream:
      rocketmq:
        binder:
          # 设置RocketMq地址
          name-server: 192.168.0.100:9876
      bindings:
        # 设置topic、消息格式、group
        input: {destination: test-topic, content-type: application/json,group: test-group}
  application:
    name: hello-alibaba-consumer
server:
  port: 8000
dubbo:
  protocol:
    name: dubbo
    port: 7010
  registry:
    # nacos注册中心
    address: nacos://192.168.0.100:8848
  application:
    name: hello-alibaba-consumer

设置消息消费类

注入接口

package top.musuixin.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Sink;
@SpringBootApplication
@EnableBinding({Sink.class})
public class HelloAlibabaConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloAlibabaConsumerApplication.class, args);
    }
}

消费类

package top.musuixin.consumer.service;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
/**
 * @author musuixin
 * @date 2020-09-22 21:31
 */
@Component
public class RocketmqConsumer {
    @StreamListener("input")
    public void helloRock(String receiveMsg) {
        System.err.println(receiveMsg);
    }
}

启动

依次启动服务提供者与消费者,访问发送消息接口
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Alibaba Sentinel 是一个流量控制、熔断降级框架,主要用于保护微服务应用程序的稳定性和可靠性。下面是 Sentinel 使用的简单步骤: 1. 在项目的 pom.xml 文件中添加 Sentinel 的依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2.2.2.RELEASE</version> </dependency> ``` 2. 在启动类上添加 @EnableDiscoveryClient、@EnableFeignClients、@EnableCircuitBreaker、@EnableHystrixDashboard、@EnableSentinelDashboard 注解。 ```java @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients @EnableCircuitBreaker @EnableHystrixDashboard @EnableSentinelDashboard public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 3. 在 application.yml 中配置 Sentinel: ```yaml spring: cloud: sentinel: transport: dashboard: localhost:8080 ``` 其中 `localhost:8080` 是 Sentinel Dashboard 的地址。 4. 在 Controller 或 Service 的方法上添加 Sentinel 注解,例如: ```java @GetMapping("/hello") @SentinelResource(value = "hello", blockHandler = "blockHandler") public String hello() { return "Hello World"; } public String blockHandler(BlockException ex) { return "Blocked by Sentinel"; } ``` 其中 `@SentinelResource` 注解用于定义资源名称,并且可以指定 blockHandler 方法,当资源被限流或降级时,将会调用该方法。 5. 启动 Sentinel Dashboard,使用浏览器访问 `http://localhost:8080` 即可查看 Sentinel 监控面板。 以上是 Spring Cloud Alibaba Sentinel 的简单使用步骤,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值