基于Dubbo协议的生产者和消费者
1、先建立一个项目
File–>new–>Project
2、新建一个接口ISayHelloInterface
新建一个接口,
并写一个实现方法
public interface ISayHelloInterface {
String sayHello(String var1);
}
3、把这个接口导成jar包
File–>Project–>Project Structure打开下图
4、新建一个生产者项目(DubboProvider)
导入ISayHelloInterface接口
配置文件
在pom.xml文件中增加配置
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.5</version>
<exclusions>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
在spring.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee"
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-3.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesMode" value="2"/>
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<!--<value>classpath:jdbc.properties</value>
<value>classpath:env.properties</value>-->
<value>classpath:application.properties</value>
</list>
</property>
</bean>
<!-- 开启注解 -->
<context:annotation-config />
<context:component-scan base-package="com.example.dubboprovider" />
<!-- 提供方应用信息 -->
<dubbo:application name="dubboprovider"></dubbo:application>
<!-- 使用zookeepeer暴露服务地址 (还可以通过multicast广播注册中心暴露服务地址)-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 用dubbo协议在20880端口暴露服务地址 -->
<dubbo:protocol accesslog="/data/logs/access.log" name="dubbo" server="netty" host="127.0.0.1" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.demo.ISayHelloInterface" protocol="dubbo" ref="dubboProvider" version="1.0.0"/>
</beans>
在.properties文件中配置
#需要启动的容器
dubbo.container=spring
#查看dubbo文档,Dubbo是通过JDK的ShutdownHook来完成优雅停机的:
#http://dubbo.io/User+Guide-zh.htm#UserGuide-zh-%E4%BC%98%E9%9B%85%E5%81%9C%E6%9C%BA
#但能实现优雅停机的前提是,在启动时,需要指定参数-Ddubbo.shutdown.hook=true
dubbo.shutdown.hook=true
#dubbo是基于spring的,指定spring配置文件
dubbo.spring.config=spring.xml
实现ISayHelloInterface接口的sayHello()方法
public class DubboProvider implements ISayHelloInterface {
@Override
public String sayHello(String s) {
return "Provider接收到信息了,你好," + s;
}
}
5、新建一个消费者(ConsumerTest)
在pom.xml文件中增加的配置与第四部一样
在spring.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee"
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-3.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 消费方应用信息 -->
<dubbo:application name="dubboconsumer"></dubbo:application>
<!-- 到zookeepeer获取服务 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 消费方用什么协议获取服务(用dubbo协议在20880端口获取服务) -->
<dubbo:protocol accesslog="/data/logs/access.log" name="dubbo" server="netty" host="127.0.0.1" port="20880" />
<!-- 声明需要获取的服务接口 这里是reference,提供者是service-->
<dubbo:reference id="sayHello" interface="com.demo.ISayHelloInterface" protocol="dubbo" version="1.0.0"/>
</beans>
调用生产者接口
public class ConsumerTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
ISayHelloInterface sayHello = (ISayHelloInterface)ctx.getBean("sayHello");//获取spring.xml配置的bean
System.out.println("Consumer 调用sayHello服务:");
System.out.println("提供者返回结果:"+sayHello.sayHello("testUser"));//调用方法
}
}