生產者和消費者

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"));//調用方法
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章