其他分享
首页 > 其他分享> > spring boot rabbitmq hello模式的初试 常见的错误

spring boot rabbitmq hello模式的初试 常见的错误

作者:互联网

1. no beans of 'amqptemplate' type found.

在引入的时候出现红色波浪线的错误。是因为spring boot 中没有导入bean所致。所以可以通过java bean的方式重写注入

package com.example.studymq.config;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
import org.springframework.retry.support.RetryTemplate;

/**
* @author: hett
* @date: 2022/7/5 9:09
*/
@Configuration
public class RabbitConfig {

@Value("${spring.rabbitmq.host}")
private String host;

@Value("${spring.rabbitmq.port}")
private int port;

@Value("${spring.rabbitmq.username}")
private String username;

@Value("${spring.rabbitmq.password}")
private String password;

@Bean
public Queue Queue() {
return new Queue("hello");
}
@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
RetryTemplate retryTemplate = new RetryTemplate();
ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
backOffPolicy.setInitialInterval(500);
backOffPolicy.setMultiplier(10.0);
backOffPolicy.setMaxInterval(10000);
retryTemplate.setBackOffPolicy(backOffPolicy);
template.setRetryTemplate(retryTemplate);
return template;
}
@Bean
public CachingConnectionFactory connectionFactory() {
CachingConnectionFactory factory = new CachingConnectionFactory();
factory.setHost(host);
factory.setPort(port);
factory.setUsername(username);
factory.setPassword(password);
return factory;
}
}
2. access_refused - login was refused using authentication mechanism plain.、项目启动的时候出现错误,说认证机制出现问题
查看服务器上的日志文件

 

 =ERROR REPORT==== 5-Jul-2022::10:00:32 ===
closing AMQP connection <0.313.0> (192.168.30.60:51210 -> 192.168.8.34:5672):
{handshake_error,starting,0,
                 {amqp_error,access_refused,
                             "PLAIN login refused: user 'guest' can only connect via localhost",
                             'connection.start_ok'}}

提醒说是guest账号,但是我审查了一下我的配置文件

server.port=8083
#rabbitmq的配置
spring.application.name=study-mq
spring.rabbitmq.host=192.168.8.34
spring.rabbitmq.port=5672
spring.rabbitmq.username=hetttest
spring.rabbitmq.password=hett
没有使用guest的账号。最后在配置文件中找到了问题

 

 一定要在连接中初始化连接的设置。

spring boot 没有将bean加载,所以自己在写的时候忘了配置信息。。。。

完整的项目代码

https://gitee.com/youran0825/study04

 

标签:spring,boot,factory,springframework,rabbitmq,import,org
来源: https://www.cnblogs.com/youran-he/p/16445586.html