当前位置:   article > 正文

spring-boot 整合 netty与rabbitMQ 处理不能消费的问题

spring-boot 整合 netty与rabbitMQ 处理不能消费的问题

1、 在集成 netty 与rabbitMQ 时,发现 不收接收 rabbitMQ消息。

具体不能监听消息通道的代码如下  我是监听ApplicationListener的时候 启动的netty服务。这样不能加入mq消息通道,思考这里是不是MQbean没有注入呢。所以我就修改了下注入方式。

  1. package com.wawi.imsocket;
  2. import com.alibaba.nacos.api.exception.NacosException;
  3. import com.alibaba.nacos.api.naming.NamingFactory;
  4. import com.alibaba.nacos.api.naming.NamingService;
  5. import com.wawi.imsocket.socket.SocketPipelineAdd;
  6. import com.wawi.imsocket.utls.NetWorkUtils;
  7. import com.wawi.imsocket.utls.SpringUtil;
  8. import io.netty.bootstrap.ServerBootstrap;
  9. import io.netty.channel.ChannelFuture;
  10. import io.netty.channel.ChannelInitializer;
  11. import io.netty.channel.ChannelOption;
  12. import io.netty.channel.EventLoopGroup;
  13. import io.netty.channel.nio.NioEventLoopGroup;
  14. import io.netty.channel.socket.SocketChannel;
  15. import io.netty.channel.socket.nio.NioServerSocketChannel;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.context.ApplicationListener;
  19. import org.springframework.context.event.ContextRefreshedEvent;
  20. import org.springframework.stereotype.Component;
  21. @Slf4j
  22. @Component
  23. public class NettyServerBootstrap implements ApplicationListener<ContextRefreshedEvent> {
  24. @Value("${nettyWebsocket.port:10025}")
  25. private int socketServerPort;
  26. @Value("${nettyWebsocket.name:wc-service}")
  27. private String name;
  28. @Value("${nettyWebsocket.nacosAddress:127.0.0.1}")
  29. private String nacosAddress;
  30. @Value("${nettyWebsocket.nacosPort:8848}")
  31. private Integer nacosPort;
  32. /**
  33. * 启动对应服务器
  34. */
  35. public void startup() {
  36. EventLoopGroup bossGroup = new NioEventLoopGroup();
  37. EventLoopGroup workGroup = new NioEventLoopGroup();
  38. try {
  39. ServerBootstrap b = new ServerBootstrap();
  40. b.option(ChannelOption.SO_BACKLOG, 4096);
  41. b.group(bossGroup, workGroup)
  42. .channel(NioServerSocketChannel.class)
  43. .childHandler(new ChannelInitializer<SocketChannel>() {
  44. @Override
  45. protected void initChannel(SocketChannel ch) throws Exception {
  46. SpringUtil.getBean(SocketPipelineAdd.class).handAdd(ch.pipeline());
  47. }
  48. });
  49. ChannelFuture future = b.bind("0.0.0.0", socketServerPort).sync();
  50. log.info("================启动成功,端口号:{}========", socketServerPort);
  51. init();
  52. future.channel().closeFuture().sync();
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. } finally {
  56. log.info("server exit...");
  57. bossGroup.shutdownGracefully();
  58. workGroup.shutdownGracefully();
  59. }
  60. }
  61. @Override
  62. public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
  63. SpringUtil.getBean(NettyServerBootstrap.class).startup();
  64. }
  65. public void init() {
  66. try {
  67. //获取nacos服务
  68. NamingService namingService = NamingFactory.createNamingService(nacosAddress+":" +nacosPort);
  69. //将服务注册到注册中心\
  70. // System.out.println(NetWorkUtils.getHost());
  71. namingService.registerInstance(name, NetWorkUtils.getHost(), Integer.valueOf(socketServerPort));
  72. } catch (Exception e) {
  73. e.printStackTrace();
  74. log.error("注册nacos失败", e);
  75. }
  76. }
  77. }

修改如下 取消 启动 监听 直接在启动main 之后直接启动 

  1. package com.wawi.imsocket;
  2. import com.alibaba.nacos.api.exception.NacosException;
  3. import com.alibaba.nacos.api.naming.NamingFactory;
  4. import com.alibaba.nacos.api.naming.NamingService;
  5. import com.wawi.imsocket.socket.SocketPipelineAdd;
  6. import com.wawi.imsocket.utls.NetWorkUtils;
  7. import com.wawi.imsocket.utls.SpringUtil;
  8. import io.netty.bootstrap.ServerBootstrap;
  9. import io.netty.channel.ChannelFuture;
  10. import io.netty.channel.ChannelInitializer;
  11. import io.netty.channel.ChannelOption;
  12. import io.netty.channel.EventLoopGroup;
  13. import io.netty.channel.nio.NioEventLoopGroup;
  14. import io.netty.channel.socket.SocketChannel;
  15. import io.netty.channel.socket.nio.NioServerSocketChannel;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.context.ApplicationListener;
  19. import org.springframework.context.event.ContextRefreshedEvent;
  20. import org.springframework.stereotype.Component;
  21. @Slf4j
  22. @Component
  23. public class NettyServerBootstrap {
  24. @Value("${nettyWebsocket.port:10025}")
  25. private int socketServerPort;
  26. @Value("${nettyWebsocket.name:wc-service}")
  27. private String name;
  28. @Value("${nettyWebsocket.nacosAddress:127.0.0.1}")
  29. private String nacosAddress;
  30. @Value("${nettyWebsocket.nacosPort:8848}")
  31. private Integer nacosPort;
  32. /**
  33. * 启动对应服务器
  34. */
  35. public void startup() {
  36. EventLoopGroup bossGroup = new NioEventLoopGroup();
  37. EventLoopGroup workGroup = new NioEventLoopGroup();
  38. try {
  39. ServerBootstrap b = new ServerBootstrap();
  40. b.option(ChannelOption.SO_BACKLOG, 4096);
  41. b.group(bossGroup, workGroup)
  42. .channel(NioServerSocketChannel.class)
  43. .childHandler(new ChannelInitializer<SocketChannel>() {
  44. @Override
  45. protected void initChannel(SocketChannel ch) throws Exception {
  46. SpringUtil.getBean(SocketPipelineAdd.class).handAdd(ch.pipeline());
  47. }
  48. });
  49. ChannelFuture future = b.bind("0.0.0.0", socketServerPort).sync();
  50. log.info("================启动成功,端口号:{}========", socketServerPort);
  51. init();
  52. future.channel().closeFuture().sync();
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. } finally {
  56. log.info("server exit...");
  57. bossGroup.shutdownGracefully();
  58. workGroup.shutdownGracefully();
  59. }
  60. }
  61. public void init() {
  62. try {
  63. //获取nacos服务
  64. NamingService namingService = NamingFactory.createNamingService(nacosAddress+":" +nacosPort);
  65. //将服务注册到注册中心\
  66. // System.out.println(NetWorkUtils.getHost());
  67. namingService.registerInstance(name, NetWorkUtils.getHost(), Integer.valueOf(socketServerPort));
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. log.error("注册nacos失败", e);
  71. }
  72. }
  73. }

 这里是重点 在 run之后启动 netty服务,这样就能创建 mq queue了

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. @EnableCircuitBreaker
  4. @ComponentScan(basePackages= {"com.wawi","cn.wawi"})
  5. @EnableCaching
  6. public class ImSocketApplication {
  7. @Bean
  8. public SpringUtil getSpringUtil() {
  9. return new SpringUtil();
  10. }
  11. public static void main(String[] args) {
  12. SpringApplication.run(ImSocketApplication.class, args);
  13. SpringUtil.getBean(NettyServerBootstrap.class).startup();
  14. }
  15. }

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/996861
推荐阅读
相关标签
  

闽ICP备14008679号