欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Dubbo、Spring、Zookeeper、集成基础案例(注解方式)

程序员文章站 2022-05-07 14:23:55
...

摘要:最近抽时间系统的学习了Dubbo的一些内容,趁有时间,整理下,顺便记录下,以防以后回顾。

一:运行环境

1>:JDK 1.8

2>:IDEA 2018.1

3>:Zookeeper 3.x

4>:Maven 3.2

5>:Dubbo 2.8.4

二:项目结构

Dubbo、Spring、Zookeeper、集成基础案例(注解方式)

三:创建服务提供者工程

pom.xml

<?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">
    <parent>
        <artifactId>micai-dubbo-chapter2</artifactId>
        <groupId>com.micai.dubbo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>micai-dubbo-provider</artifactId>

    <name>micai-dubbo-provider</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <spring.version>4.2.1.RELEASE</spring.version>
        <jdk.version>1.8</jdk.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.8.4</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.3</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
DemoService.java
package com.micai.dubbo.provider;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/9/10 13:33
 * @Description: 定义服务接口
 */
public interface DemoService {

    String sayHello(String name);
}
DemoServiceImpl.java
package com.micai.dubbo.provider;

import com.alibaba.dubbo.config.annotation.Service;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/9/10 13:34
 * @Description: 在服务提供方实现接口
 */
@Service(version = "1.0.0")
public class DemoServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        return "Hello" + name;
    }
}
Provider.java
package com.micai.dubbo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/9/10 14:13
 * @Description:
 */
public class Provider {

    public static void main(String [] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        classPathXmlApplicationContext.start();
        try {
            System.in.read();//按任意键退出
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

applicationContext.xml

<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-4.2.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!--提供方应用信息,用于计算依赖关系-->
    <dubbo:application name="hello-world-app"/>

    <!--使⽤zookeeper⼴播注册中⼼暴露服务地址-->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>

    <!--用dubbo协议在20880端口暴露服务-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--参数回调配置-->
    <!--<dubbo:service interface="com.micai.dubbo.provider.CallBackService" ref="callBackServiceImpl" connections="1" callbacks="1000">
        <dubbo:method name="addListener">
            <dubbo:argument type="com.micai.dubbo.provider.CallBackListener" callback="true"/>
        </dubbo:method>
    </dubbo:service>-->

    <!--&lt;!&ndash;声明需要暴露的服务接口&ndash;&gt;
    <dubbo:service interface="com.micai.dubbo.provider.DemoService" ref="demoService"/>
    &lt;!&ndash;和本地bean一样实现服务&ndash;&gt;
    <bean id="demoService" class="com.micai.dubbo.provider.DemoServiceImpl"/>-->

    <!--扫描注解包路径,多个包⽤逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类-->
    <dubbo:annotation package="com.micai.dubbo.provider"/>

</beans>

四:创建服务消费者工程

pom.xml

<?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">
    <parent>
        <artifactId>micai-dubbo-chapter2</artifactId>
        <groupId>com.micai.dubbo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>micai-dubbo-consumer</artifactId>

    <name>micai-dubbo-consumer</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.2.1.RELEASE</spring.version>
        <jdk.version>1.8</jdk.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.8.4</version>
        </dependency>
        <!--zookeeper-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.3</version>
        </dependency>
        <dependency>
            <groupId>com.micai.dubbo</groupId>
            <artifactId>micai-dubbo-provider</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
DemoAction.java
package com.micai.dubbo.consumer;

import com.alibaba.dubbo.config.annotation.Reference;
import com.micai.dubbo.provider.DemoService;
import org.springframework.stereotype.Component;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/9/11 15:35
 * @Description:
 */
@Component
public class DemoAction {

    // 服务版本号,关闭启动时检查,失败重试次数2次,负载均衡策略轮询,缓存类型
    @Reference(version = "1.0.0")
    private DemoService demoService;

    public String sayHello() {
        return demoService.sayHello("李思慧");
    }

}
Consumer.java
package com.micai.dubbo;

import com.micai.dubbo.consumer.DemoAction;
import com.micai.dubbo.consumer.UserAction;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/9/11 14:36
 * @Description:
 */
public class Consumer {

    public static void main(String [] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        classPathXmlApplicationContext.start();
        DemoAction demoAction = (DemoAction) classPathXmlApplicationContext.getBean("demoAction");
        String str = demoAction.sayHello();
        System.out.println("调用结果" + str);

        /*UserAction userAction = (UserAction) classPathXmlApplicationContext.getBean("userAction");
        userAction.getUserName();
        System.out.println("异步调用结束");*/

        /*CallBackService callBackService = classPathXmlApplicationContext.getBean("callBackService", CallBackService.class);
        callBackService.addListener("zhaoxinguo", new CallBackListener() {
            @Override
            public void changed(String msg) {
                System.out.println("callback:" + msg);
            }
        });
        System.out.println("参数回调");*/
    }
}

applicationContext.xml

<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-4.2.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!--消费方应用名、用于计算依赖关系,不是匹配条件,不要与提供方一样-->
    <dubbo:application name="consumer-of-helloworld-app"/>

    <!--使用zookeeper广播注册中心暴露发现服务地址-->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>

    <!--&lt;!&ndash;生成远程服务代理,可以和本地bean一样使用demoService&ndash;&gt;
    <dubbo:reference id="demoService" interface="com.micai.dubbo.provider.DemoService"/>-->

    <!--扫描注解包路径,多个包⽤逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类-->
    <dubbo:annotation package="com.micai.dubbo.consumer"/>

</beans>

五:运行结果

Dubbo、Spring、Zookeeper、集成基础案例(注解方式)

六:源码下载请扫描下面的QQ交流群获取

Dubbo、Spring、Zookeeper、集成基础案例(注解方式)

相关标签: Dubbo