前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >dubbo框架搭建

dubbo框架搭建

作者头像
week
发布2018-08-24 15:10:28
7170
发布2018-08-24 15:10:28
举报
文章被收录于专栏:用户画像用户画像

一、原理

dubbo官网:http://dubbo.io/

Dubbo offers three key functionalities, which include interface based remote call, fault tolerance & load balancing, and automatic service registration & discovery.

Dubbo有三个主要功能

1、远程调用

2、PRC:容错机制和负载均衡

3、服务目录框架:用于服务的注册和服务事件发布和订阅

源码地址:https://github.com/jxq0816/dubbo_demo

二、dubbo项目组织结构

三、编码

1、dubbo-demo 

pom.xml

代码语言:javascript
复制
<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.weeking</groupId>
    <artifactId>dubbo-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>dubbo-server</module>
        <module>dubbo-client</module>
    </modules>
    <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <!-- dubbo begin -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
        <!-- dubbo end -->
        <!-- 注册中心zookeeper begin -->
        <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.5.3-beta</version>
            <type>pom</type>
        </dependency>
        <!--A zookeeper client, that makes life a little easier.-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.8</version>
        </dependency>
    </dependencies>

    </dependencyManagement>
</project>

2. dubbo-server

① pom.xml

代码语言:javascript
复制
<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>dubbo</groupId>
    <artifactId>dubbo-server</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>com.weeking</groupId>
        <artifactId>dubbo-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <!-- spring begin -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <!-- spring end -->

        <!-- dubbo begin -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <!-- dubbo end -->

        <!-- 注册中心zookeeper begin -->
        <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <type>pom</type>
        </dependency>

        <!-- 注册中心zookeeper end -->

        <!-- The Netty project is an effort to provide an asynchronous event-driven network application framework
        and tools for rapid development of maintainable high performance and high scalability protocol servers and clients.
        In other words, Netty is a NIO client server framework which enables quick and easy development of network applications
         such as protocol servers and clients.
         It greatly simplifies and streamlines network programming such as TCP and UDP socket server.
        -->
        <dependency>
            <groupId>org.jboss.netty</groupId>
            <artifactId>netty</artifactId>
            <version>3.2.0.Final</version>
        </dependency>
        <!--A zookeeper client, that makes life a little easier.-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
    </dependencies>

</project>

② ServerMain

代码语言:javascript
复制
package com.jxq.main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * Created by jiangxingqi on 2017/2/8.
 */
public class ServerMain {
    public static void main(String[] args) throws IOException {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationProvider.xml" });
        context.start();
        System.out.println("输入任意按键退出 ~ ");
        System.in.read();
        context.close();
    }
}

③ DubboService

代码语言:javascript
复制
package com.jxq.service;

/**
 * Created by jiangxingqi on 2017/2/8.
 */
public interface DubboService {
    String sayHello(String name);
}

④DubboServiceImpl

代码语言:javascript
复制
package com.jxq.service.impl;

import com.jxq.service.DubboService;

/**
 * Created by jiangxingqi on 2017/2/8.
 */
public class DubboServiceImpl implements DubboService {
    public String sayHello(String name) {
        System.out.println("init : " + name);
        return "hello " + name;
    }
}

⑤ applicationProvider.xml

代码语言:javascript
复制
<?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="dubbo-demo" />
    <!-- zookeeper注册中心 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.jxq.service.impl.DubboServiceImpl" />

    <!-- 向注册中心注册暴漏服务地址,注册服务 -->
    <dubbo:service interface="com.jxq.service.DubboService"
                   ref="demoService" executes="10" />

</beans>

3. dubbo-client

①ClientMain

代码语言:javascript
复制
package com.jxq.service;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by jiangxingqi on 2017/2/8.
 */
public class ClientMain {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationConsumer.xml" });
        context.start();
        DubboService service = (DubboService) context.getBean("demoService");
        System.out.println(service.sayHello("world"));
        context.close();
    }
}

②applicationConsumer.xml

代码语言:javascript
复制
<?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-of-dubbo-demo" />

    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 向注册中心订阅服务 -->
    <dubbo:reference id="demoService" interface="com.jxq.service.DubboService" />
</beans>

四、联调

启动zookeep

consumer请求server所提供的sayHello接口

1、run ServerMain 启动服务提供方

2、run ClientMain 启动消费方

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年11月23日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
微服务引擎 TSE
微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档