Dubbo-基础应用

随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进,Dubbo应运而生。

架构

Dubbo-基础应用_第1张图片
上图为dubbo结合注册中心的架构(也有单点模式),服务提供方(Provider)将所有的服务注册到注册中心(Registry),服务消费方(Consumer)通过访问注册中心取得可用的服务列表,然后调用服务。

后台管理部署

源码下载

https://github.com/alibaba/dubbo

控制台部署

Dubbo是一个多模块的maven项目,其中dubbo-admin是控制台。
- 修改dubbo-admin模块下的dubbo.properties的配置,如

dubbo.registry.address=zookeeper://xxx.xxx.xxx.xxx:2181;zookeeper://xxx.xxx.xxx.xxx:2182;zookeeper://xxx.xxx.xxx.xxx:2183
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
  • 部署到web容器,启动即可
    Dubbo-基础应用_第2张图片

实现简单dubbo服务

分别建立三个模块


    <module>dubbo-api</module>
    <module>dubbo-provider</module>
    <module>dubbo-consumer</module>
</modules>

dubbo-api

  • 定义接口
public interface IHelloWorldService {

    public String sayHello(String name);
}
  • pom配置
<artifactId>dubbo-apiartifactId>

dubbo-provider

  • pom配置
<dependencies>
        <dependency>
            <groupId>com.bdonggroupId>
            <artifactId>dubbo-apiartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
            <version>2.5.3version>
        dependency>
        
        <dependency>
            <groupId>org.apache.zookeepergroupId>
            <artifactId>zookeeperartifactId>
            <version>3.4.9version>
        dependency>
        
        <dependency>
            <groupId>com.github.sgroschupfgroupId>
            <artifactId>zkclientartifactId>
            <version>0.1version>
        dependency>
        
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>
    dependencies>
  • 实现接口
public class HelloWorldServiceImpl implements IHelloWorldService {

    public String sayHello(String name) {
        return "Hello World : "+ name;
    }
}
  • 配置文件

<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="hello-world-provider"/>
    
    <dubbo:registry address="xxx.xxx.xxx.xxx:2181" protocol="zookeeper"/>
    
    
    
    <dubbo:protocol name="dubbo" port="20881"/>
    
    <dubbo:service interface="com.bdong.dubbo.api.IHelloWorldService" ref="demoService" protocol="dubbo"/>

    <bean id="demoService" class="com.bdong.dubbo.provider.HelloWorldServiceImpl"/>

beans>
  • 启动服务
public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"provider.xml"});
        context.start();
        //保持服务可用
        System.in.read();
    }

dubbo-consumer

  • pom配置同dubbo-provider
  • 配置文件

    <dubbo:application name="hello-world-provider"/>
    
    <dubbo:registry address="xxx.xxx.xxx.xxx:2181" protocol="zookeeper"/>
    
    
    
    <dubbo:protocol name="dubbo" port="20881"/>
    
    <dubbo:reference interface="com.bdong.dubbo.api.IHelloWorldService" id="demoService" protocol="dubbo"/>
  • 调用
public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"consumer.xml"});
        IHelloWorldService helloWorldService = (IHelloWorldService) context.getBean("demoService");
        System.out.println(helloWorldService.sayHello("man!"));
    }
  • 结果
Hello World : man!

你可能感兴趣的:(Dubbo,Distribute)