项目搭建
由于是第一次学习Dubbo,所以这里只是将项目搭建成普通的Java项目。
**提前说明:**这里没有用到注册中心,链接是点对点直连。
这里的生产者项目负责提供服务,消费者用来调用生产者提供的服务。
引入依赖
我是通过Maven的方式引入依赖的,方法非常简单。在生产者项目和消费者项目的pom文件中添加:
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.1</version>
</dependency>
由于dubbo中已经引入了Spring的依赖,所以我没有自己引入Spring。如果要引入自己的想要的版本的Spring,就要屏蔽dubbo中自带的Spring版本:
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.1</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
配置文件
生产者项目的配置文件
也可将两个文件合并为一个。
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="applicationContext-dubbo-provider.xml" />
</beans>
applicationContext-dubbo-provider.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.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 提供服务的实现类 -->
<bean id="sayHelloServiceImpl" class="me.jiang.demo.dubbo.provider01.service.impl.SayHelloServiceImpl" />
<!-- 服务应用的名称 -->
<dubbo:application name="dubbo-provider01" />
<!-- 设置服务,因为实例中未使用注册中心,所以将registry属性设置为“N/A” -->
<dubbo:service interface="me.jiang.demo.dubbo.provider01.service.SayHelloService" ref="sayHelloServiceImpl" registry="N/A"/>
</beans>
消费者配置文件
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="applicationContext-dubbo-consumer01.xml"/>
</beans>
applicationContext-dubbo-provider.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.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 消费者服务应用的名字 -->
<dubbo:application name="dubbo-consumer01" />
<!-- 指定要订阅的服务,interface属性中的值是服务接口的全限定名,url属性表示默认的本地dubbo服务 -->
<!-- 由于interface属性中引用的是生产者服务中的类,所以要在消费者项目中导入生产者项目中相应接口的jar包 -->
<dubbo:reference id="sayHello" interface="me.jiang.demo.dubbo.provider01.service.SayHelloService" url="dubbo://localhost:20880" />
</beans>
提供生产者服务代码
新建一个接口和一个实现了接口的类
SayHelloService.java
package me.jiang.demo.dubbo.provider01.service;
public interface SayHelloService {
String sayHello();
}
SayHelloServiceImpl.java
package me.jiang.demo.dubbo.provider01.service.impl;
import me.jiang.demo.dubbo.provider01.service.SayHelloService;
public class SayHelloServiceImpl implements SayHelloService{
@Override
public String sayHello() {
// TODO Auto-generated method stub
return "Hello Dubbo";
}
}
启动生产者服务
创建一个带有main方法的类:
package me.jiang.demo.dubbo.provider01.main;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RunProvider01 {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
ctx.start();
try {
System.in.read(); //阻塞服务
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ctx.close();
}
}
提供消费者服务代码
package me.jiang.demo.dubbo.consumer01.main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import me.jiang.demo.dubbo.provider01.service.SayHelloService;
public class RunConsumer01 {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-dubbo-consumer01.xml");
SayHelloService sayHello = ctx.getBean("sayHello", SayHelloService.class);
System.out.println(sayHello.sayHello());
ctx.close();
}
}
启动服务
先启动生产者服务,再启动消费之服务查看效果。
生产者启动
消费者启动
第一次写博客,如有错误望诸君指正。