1.使用RabbitMQ监控用户支付结果,项目采用微服务架构,一个项目专门支付,一个项目专门监控并修改mysql数据库
2.RabbitMQ的使用
.a)在CentOS7中通过docker拉取RabbgitMQ镜像并创建容器,具体:https://blog.csdn.net/brantykl/article/details/123012122?spm=1001.2014.3001.5502
.b)新建支付微服务,并添加相关依赖(暂不添加支付,通过postman模拟支付的回调保存数据到RabbitMQ的队列中)

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-amqp</artifactId>
  <version>2.1.4.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>2.1.4.RELEASE</version>
</dependency>

.c)支付微服务添加配置文件yml(注意:RabbitMQ配置位置要正确)

server:
  port: 18090
spring:
  application:
    name: order
  rabbitmq:
    username: brant
    password: brant
    virtual-host: my_vhost
    host: 192.168.211.132
    port: 5672
mq:
  pay:
    exchange:
      order: exchange.order
    queue:
      order: queue.order
    routing:
      key: queue.order

.d)配置启动类.将交换机,队列,路由key交给spring容器(订阅模式)

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableEurekaClient
/*@Import(RabbitMQInfo.class)*/
@EnableAutoInject
public class WeixinPayApplication {
    public static void main(String[] args) {
        SpringApplication.run(WeixinPayApplication.class,args);
    }

    @Autowired
    private RabbitMQInfo rabbitMQInfo;

    @Bean
    public IdWorker idWorker(){
        return new IdWorker(0,1);
    }

    //创建队列
    @Bean
    public Queue queueOrder(){
        return new Queue(rabbitMQInfo.getQueue());
    }

    //创建交互机 路由模式的交换机
    @Bean
    public DirectExchange createExchange(){
        return new DirectExchange(rabbitMQInfo.getExchange());
    }

    //创建绑定 指定routingkey
    @Bean
    public Binding createBinding(){
        return BindingBuilder.bind(queueOrder()).to(createExchange()).with(rabbitMQInfo.getRouting());
    }

}

. 1)在开始的时候,我没有配置,而是在web界面上进行了配置,后来发现在程序启动时也是可以创建的
. 2)读取yml参数是通过自定义注解实现第三方Bean的注入

/**
 * 实现加载第三方Bean
 *
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import({RabbitMQInfo.class})
public @interface EnableAutoInject {
}

.e)监控微服务配置与上述一致,除了以上配置,还需注意在web界面需要新建一个用户并给予权限:https://www.cnblogs.com/xdr630/p/15254603.html
.f)在所有配置完成后通过postman模拟携带支付数据发送post请求到支付微服务
.g)支付微服务接收到请求执行以下程序

@RequestMapping(value = "/notify/url")
public String notifyUrl(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam String pay){
       
//发送消息给MQ 订单号 交易流水 支付结果
//参数1 指定交换机的名称
//参数2 指定routingkey
//参数3 指定消息本身   
  rabbitTemplate.convertAndSend(rabbitMQInfo.getExchange(),
  								rabbitMQInfo.getRouting(),
   								JSON.toJSONString(pay));
return null;
}

3.总结
 1)注意yml文件里的配置的位置
 2)RabbitMQ用户的权限问题
 3)交换机,队列,路由key交给spring容器
 4)RabbitMQ在新建容器时默认用户是guest,guest.此时默认只可以通过localhost访问
 5)加载第三方Bean需要使用yml的参数是要写全称
 6)在使用SpringBoot整合junit时,需要加上以下注解,并导入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
@SpringBootTest
@RunWith(SpringRunner.class)
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐