dubbo基本用法

  1. 对外提供接口项目作为其他项目调用dubbo服务的入口

  2. 新建一个项目Provider,提供具体的服务

  3. 在Provider项目中配置pom.xml

    <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>
    	<parent>
    		<groupId>com.ego</groupId>
    		<artifactId>ego-parent</artifactId>
    		<version>0.0.1-SNAPSHOT</version>
    	</parent>
    	<artifactId>ego-service-impl</artifactId>
    	<properties>
    		<spring-version>4.1.6.RELEASE</spring-version>
    		<aspectjweaver-version>1.8.6</aspectjweaver-version>
    		<mybatis-version>3.2.7</mybatis-version>
    		<mybatis-spring-version>1.2.3</mybatis-spring-version>
    		<log4j-version>1.2.17</log4j-version>
    		<mysql-connector-java-version>5.1.38</mysql-connector-java-version>
    		<dubbo-version>2.5.3</dubbo-version>
    		<zkClient-version>0.10</zkClient-version>
    		<pagehelper-version>4.1.6</pagehelper-version>
    	</properties>
    	<dependencies>
    		<--分页插件-->
    		<dependency>
    			<groupId>com.github.pagehelper</groupId>
    			<artifactId>pagehelper</artifactId>
    			<pagehelper-version>4.1.6</pagehelper-version>
    			<pagehelper-version>4.1.6</pagehelper-version>
    		</dependency>
    		<--依赖对外提供的公共接口-->
    		<dependency>
    			<groupId>com.ego</groupId>
    			<artifactId>ego-service</artifactId>
    			<version>0.0.1-SNAPSHOT</version>
    		</dependency>
    		<!-- spring -->
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-webmvc</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-jdbc</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.aspectj</groupId>
    			<artifactId>aspectjweaver</artifactId>
    		</dependency>
    		<!-- mybatis -->
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis</artifactId>
    		</dependency>
    		<!-- mybatis和spring整合 -->
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis-spring</artifactId>
    		</dependency>
    		<!-- log4j 日志 -->
    		<dependency>
    			<groupId>log4j</groupId>
    			<artifactId>log4j</artifactId>
    		</dependency>
    		<!-- mysql 驱动类 -->
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    		</dependency>
    		<!-- dubbo -->
    		<dependency>
    			<groupId>com.alibaba</groupId>
    			<artifactId>dubbo</artifactId>
    			<exclusions>
    				<exclusion>
    					<artifactId>spring</artifactId>
    					<groupId>org.springframework</groupId>
    				</exclusion>
    			</exclusions>
    		</dependency>
    		<!-- 访问zookeeper的客户端jar -->
    		<dependency>
    			<groupId>com.101tec</groupId>
    			<artifactId>zkclient</artifactId>
    		</dependency>
    	</dependencies>
    </project>
    

    applicationContext.xml

    <?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:aop="http://www.springframework.org/schema/aop"
    	xmlns:tx="http://www.springframework.org/schema/tx" 
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.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" default-autowire="byName">
        <!-- 注解扫描 -->
        <context:component-scan base-package="com.wd.dubbo.service.impl"></context:component-scan>
        <!-- 加载属性文件 -->
        <context:property-placeholder location="classpath:db.properties"/>
        <!-- 数据源 -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        	<property name="driverClassName" value="${jdbc.driver}"></property>
        	<property name="url" value="${jdbc.url}"></property>
        	<property name="username" value="${jdbc.username}"></property>
        	<property name="password" value="${jdbc.password}"></property>
        </bean>
        <!-- SqlSessionFactory -->
        <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
        	<property name="dataSource" ref="dataSource"></property>
        	<property name="typeAliasesPackage" value="com.wd.pojo"></property>
        	<property name="configLocation" value="classpath:mybatis.xml"></property>
        </bean>
        <!-- 扫描器 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        	<property name="basePackage" value="com.wd.mapper"></property>
        	<property name="sqlSessionFactoryBeanName" value="factory"></property>
        </bean>
        <!-- 事务管理器 -->
        <bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        	<property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 声明式事务 -->
        <tx:advice id="txAdvice" transaction-manager="txManage">
        	<tx:attributes>
        		<tx:method name="ins*" rollback-for="java.lang.Exception"/>
        		<tx:method name="del*"  rollback-for="java.lang.Exception"/>
        		<tx:method name="upd*"  rollback-for="java.lang.Exception"/>
        		<tx:method name="*" read-only="true"/>
        	</tx:attributes>
        </tx:advice>
        <!-- 配置aop -->
        <aop:config>
        	<aop:pointcut expression="execution(* com.wd.dubbo.service.impl.*.*(..))" id="mypoint"/>
        	<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>
        </aop:config>
    </beans>

    db.properties

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url = jdbc:mysql://localhost:3306/ssm
    jdbc.username = root
    jdbc.password = root

    log4j.xml

    log4j.rootCategory=ERROR, CONSOLE 
    
    log4j.logger.com.wd.mapper=DEBUG
    
    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
    log4j.appender.CONSOLE.layout.ConversionPattern=%C %d{YYYY-MM-dd hh:mm:ss}  %m %n
    
    log4j.appender.LOGFILE=org.apache.log4j.FileAppender
    log4j.appender.LOGFILE.File=E:/my.log
    log4j.appender.LOGFILE.Append=true
    log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
    log4j.appender.LOGFILE.layout.ConversionPattern=%m %n
    

    mybatis.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration
      PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    	<plugins>
    		<plugin interceptor="com.github.pagehelper.PageHelper">
    			<!-- 告诉分页插件是哪个数据库 -->
    			<property name="dialect" value="mysql"/>
    		</plugin>
    	</plugins>
    </configuration>

     

  4. 在src目录下创建第一步中接口中所提供接口的实现类

  5. 进行功能注册,在src\main\resources\META-INF\spring下新建applicationContext-dubbo.xml

    <?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: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://code.alibabatech.com/schema/dubbo 
    		http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    	<dubbo:application name="dubbo-service"/>	
    	
    	<dubbo:registry address="192.168.139.130:2181" protocol="zookeeper"></dubbo:registry>
    	
    	<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
    	
    	<dubbo:service interface="com.wd.dubbo.service.MenuDubboService" ref="menuServiceImpl"></dubbo:service>
    	<bean id="menuServiceImpl" class="com.wd.dubbo.service.impl.MenuDubboServiceImpl"></bean>
    	
    	<!-- 同时把spring配置文件也加载 -->
    	<import resource="../../applicationContext.xml"/>
    </beans>
    
    

     

  6. 在src目录下引入generatorSqlmapCustom逆向工程生成的mapper文件(此处也可以自己创建mapper)

  7. 在src目录下新建dubbo启动器

    package com.ego.test;
    
    import com.alibaba.dubbo.container.Main;
    
    public class Test {
    	public static void main(String[] args) {
    		Main.main(args);
    	}
    }
    

     

posted @ 2019-10-23 18:25  Steven-Russell  阅读(6)  评论(0编辑  收藏  举报  来源