SpringCloud微服务学习----------SpringCloudStream

SpringCloud微服务学习----------SpringCloudStream_第1张图片

SpringCloudStream是操作消息组件的另一种方法。

SpringCloudStream是为微服务应用构建消息能力的一个框架

大概流程就是应用程序通过input和output于stream中的binder交互,而binder会与中间件交互,Binder是Stream的抽象概念

也就是对中间件的进一步封装,下面来操作流程

首先引入依赖,stream现在只支持图上两种中间件,所以后缀名你懂的

		
			org.springframework.cloud
			spring-cloud-starter-stream-rabbit
		

之后要配置mq的账户地址信息

  rabbitmq:
    host: 192.168.226.140
    port: 5672
    username: guest
    password: guest

配置完之后可以来写代码了,首先建立一个接口(基本是固定格式)

package com.sola.msg;

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

/**
 * @author JAVA
 * Stream
 *
 */
public interface StreamClient {
	
	String INPUT = "myStreamMsg";

	@Input(StreamClient.INPUT)
	SubscribableChannel input();
	
	@Output(StreamClient.INPUT)
	MessageChannel output();
}

然后去建立一个接收服务

package com.sola.msg;

import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;

@Component
//重点 值是刚写的接口
@EnableBinding(StreamClient.class)
public class StreamGetMsg {

	@StreamListener(StreamClient.INPUT)
	public void getMsg(Object message){
		
		System.out.println("Stream框架接收到的消息"+message);
	}
}

去建立一个发送方,就直接写在Controller里了

public class PostManController {
	
	@Autowired
	private StreamClient streamclient;

	@GetMapping(value="streamsendmsg")
	@ApiOperation("Stream框架发送rabbitmq消息")
	public void streamSendMsg(String msg){
		
		streamclient.output().send(MessageBuilder.withPayload(msg).build());
	}
}

然后去运行,结果启动都启动不了=。=,大概说的是命名重复了

然后 网上去一搜 说是 版本的问题 不允许这么命名,这么说的话理论上说不在一一个服务里这么写是可以的。

那么我就去实验一下建立一个接收方例如CatchMQ服务 先写接口

package com.sola.service;

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

public interface StreamClient {
	
	@Input("MyMessage")
	SubscribableChannel input();

}
package com.sola.service;

import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;

@Component
@EnableBinding(StreamClient.class)
public class StreamReceiver {

	
	@StreamListener("MyMessage")
	public void process(String message){
		
		System.out.println(message);
	}
	
}

然后运行,然后去建立发送方

package com.sola.rabbitmq;

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

public interface StreamClient {
	
	@Output("MyMessage")
	MessageChannel output();
	

}
package com.sola.rabbitmq;

import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.stereotype.Component;

@Component
@EnableBinding(StreamClient.class)
public class StreamReceiver {

}
@RestController
@RequestMapping("eurekaApi")
/*@RefreshScope*/
public class PostManController {
	
	@Autowired
	private StreamClient streamclient;

    @GetMapping(value="rabbitsend")
	@ApiOperation("mq发送测试")
	public void rabbitSend(String str){
		
		streamclient.output().send(MessageBuilder.withPayload(str).build());
	}

}

这样简单的通路就完成了

你可能感兴趣的:(SpringCloud)