ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-07-05 11:04:06  阅读:230  来源: 互联网

标签:spring boot factory springframework rabbitmq import org


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有