将项目拆分搭建dubbo项目(二)dubbo项目搭建实例与应用

dubbo项目的配置

根据上一篇文章,我们已经创建好两项目一个xxx-common一个xxx-service并创建好相应的目录结构

开始配置配置文件内容

一、配置pom.xml 文件,

    (1)payment-common的pom.xml文件配置

        配置上使用的父项目或者建立上jar包依赖即可

    (2)payment-service的pom.xml文件配置

        配置上除了配置父项目以外还要配置对payment-common项目的依赖因为service要使用到common中的实体和接口工具类等。还有很重要的一个配置打包

<?xml version="1.0" encoding="UTF-8"?>
<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>com.dxl.payment</groupId>
    <artifactId>payment-service</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.uz.dxt</groupId>
        <artifactId>manager</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>com.dxl.payment</groupId>
            <artifactId>payment-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.dxl.util</groupId>
            <artifactId>common-util</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>
    <build><!--打包配置 -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.dxl.payment.main.Application</mainClass><!--配置启动项目的类-->
                                </transformer>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.handlers</resource>
                                </transformer>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.schemas</resource>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

二、配置payment-service项目的resources中的配置文件

一般的ssm项目配置文件

    1、配置applicationContext.xml 文件

        特殊地方引入applicationContext_dubbo_zookeeper.xml

        <import resource="applicationContext_dubbo_zookeeper.xml" />

    2、配置jdbc.properties

    3、mybatis.xml

    4、log4j.properties

    5、spring-mybatis-transaction.xml

 

dubbo    atomikos分布式数据库事务组件相关配置文件 在applicationContext.xml中引用

    1 applicationContext_dubbo_zookeeper.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: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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="Adminuser" />
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <!-- 测试 -->
    <dubbo:registry protocol="zookeeper"  address="192.168.10.14:2181,192.168.10.17:2181,192.168.10.18:2181" />
    <!--<dubbo:registry protocol="zookeeper"  address="192.168.10.4:2181" />-->
    <!-- 生产 -->
    <!--<dubbo:registry protocol="zookeeper"  address="10.27.78.162:2181,10.25.140.22:2181,10.27.81.154:2181" />-->
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20891" />
    <!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 暴露给外部使用的接口-->
    <!-- <dubbo:annotation package="com.call.service.Impl"/> -->
    <dubbo:service interface="com.dxl.payment.service.payment.TRepaymentService" ref="tRepaymentService" timeout="300000"/>
</beans>

<dubbo:service interface="com.dxl.payment.service.payment.TRepaymentService" ref="tRepaymentService" timeout="300000"/>

这句话是代表暴露给外部的接口。interface是payment-common项目中定义的接口

ref是payment-service项目接口实现类的的名称在@Service("tRepaymentService")中声明的名称

示例:

payment-common的接口

package com.dxl.payment.service.payment;
import com.dxl.payment.model.payment.TRepayment;
import com.dxl.payment.service.base.BaseService;
import java.util.List;

public interface TRepaymentService extends BaseService<TRepayment> {
	
	public List<TRepayment>  selectAllRepayment(TRepayment tRepayment);

}

payment-service的接口实现类

package com.dxl.payment.service.payment.impl;
import com.dxl.payment.dao.payment.TRepaymentMapper;
import com.dxl.payment.model.payment.TRepayment;
import com.dxl.payment.service.TipsService;
import com.dxl.payment.service.base.impl.BaseServiceImpl;
import com.dxl.payment.service.payment.TRepaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("tRepaymentService")
public class TRepaymentServiceImpl extends BaseServiceImpl<TRepayment>
		implements TRepaymentService, TipsService<TRepayment> {

	@Autowired
	private TRepaymentMapper tRepaymentMapper;
	
	
	private Format fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Override
	public boolean setTips(TRepayment t) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean removeTips(TRepayment t) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public List<TRepayment> selectAllRepayment(TRepayment tRepayment) {
		return  tRepaymentMapper.selectAllRepayment(tRepayment);
	}
}


dao层与sql正常写即可

在applicationContext_dubbo_zookeeper.xml中暴露出接口信息

<dubbo:service interface="com.dxl.payment.service.payment.TRepaymentService" ref="tRepaymentService" timeout="300000"/>

payment-common与payment-service相当于生产者。而其他web项目调用相当于消费者


使用dubbo项目的方法:

1首先必须确认dubbo项目已经暴露好接口

2在要调用dubbo项目的项目中配置spring-dubbo.xml

    这里演示在其他项目的serivce中调用dubbo,由于web本身就对自身项目的service具有依赖关系所以此处将dubbo的配置文件放在service中。如果不想放在service可以直接在web中配置spring-dubbo.xml文件

spring-dubbo.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	
	xsi:schemaLocation="
					http://www.springframework.org/schema/beans
					http://www.springframework.org/schema/beans/spring-beans.xsd
					http://www.springframework.org/schema/mvc
					http://www.springframework.org/schema/mvc/spring-mvc.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="callphone-service123123"/>

	<!--<dubbo:registry protocol="zookeeper" address="192.168.10.14:2181,192.168.10.17:2181,192.168.10.18:2181" check="false"/>-->
	<!--<dubbo:registry protocol="zookeeper" address="192.168.10.4:2181" check="false"/>-->

	<!-- <dubbo:registry protocol="zookeeper" address="192.168.10.14:2181,192.168.10.17:2181,192.168.10.18:2181" check="false"/>	 -->
	<!--<dubbo:registry protocol="zookeeper" address="192.168.10.4:2181" check="false"/>	-->


	<!-- 测试 -->
	<!--<dubbo:registry protocol="zookeeper" address="192.168.10.4:2181" check="false"/>-->
	 <dubbo:registry protocol="zookeeper" address="192.168.10.14:2181,192.168.10.17:2181,192.168.10.18:2181" check="false"/>
	<!--<!– 生产 –>-->
	 <!--<dubbo:registry protocol="zookeeper"  address="10.27.78.162:2181,10.25.140.22:2181,10.27.81.154:2181" check="false"/>-->
        <!-- 要获取到的dubbo暴露的接口配置-->
	<dubbo:reference id="tRepaymentService" interface="com.dxl.payment.service.payment.TRepaymentService" check="false"/>
</beans>

配置好之后,将payment-common项目编译成jar包,并添加到要使用的项目中示例此处将jar包添加到service项目中和web项目张

测试时可直接加入项目,如果要发布生产,需要将dubbo的payment-common项目jar包放到私库中引用,这样就确保了其他项目只能查看到common中暴露的接口而看不到具体的实现编码,从而使项目和逻辑分离。独立维护。

spring-dubbo.xml中配置的

<dubbo:reference id="tRepaymentService" interface="com.dxl.payment.service.payment.TRepaymentService" check="false"/>

是dubbo的消费者。配置此处可以使调用的payment-service使用到jdbc数据库等。


3测试

编写controller方法

@RequestMapping("/testPaymentDubbo")
	@ResponseBody
	public void testPaymentDubbo(){
		TRepayment tRepayment=new TRepayment();
		tRepayment.setCompanyCode("ceshilvsuo");
		List<TRepayment> list=tRepaymentService.selectAllRepayment(tRepayment);
		for (TRepayment t:list) {
			System.out.println(t.toString());
		}
	}

自己测试时手动启动payment-service中的main层的application类的main方法启动dubbo生产者服务

注意运行时要保证payment-service项目和payment-common项目都编译install一遍之后


dubbo生产者已经启动了

可以用postman直接测试接口是否可用,dubug查看运行。

测试通过即成功完成

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值