快速搭建一个简单的dubbo示例

目录

  • 一、服务端
    • 1、导入依赖
    • 2、ProviderService和ProviderServiceImpl
    • 3、provider.xml
    • 4、启动类
  • 二、消费端
    • 1、添加依赖
    • 2、consumer.xml
    • 3、启动类
  • 三、加入zookeeper作为注册中心
    • 1、zookeeper安装
    • 2、修改provider.xml
    • 3、修改consumer.xml
    • 4、运行两个Application,注意要先开启zookeeper服务。
  • 四、API配置方式
    • 1、服务端的启动类
    • 2、消费端的启动类
  • 五、注解配置方式
    • 1、给ProviderServiceImpl添加注解 @Service
    • 2、组装服务端
    • 3、服务端启动类
    • 4、消费端的service
    • 5、组装消费者
    • 6、消费者启动类

源代码已上传 github:https://github.com/zhsun1995/dubbo_test

一、服务端

1、导入依赖

<?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>dubbotest</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo_provider</artifactId>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.10</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.5</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.32.Final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.8.0</version>
        </dependency>

    </dependencies>

</project>

2、ProviderService和ProviderServiceImpl

public interface ProviderService {
    String sayHello(String name);
}

public class ProviderServiceImpl implements ProviderService {

    public String sayHello(String name) {
        return "Hello " + name;
    }
}

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

    <!--当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签-->
    <dubbo:application name="provider" owner="laosun">
        <dubbo:parameter key="qos.enable" value="true"/>
        <dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
        <dubbo:parameter key="qos.port" value="55555"/>
    </dubbo:application>

    <dubbo:monitor protocol="registry"/>

    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
    <!--<dubbo:registry address="N/A"/>-->
    <dubbo:registry address="N/A" />

    <!--当前服务发布所依赖的协议;webserovice、Thrift、Hessain、http-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--服务发布的配置,需要暴露的服务接口-->
    <dubbo:service
            interface="com.laosun.service.ProviderService"
            ref="providerService"/>

    <!--Bean bean定义-->
    <bean id="providerService" class="com.laosun.service.impl.ProviderServiceImpl"/>

</beans>

4、启动类

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class Application {
    public static void main( String[] args ) throws IOException, IOException {
        //加载xml配置文件启动
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        context.start();
        System.in.read(); // 按任意键退出
    }
}

注意添加log4j.xml。
运行后,
在这里插入图片描述
注意图中dubbo的url,后续要用。

二、消费端

1、添加依赖

<?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>dubbotest</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo_consumer</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>dubbo_provider</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.10</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.5</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.32.Final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.8.0</version>
        </dependency>
    </dependencies>
</project>

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

    <!--当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签-->
    <dubbo:application name="consumer" owner="sihai"/>

    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
    <!--点对点的方式-->
    <dubbo:registry address="N/A" />
    <!--<dubbo:registry address="zookeeper://localhost:2181" check="false"/>-->

    <!--生成一个远程服务的调用代理-->
    <!--点对点方式-->
    <dubbo:reference id="providerService"
                     interface="com.laosun.service.ProviderService"
                     url="dubbo://10.4.16.50:20880/com.laosun.service.ProviderService"/>

    <!--<dubbo:reference id="providerService"
                     interface="com.sihai.dubbo.provider.service.ProviderService"/>-->
</beans>

这里用到了服务端产生的dubbo的url。

3、启动类

package com.laosun.consumer;

import com.laosun.service.ProviderService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class Application {
    public static void main( String[] args ) throws IOException {

        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        ProviderService providerService = (ProviderService) context.getBean("providerService");
        String str = providerService.sayHello("laosun");
        System.out.println(str);
        System.in.read();

    }
}

运行之后,出现
在这里插入图片描述
点对点方式搭建完成。
快速搭建一个简单的dubbo示例_第1张图片

三、加入zookeeper作为注册中心

1、zookeeper安装

(1)官网下载链接,本文使用的是3.4.14版本,下载后解压。
(2)复制zookeeper-3.4.14\conf目录下的zoo_sample.cfg并改名为zoo.cfg,对其zoo.cfg配置进行修改:
dataDir=C:/ZHSUN/zookeeper-3.4.14/data
(3)cmd命令进入bin目录,输入命令:
zkServer.cmd
打开服务端
在这里插入图片描述

2、修改provider.xml

只需修改一句,将

<dubbo:registry address="N/A" />

修改为:

<dubbo:registry address="zookeeper://localhost:2181" check="false"/>

如果使用zookeeper,则改为:

<dubbo:registry protocol="zookeeper" address="192.168.11.129:2181,192.168.11.137:2181,192.168.11.138:2181"/>

3、修改consumer.xml

(1)同provider.xml一样:

<dubbo:registry address="zookeeper://localhost:2181" check="false"/>

(2)使用zookeeper时,reference就不需要dubbo的url,改为:

<dubbo:reference id="providerService"
                 interface="com.laosun.service.ProviderService"/>

4、运行两个Application,注意要先开启zookeeper服务。

同样运行成功。
在这里插入图片描述

四、API配置方式

此方式不需要任何配置文件,只需要开启zookeeper,运行下面两个类即可。

1、服务端的启动类

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import com.laosun.service.ProviderService;
import com.laosun.service.impl.ProviderServiceImpl;

import java.io.IOException;

public class AppApi {
    public static void main(String[] args) throws IOException {

        // 生成服务端对象
        ProviderService providerService = new ProviderServiceImpl();

        // 当前应用配置
        ApplicationConfig application = new ApplicationConfig();
        application.setName("provider");
        application.setOwner("laosun");

        // 连接注册中心
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://127.0.0.1:2181");

        // 服务提供者协议配置
        ProtocolConfig protocol = new ProtocolConfig();
        protocol.setName("dubbo");
        protocol.setPort(20880);

        // ServiceConfig内部封装了与注册中心的连接,以及开启服务端口,实例很重,正式应用需要进行缓存
        // 服务者暴露服务配置
        ServiceConfig<ProviderService> service = new ServiceConfig<ProviderService>();
        service.setApplication(application);
        service.setRegistry(registry);
        service.setProtocol(protocol);
        service.setInterface(ProviderService.class);
        service.setRef(providerService);
        service.setVersion("1.0.0");

        // 暴露服务及注册
        service.export();

        System.in.read();
    }
}

2、消费端的启动类

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.laosun.service.ProviderService;
import com.laosun.service.impl.ProviderServiceImpl;

public class AppApi {
    public static void main(String[] args) {
        ApplicationConfig application = new ApplicationConfig();
        application.setName("consumer");
        application.setOwner("laosun");

        // 配置连接中心
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://127.0.0.1:2181");

        // 引用远程服务
        ReferenceConfig<ProviderService> reference = new ReferenceConfig<ProviderService>(); //实例很重,需要进行缓存
        reference.setApplication(application);
        reference.setRegistry(registry);
        reference.setInterface(ProviderService.class);

        // 使用服务端
        ProviderService providerService = new ProviderServiceImpl();
        String str = providerService.sayHello("laosun");
        System.out.println(str);
    }
}

在这里插入图片描述

五、注解配置方式

1、给ProviderServiceImpl添加注解 @Service

在这里插入图片描述

2、组装服务端

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableDubbo(scanBasePackages = "com.laosun.service")
public class DubboProviderConfig {
    @Bean
    // 服务端信息配置
    public ProviderConfig providerConfig() {
        ProviderConfig config = new ProviderConfig();
        config.setTimeout(1000);
        return config;
    }

    @Bean
    // 分布式应用信息配置
    public ApplicationConfig applicationConfig() {
        ApplicationConfig config = new ApplicationConfig();
        config.setName("provider");
        return config;
    }

    @Bean
    // 注册中心信息配置
    public RegistryConfig registryConfig() {
        RegistryConfig config = new RegistryConfig();
        config.setProtocol("zookeeper");
        config.setAddress("127.0.0.1");
        config.setPort(2181);
        return config;
    }

    @Bean
    // 使用dubbo协议配置
    public ProtocolConfig protocolConfig() {
        ProtocolConfig config = new ProtocolConfig();
        config.setName("dubbo");
        config.setPort(20881);
        return  config;
    }
}

3、服务端启动类

import com.laosun.config.DubboProviderConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.io.IOException;

public class AppAnnotation {
    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DubboProviderConfig.class);
        context.start();
        System.in.read();
    }
}

启动成功:
在这里插入图片描述

4、消费端的service

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

@Component
public class ConsumerAnnotationService {
    @Reference
    private ProviderService providerService;

    public String sayHello(String name) {
        return providerService.sayHello(name);
    }
}

这里注意Reference包不要引用错了,不然会报NPE异常。

5、组装消费者

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableDubbo(scanBasePackages = "com.laosun.service")
@ComponentScan(value = {"com.laosun.service"})
public class DubboConsumerConfig {
    @Bean
    // 应用配置
    public ApplicationConfig applicationConfig() {
        ApplicationConfig config = new ApplicationConfig();
        config.setName("consumer");
        Map<String, String> map = new HashMap<String, String>();
        map.put("qos.enable", "true");
        map.put("qos.accept.foreign.ip", "false");
        map.put("qos.port", "33333");
        config.setParameters(map);
        return config;
    }

    @Bean
    // 消费者配置
    public ConsumerConfig consumerConfig() {
        ConsumerConfig config = new ConsumerConfig();
        config.setTimeout(5000);
        return config;
    }

    @Bean
    // 配置注册中心
    public RegistryConfig registryConfig() {
        RegistryConfig config = new RegistryConfig();
        config.setProtocol("zookeeper");
        config.setAddress("127.0.0.1");
        config.setPort(2181);
        return config;
    }
}

6、消费者启动类

import com.laosun.config.DubboConsumerConfig;
import com.laosun.service.ConsumerAnnotationService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.io.IOException;

public class AppAnnotation {

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DubboConsumerConfig.class);
        context.start();
        ConsumerAnnotationService service = context.getBean(ConsumerAnnotationService.class);
        String str = service.sayHello("laosun");
        System.out.println(str);
        System.in.read();
    }
}

运行成功:
在这里插入图片描述
参考链接:https://segmentfault.com/a/1190000019896723

你可能感兴趣的:(实例用法,java,分布式,zookeeper)