Skip to content

SpringCloud User Guide

流浪诗人丶 edited this page Dec 20, 2017 · 1 revision
  • 在springcloud 服务提供方(事务的参与方法,被调用方)

    • 引入myth-springcloud 包

       <dependency>
           <groupId>com.github.myth</groupId>
           <artifactId>myth-springcloud</artifactId>
       </dependency>
    • 配置 MythTransactionBootstrap启动类,可以采用xml方式,或者@Bean的方式,具体配置可以参考:配置详解

      <context:component-scan base-package="com.github.myth.*"/>
      <aop:aspectj-autoproxy expose-proxy="true"/>
      <bean id="mythTransactionBootstrap" class="com.github.myth.core.bootstrap.MythTransactionBootstrap">
        <property name="repositorySuffix" value="account-service"/>
        <property name="serializer" value="kryo"/>
        <property name="coordinatorQueueMax" value="5000"/>
        <property name="coordinatorThreadMax" value="8"/>
        <property name="rejectPolicy" value="Abort"/>
        <property name="blockingQueueType" value="Linked"/>
        <property name="repositorySupport" value="db"/>
        <property name="mythDbConfig">
            <bean class="com.github.myth.common.config.MythDbConfig">
                <property name="url"
                          value="jdbc:mysql://192.168.1.68:3306/myth?useUnicode=true&amp;characterEncoding=utf8"/>
                <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
                <property name="password" value="Wgj@555888"/>
                <property name="username" value="xiaoyu"/>
            </bean>
        </property>
      </bean>
    • springcloud服务的实现方法上加上 @Myth

    • 监听消息队列(注解方法上写的消息队列名称),调用框架提供 的MythMqReceiveService.processMessage方法。列如使用jms,具体可以参考demo工程。

      @JmsListener(destination = "account",containerFactory = "queueListenerContainerFactory")
      public void receiveQueue(byte[] message) {
        LOGGER.info("=========扣减账户信息接收到Myth框架传入的信息==========");
        final Boolean success = mythMqReceiveService.processMessage(message);
        if(success){
            //消费成功,消息出队列,否则不消费
        }
      }
    • 在springcloud 消费方(事务的发起者,调用方)

      • 引入myth-springcloud 包

      <dependency>
           <groupId>com.github.myth</groupId>
           <artifactId>myth-springcloud</artifactId>
       </dependency>
      • 设置feignClient在接口上加上@Myth注解,target必须设置成你真实调用的服务接口,千万要设置正确

       @FeignClient(value = "account-service", configuration = MyConfiguration.class)
       public interface AccountClient {
         
              @Myth(destination = "account", target = AccountService.class)
             Boolean payment(@RequestBody AccountDTO accountDO);
       }
      • FeignClient 配置 configuration = MyConfiguration.class

            @Configuration
            public class MyConfiguration {
      
             @Bean
             @Scope("prototype")
             public Feign.Builder feignBuilder() {
                   return Feign.builder()
                 .requestInterceptor(new MythRestTemplateInterceptor())
                      .invocationHandlerFactory(invocationHandlerFactory());
           }
      
             @Bean
             public InvocationHandlerFactory invocationHandlerFactory() {
                    return (target, dispatch) -> {
                    MythFeignHandler handler = new MythFeignHandler();
                    handler.setTarget(target);
                   handler.setHandlers(dispatch);
                  return handler;
              };
           }
          }
      • 配置 MythTransactionBootstrap启动类,可以采用xml方式,或者@Bean的方式,具体配置可以参考:配置详解

        <context:component-scan base-package="com.github.myth.*"/>
        <aop:aspectj-autoproxy expose-proxy="true"/>
        <bean id="mythTransactionBootstrap" class="com.github.myth.core.bootstrap.MythTransactionBootstrap">
          <property name="repositorySuffix" value="account-service"/>
          <property name="serializer" value="kryo"/>
          <property name="coordinatorQueueMax" value="5000"/>
          <property name="coordinatorThreadMax" value="8"/>
          <property name="rejectPolicy" value="Abort"/>
          <property name="blockingQueueType" value="Linked"/>
          <property name="needRecover" value="true"/>
          <property name="scheduledDelay" value="120"/>
          <property name="scheduledThreadMax" value="4"/>
          <property name="repositorySupport" value="db"/>
          <property name="mythDbConfig">
              <bean class="com.github.myth.common.config.MythDbConfig">
                  <property name="url"
                            value="jdbc:mysql://192.168.1.68:3306/myth?useUnicode=true&amp;characterEncoding=utf8"/>
                  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
                  <property name="password" value="Wgj@555888"/>
                  <property name="username" value="xiaoyu"/>
              </bean>
          </property>
        </bean>
      • 注意:在消费方请配置自动恢复策略:防止在极端情况下消息发送失败的情况

        <!-设置为true,表明采用恢复策略-->
        <property name="needRecover" value="true"/>
        <!--调度延迟时间-->
        <property name="scheduledDelay" value="120"/>
        <!--调度线程线程大小-->
        <property name="scheduledThreadMax" value="4"/>
      • 选择你的消息中间件类型,来引入不同的jar包

      • 如果你是使用jms(activemq),引入jar包,并配置ActivemqSendServiceImpl。
        <dependency>
            <groupId>com.github.myth</groupId>
            <artifactId>myth-jms</artifactId>
        </dependency>
        <bean id="activemqSendService" class="com.github.myth.jms.service.ActivemqSendServiceImpl">    
            <property name="jmsTemplate" ref="jmsTemplate"/>
        </bean>
      • 如果你是使用 rabbitmq,引入jar包,并配置RabbitmqSendServiceImpl。
        <dependency>
            <groupId>com.github.myth</groupId>
            <artifactId>myth-rabbitmq</artifactId>
        </dependency>
        
        <bean id="rabbitmqSendService" class="com.github.myth.rabbitmq.service.RabbitmqSendServiceImpl">
          <property name="amqpTemplate" ref="amqpTemplate"/>
        </bean>
      • 如果你是使用 kafka,引入jar包,并配置KafkaSendServiceImpl。
          <dependency>
             <groupId>com.github.myth</groupId>
             <artifactId>myth-kafka</artifactId>
         </dependency>
        <bean id="KafkaSendService" class="com.github.myth.kafka.service.KafkaSendServiceImpl" >
           <property name="kafkaTemplate" ref="kafkaTemplate"/>
        </bean      
      • 如果你是使用 rocketmq,引入jar包,并配置RocketmqSendServiceImpl。
              <dependency>
                 <groupId>com.github.myth</groupId>
                 <artifactId>myth-rocketmq</artifactId>
              </dependency>
        <bean id="rocketmqSendService"   class="com.github.myth.rocketmq.service.RocketmqSendServiceImpl">
          <property name="defaultMQProducer" ref="defaultMQProducer"/>
        </bean>    
Clone this wiki locally