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.0</modelVersion>
    <groupId>cn.hzy</groupId>
    <artifactId>SpringTaskServiceImpl</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-spec</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-impl</artifactId>
            <version>1.2.1</version>
        </dependency>

        <!-- 加载tomcat-api jar文件 -->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jsp-api</artifactId>
            <version>7.0.53</version>
        </dependency>
        <!-- 加载spring 核心jar文件 -->

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>

        <!-- 加载springmvc 核心文件 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>cn.hzy</groupId>
            <artifactId>SpringTaskService</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.40</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.1.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.jboss.netty</groupId>
                    <artifactId>netty</artifactId>
                </exclusion>
            </exclusions>

        </dependency>


        <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
            <type>pom</type>
        </dependency>


        <!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </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的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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应用名写项目名就可以了 -->
        <dubbo:application name="SpringTaskServiceImpl"/>

        <!-- 配置zookeeper路径 -->
        <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/> 

        <!-- 申明是dubbo,端口号 -->
        <dubbo:protocol name="dubbo" port="20880" />

        <!-- 最重要的就是这个,ref由于前面扫描包会小写类名,所以ref写小写类名 -->
        <dubbo:service interface="cn.hzy.service.DeptService" ref="deptServiceImpl" />



        <!--*************************mybatis配置文件部分 ********************************** -->
        <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<Dept> list = deptService.findDept();
        for (Dept dept : list) {
            System.out.println(dept.getDeptno()+"***"+dept.getDname());
        }
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<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/>

        <!--spring定时器  -->

        <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">
            <!-- 支持jstl -->
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />

            <!--webapp下的所有文件-->
            <property name="prefix" value="/" />

            <!--control层返回值是.jsp结尾-->
            <!--作用:control层返回值是什么就返回到什么jsp去  -->
            <property name="suffix" value=".jsp" />
         </bean>

         <!-- dubbo服务 -->
         <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>

测试

这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是Spring Cloud Alibaba + Spring Boot + Dubbo + Nacos + Mybatis Plus + MySQL 项目搭建步骤: 1. 创建一个Spring Boot项目,并添加以下依赖: ```xml <!--Spring Boot依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Dubbo依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> <version>2.2.1.RELEASE</version> </dependency> <!--Nacos依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.1.RELEASE</version> </dependency> <!--Mybatis Plus依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3</version> </dependency> <!--MySQL依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> ``` 2. 配置application.yml文件,添加以下内容: ```yaml spring: application: name: service-provider # 服务名称 datasource: url: jdbc:mysql://localhost:3306/db_example # 数据库URL username: root # 数据库用户名 password: root # 数据库密码 driver-class-name: com.mysql.cj.jdbc.Driver # 数据库驱动 mybatis-plus: mapper-locations: classpath:mapper/*.xml # Mybatis Plus的mapper文件路径 dubbo: application: name: dubbo-service-provider # Dubbo应用名称 registry: address: nacos://localhost:8848 # Nacos注册中心地址 protocol: name: dubbo # Dubbo协议名称 port: 20880 # Dubbo协议端口号 ``` 3. 创建一个数据模型类,例如: ```java @Data public class User { private Long id; private String username; private String password; private String email; } ``` 4. 创建一个Mapper类,例如: ```java @Mapper public interface UserMapper extends BaseMapper<User> { } ``` 5. 创建一个Service类,例如: ```java @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { } ``` 6. 创建一个Controller类,例如: ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public User getUser(@PathVariable Long id) { return userService.getById(id); } } ``` 7. 在Nacos控制台中添加服务提供者的配置信息。 8. 启动服务提供者,并在Nacos控制台中查看服务是否注册成功。 9. 创建一个服务消费者项目,并添加以下依赖: ```xml <!--Spring Boot依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Dubbo依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> <version>2.2.1.RELEASE</version> </dependency> <!--Nacos依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.1.RELEASE</version> </dependency> ``` 10. 配置application.yml文件,添加以下内容: ```yaml spring: application: name: service-consumer # 服务名称 dubbo: application: name: dubbo-service-consumer # Dubbo应用名称 registry: address: nacos://localhost:8848 # Nacos注册中心地址 ``` 11. 创建一个Service类,例如: ```java @Service public class UserServiceImpl implements UserService { @Reference private UserService userService; @Override public User getUser(Long id) { return userService.getById(id); } } ``` 12. 创建一个Controller类,例如: ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public User getUser(@PathVariable Long id) { return userService.getUser(id); } } ``` 13. 启动服务消费者,访问http://localhost:8080/user/{id},查看服务是否调用成功。 以上就是Spring Cloud Alibaba + Spring Boot + Dubbo + Nacos + Mybatis Plus + MySQL项目搭建步骤。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值