Dubbo+Spring定时器

jar

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>cn.hzygroupId>
    <artifactId>SpringTaskServiceImplartifactId>
    <version>0.0.1-SNAPSHOTversion>

    <dependencies>
        <dependency>
            <groupId>org.apache.taglibsgroupId>
            <artifactId>taglibs-standard-specartifactId>
            <version>1.2.1version>
        dependency>
        <dependency>
            <groupId>org.apache.taglibsgroupId>
            <artifactId>taglibs-standard-implartifactId>
            <version>1.2.1version>
        dependency>

        
        <dependency>
            <groupId>org.apache.tomcatgroupId>
            <artifactId>tomcat-jsp-apiartifactId>
            <version>7.0.53version>
        dependency>
        

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>4.3.2.RELEASEversion>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>4.3.2.RELEASEversion>
        dependency>

        <dependency>
            <groupId>cn.hzygroupId>
            <artifactId>SpringTaskServiceartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.40version>
        dependency>

        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.2.1version>
        dependency>

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.1.1version>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
            <version>2.5.3version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframeworkgroupId>
                    <artifactId>springartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.jboss.nettygroupId>
                    <artifactId>nettyartifactId>
                exclusion>
            exclusions>

        dependency>


        
        <dependency>
            <groupId>org.apache.zookeepergroupId>
            <artifactId>zookeeperartifactId>
            <version>3.4.6version>
            <type>pomtype>
        dependency>


        
        <dependency>
            <groupId>com.101tecgroupId>
            <artifactId>zkclientartifactId>
            <version>0.10version>
        dependency>


    dependencies>
project>

1.需要有zookeeper,并且zookeeper服务开启

2.生产者提供服务,把service接口项目作为依赖,

//首先,加载spring的配置文件使用dubbo把服务发布出去
public class SendService {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:Spring-mybatis.xml");
        context.start();

        //下面就是让这个类一直跑着,为controller提供服务
        synchronized (SendService.class) {
            while (true) {
                try {
                    SendService.class.wait();
                } catch (InterruptedException e) {

                }
            }
        }
    }
}

spring的配置文件


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd 
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/task  
       http://www.springframework.org/schema/task/spring-task-3.1.xsd  
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

        <context:component-scan base-package="cn.hzy"/>

        <mvc:annotation-driven/>

        <mvc:default-servlet-handler/>

        
        
        <dubbo:application name="SpringTaskServiceImpl"/>

        
        <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/> 

        
        <dubbo:protocol name="dubbo" port="20880" />

        
        <dubbo:service interface="cn.hzy.service.DeptService" ref="deptServiceImpl" />



        
        <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://127.0.0.1:3306/database_one"/>
            <property name="username" value="root"/>
            <property name="password" value="root"/>
        bean>

        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="datasource"/>
            <property name="mapperLocations" value="classpath*:cn/hzy/persistence/**/*.xml"/>
        bean>

        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="cn.hzy.persistence">property>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
        bean>

        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="datasource"/>
        bean>

        <tx:annotation-driven transaction-manager="transactionManager"/>

beans>

3.消费者使用服务,把service接口项目作为依赖,

@Controller
public class TestDemo {

    @Autowired
    private DeptService deptService;

    @Scheduled(cron="0/5 * *  * * ? ")
    @RequestMapping("/aaa")
    public void demo1(){
        List list = deptService.findDept();
        for (Dept dept : list) {
            System.out.println(dept.getDeptno()+"***"+dept.getDname());
        }
    }
}

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd 
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/task  
       http://www.springframework.org/schema/task/spring-task-3.1.xsd  
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

        <context:component-scan base-package="cn.hzy"/>

        <mvc:annotation-driven/>

        <mvc:default-servlet-handler/>

        

        <task:annotation-driven/>

        <context:annotation-config/>  
            <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  
        <context:component-scan base-package="cn.hzy"/> 

        <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />

            
            <property name="prefix" value="/" />

            
            
            <property name="suffix" value=".jsp" />
         bean>

         
         <dubbo:application name="SpringTask"/>

         <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>

         //要和注入的service名字一样
         <dubbo:reference interface="cn.hzy.service.DeptService" id="deptService" />

beans>

测试

Dubbo+Spring定时器_第1张图片

你可能感兴趣的:(后端框架)