创建消息生产模块cloud-stream-rabbitmq-provider8801
更改生产模块的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2022</artifactId>
<groupId>org.gcl.learn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-stream-rabbitmq-provider8801</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
更改生产模块的yml
server:
port: 8801
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://127.0.0.1:7001/eureka/
spring:
application:
name: cloud-stream-rabbitmq-provider
rabbitmq:
host: xxx.xx.xx.xx
port: 5672
username: guest
password: guest
cloud:
stream:
binders:
defaultRabbit:
type: rabbit
bindings:
output:
destination: studyExchange
content-type: application/json
binder: defaultRabbit
更改生产模块的主启动类
package com.gcl.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class StreamRabbitMQMain8801 {
public static void main(String[] args) {
SpringApplication.run( StreamRabbitMQMain8801.class, args );
}
}
更改生产模块的接口类和实现类
package com.gcl.springcloud.service;
public interface StreamService {
String send();
}
package com.gcl.springcloud.service.impl;
import com.gcl.springcloud.service.StreamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.MessageChannel;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.UUID;
@EnableBinding(Source.class)
@Service
@Slf4j
public class StreamServiceImpl implements StreamService {
@Resource
private MessageChannel output;
@Value("${server.port}")
private String serverPort;
@Override
public String send() {
String serial = UUID.randomUUID().toString();
output.send(MessageBuilder.withPayload(serial.getBytes()).build());
log.info( "****************serial"+ serial + " , server.port : " + serverPort );
return null;
}
}
更改生产模块的控制层
package com.gcl.springcloud.controller;
import com.gcl.springcloud.service.StreamService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class StreamRabbitMQController {
@Resource
private StreamService service;
@GetMapping(value = "/sendMessage")
public String sendMessage(){
return service.send();
}
}
运行
需要先启动cloud-eureka-server7001,再启动cloud-stream-rabbitmq-provider8801。
我们连续的去请求,这个链接,产生大量的数据。