Maven项目的使用
项目拆解为 api 接口模块和 provider两个模块时,provider需要对api进行依赖。pom文件中对版本进行管理,子项目需要引入对应的Jar。
父项目pom.xml中
<dependencyManagement>
<dependency>
<artifactId>pay-api</artifactId>
<groupId>com.fightingan.dev</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
项目中pom.xml引用(本项目为provider模块)
<dependencies>
<dependency>
<artifactId>pay-api</artifactId>
<groupId>com.fightingan.dev</groupId>
</dependency>
</dependencies>
不同项目的调用
不同项目要进行远程调用,需要项目进行发布,调用maven的install方法发布到本地,其他项目引入jar包但是注意,引入的时api 对外暴漏接口jar。
<?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.fightingan.dev</groupId>
<artifactId>order-service</artifactId>
<version>1.0-SNAPSHOT</version>
<name>order-service</name>
<dependencies>
<dependency>
<groupId>com.fightingan.dev</groupId>
<artifactId>pay-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
小插曲
多模块项目Module must not contain source root. The root already belongs to module
产生原因:
有时候新建了maven工程,然后删了里面的src目录让它成为空的父项目,但是会报下面的错误。
Module “*” must not contain source root . The root already belongs to module “”.
删除其中不存在的文件夹
dubbo配置文件(服务提供者)
首先默认路径为 META-INF/spring/application.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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 服务名称 唯一便于之后查找 owner : 所有者 organization : 组织-->
<dubbo:application name="order-service" owner="yan" organization="anna"/>
<!-- 配置注册中心 先不用设置为空 -->
<dubbo:registry address="N/A"/>
<!-- 通过哪个协议 端口号是多少 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!--发布服务接口为服务 ref 为实现类-->
<dubbo:service interface="com.fightingan.dev.IPayService" ref="payService"/>
<bean id="payService" class="com.fightingan.dev.impl.PayServiceImpl"/>
</beans>
运行主类
package com.fightingan.dev;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* @author 30378
*/
public class App
{
public static void main( String[] args ) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"META-INF/spring/application.xml"});
context.start();
//项目不终止
System.in.read();
}
}
dubbo服务发布成功并生成的地址信息
dubbo://192.168.56.1:20880/com.fightingan.dev.IPayService?anyhost=true&application=pay-service&bean.name=com.fightingan.dev.IPayService&bind.ip=192.168.56.1&bind.port=20880&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=com.fightingan.dev.IPayService&methods=pay&organization=anna&owner=yan&pid=21076®ister=true&release=2.7.2&side=provider×tamp=1571237731926, dubbo version: 2.7.2, current host: 192.168.56.1
dubbo配置文件(服务调用者)
默认路径为 META-INF/spring/application.xm
这里
application.xml中
id="pay-server" url="dubbo://192.168.56.1:20880/com.fightingan.dev.IPayService"/>为上文生成的 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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 服务名称 唯一便于之后查找 owner : 所有者 organization : 组织-->
<dubbo:application name="pay-service" owner="yan" organization="anna"/>
<!-- 配置注册中心 先不用设置为空 -->
<dubbo:registry address="N/A"/>
<dubbo:reference interface="com.fightingan.dev.IPayService"
id="pay-server" url="dubbo://192.168.56.1:20880/com.fightingan.dev.IPayService"/>
</beans>
package com.fightingan.dev;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* Hello world!
*
* @author 30378
*/
public class App
{
public static void main( String[] args ) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"META-INF/spring/application.xml"});
context.start();
IPayService service = (IPayService) context.getBean("pay-server");
String s = service.pay("Hello yan !");
System.out.println(s);
System.in.read();
}
}
pom.xml引入新依赖
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.26</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>