2.Hello World及注册中心的选择使用

2018-10-27  本文已影响0人  小manong

一、spring整合Dubbo使用

1.创建spring项目(这里使用springBoot)

最终项目结构
 <modules>
        <module>api</module>
        <module>server</module>
        <module>consumer</module>
    </modules>
    <properties>
        <!-- 使用UTF-8编码  -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!-- 使用jdk1.8版本  -->
        <java.version>1.8</java.version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--热启动:每自修改后, 程序自动启动spring Application上下文。 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
<!--dubbo文件-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
    </dependencies>

2.api模块

public interface HelloService {
    /**
     * 服务方法
     */
    public String say();
}

3.provider服务创建模块

(1)添加依赖,依赖于api模块及spring、dubbo等框架,这里父工程提供了

    <dependencies>
        <dependency>
            <artifactId>api</artifactId>
            <groupId>com.qiu</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

(2)创建接口实现类

public class HelloServiceImpl implements HelloService {
    @Override
    public String say() {
        System.out.println("服务执行,是在服务端=======================");
        return  "服务执行的结果,hello world";
    }
}

(3)创建dubbo配置文件provider.xml,这里注册中心使用multicast广播注册中心

<?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://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

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

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.qiu.service.HelloService" ref="helloService" />

    <!-- 和本地bean一样实现服务 -->
    <bean id="helloService" class="com.qiu.service.impl.HelloServiceImpl" />
</beans>

(4)创建测试方法

public class AppTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        HelloService helloService = (HelloService) context.getBean("helloService");
        System.out.println("服务启动成功===========");
        //是程序循环等待中,模拟不断运行的生成环境
        try {
            System.in.read();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

4.consumer服务消费模块

(1)和服务创建模块一样依赖于api模块及spring、dubbo等框架

 <dependencies>
        <dependency>
            <artifactId>api</artifactId>
            <groupId>com.qiu</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

(2)创建consumer.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://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!--配置一个服务名  -->
    <dubbo:application name="clientServer"/>
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />
    <!-- 引用服务 ,id是为了获取服务用-->
    <dubbo:reference interface="com.qiu.service.HelloService"  id="helloService" ></dubbo:reference>
</beans>

(3)进行服务消费

public class AppSpringTest {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("classpath:consumer.xml");
        HelloService helloService=(HelloService) context.getBean("helloService");
        System.out.println("服务消费=========");
        String result = helloService.say();
        System.out.println(result);
    }
}

4.进行测试

二、使用zookeeper作为注册中心(官方推荐)

1.Zookeeper简介

2.window安装zookeeper

(1)进入zookeeper官网:http://mirror.bit.edu.cn/apache/zookeeper/下载安装包并解压到D盘目录下面。
(2)修改一些常用的配置

# 存在内存中的数据的快照位置
dataDir=D:/zookeeper-3.4.5/data
#存储错误日志的位置
dataLogDir=D:/zookeeper-3.4.5/log
# zookeeper的端口号
clientPort=2181

(3)启动zookeeper,看看是否可以用

3.dubbo中使用zookeeper为注册中心

(1)引入依赖(服务模块和客户端模块)

<dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

(2)修改配置文件注册中心为zookeeper

 <dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:registry address="zookeeper://10.20.153.10:2181?
backup=10.20.153.11:2181,10.20.153.12:2181" />
或者
<dubbo:registry protocol="zookeeper" 
address="10.20.153.10:2181,10.20.153.11:2181,10.20.153.12:2181" />

三、dubbo官方推荐配置配置文件

四、相关问题解决部分

1.多接口解决

上一篇 下一篇

猜你喜欢

热点阅读