Hello Dubbo
来源:互联网 发布:融资租赁渠道 知乎 编辑:程序博客网 时间:2024/05/21 21:50
概述
Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点,自开源后,已有不少非阿里系公司在使用Dubbo,所以我也得学习学习。
那么,Dubbo是什么?
随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用框架已无法应对,分布式服务框架以及流动计算架构势在必行。
单一应用架构
● 当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。
● 此时,用于简化增删改查工作量的 数据访问框架(ORM) 是关键。
垂直应用架构
● 当访问量逐渐增大,单一应用增加机器带来的加速度越来越小,将应用拆成互不相干的几个应用,以提升效率。
● 此时,用于加速前端页面开发的 Web框架(MVC) 是关键。
分布式服务架构
● 当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务 中心,使前端应用能更快速的响应多变的市场需求。
● 此时,用于提高业务复用及整合的 分布式服务框架(RPC) 是关键。
流动计算架构
● 当服务越来越多,容量的评估,小服务资源的浪费等问题逐渐显现,此时需增加一个调度中心基于访问压力实 时管理集群容量,提高集群利用率。
● 此时,用于提高机器利用率的 资源调度和治理中心(SOA) 是关键。
Dubbo |ˈdʌbəʊ| 是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。 其核心部分包含:
● 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
● 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持
● 自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。
Dubbo能做什么?
● 透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。
● 软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
● 服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。
Dubbo Hello World
概述是照抄官网,代码当然也是官网的了,照着官方提供的 dubbo-demo 敲了一遍(https://github.com/alibaba/dubbo)
准备工作:idea+maven+jdk
1.创建maven项目(父项目)
添加依赖 pom.xml
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <modules> <module>dubbo-hello-server</module> <module>dubbo-hello-consumer</module> <module>dubbo-hello-provider</module> </modules> <groupId>priv.starfish</groupId> <artifactId>testDubbo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>testDubbo Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> </dependencies> <build> <finalName>testDubbo</finalName> </build></project>
2.创建子项目(模块)
(和创建普通的maven项目一样一样的,
dubbo-hello-server:中间件,远程服务接口,也可以写到生产者模块中;
dubbo-hello-provider:生产者,provider发布远程服务到注册中心;
dubbo-hello-consumer:消费者,consumer自动发现远程服务并完成服务调用)
项目的层次结构是介个样子的
3.开始附代码:
dubbo-hello-server模块:
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>testDubbo</artifactId> <groupId>priv.starfish</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <packaging>jar</packaging> <artifactId>dubbo-hello-server</artifactId> <!-- 这是干嘛的 不懂--> <properties> <skip_maven_deploy>true</skip_maven_deploy> </properties></project>
HelloService(定义服务接口)
package priv.starfish.dubbo.hello;/** * Created by starfish on 2017/9/5. */public interface HelloService { String sayHello(String name);}
dubbo-hello-provider模块:
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>testDubbo</artifactId> <groupId>priv.starfish</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo-hello-provider</artifactId> <dependencies> <dependency> <groupId>priv.starfish</groupId> <artifactId>dubbo-hello-server</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies></project>
HelloServiceImpl(在服务提供方实现接口)
package priv.starfish.dubbo.hello;/** * Created by starfish on 2017/9/5. */public class HelloServiceImpl implements HelloService { public String sayHello(String name) { System.out.println("Hello Provider-----"+name); name = "starfish begin dubbo hello"+name; return name; }}
Provider(加载Spring配置)
package priv.starfish.dubbo.hello;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Created by starfish on 2017/7/31. */public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-hello-provider.xml"}); context.start(); System.in.read(); // 按任意键退出 }}
dubbo-hello-provider.xml(用 Spring 配置声明暴露服务)
<?xml version="1.0" encoding="UTF-8"?><!-- - Copyright 1999-2011 Alibaba Group. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License.--><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="dubbo-hello-provider"/> <!-- 使用multicast广播注册中心暴露服务地址 --> <dubbo:registry address="multicast://224.5.6.7:1234"/> <!-- zookeeper注册中心 --> <!-- <dubbo:registry address="zookeeper://127.0.0.1:2181" />--> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880"/> <!-- 声明需要暴露的服务接口 --> <bean id="helloService" class="priv.starfish.dubbo.hello.HelloServiceImpl"/> <!-- 和本地bean一样实现服务 --> <dubbo:service interface="priv.starfish.dubbo.hello.HelloService" ref="helloService"/></beans>
dubbo-hello-consumer模块:
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>testDubbo</artifactId> <groupId>priv.starfish</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo-hello-consumer</artifactId> <dependencies> <dependency> <groupId>priv.starfish</groupId> <artifactId>dubbo-hello-server</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies></project>
Consumer(加载Spring配置,并调用远程服务,可以用IOC注入)
package priv.starfish.dubbo.hello;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Created by starfish on 2017/9/5. */public class Consumer { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-hello-consumer.xml"}); context.start(); HelloService helloService = (HelloService) context.getBean("helloService"); // 获取远程服务代理 String hello = helloService.sayHello("world"); // 执行远程方法 System.out.println(hello); // 显示调用结果 }}
dubbo-hello-consumer.xml(通过Spring配置引用远程服务)
<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> <dubbo:application name="dubbo-hello-consumer"/> <!-- 使用multicast广播注册中心暴露发现服务地址 --> <dubbo:registry address="multicast://224.5.6.7:1234"/> <!-- 使用 zookeeper 广播注册中心暴露发现服务地址 --> <!--<dubbo:registry address="zookeeper://127.0.0.1:2181" />--> <!-- 生成远程服务代理,可以和本地bean一样使用helloService --> <dubbo:reference id="helloService" check="false" interface="priv.starfish.dubbo.hello.HelloService"/></beans>
4.运行:
先启动生产方,再启动消费方(运行provider的main方法后,再运行consumer的main方法,哦了)
- Hello Dubbo
- 1.Hello Dubbo
- dubbo hello word
- dubbo hello world
- dubbo hello world
- dubbo入门--Hello World
- dubbo hello world
- dubbo(一)dubbo+zookeeper 之 Hello World
- dubbo(一)dubbo+zookeeper 之 Hello World
- Dubbo入门详解之Hello World
- dubbo
- Dubbo
- dubbo
- dubbo
- dubbo
- dubbo
- Dubbo
- dubbo
- 设计模式之单例模式
- 正则表达式的基本用法
- 百度地图android
- windows查看内存的频率等详细信息
- VirtualBox中安装CentOS 7
- Hello Dubbo
- oracle11.2.0.4.0 配置dg(lgwr+async)笔记
- MySQL删除列,添加列的sql语句
- Unity获取模型的顶点坐标
- angularJs 表格添加删除修改查询
- 深度解析vivoX20四大创新突破!小米Mix2望尘莫及,雷军慌了
- CteX与LaTeX有什么区别
- Python numpy函数:hstack()、vstack()、stack()、dstack()、vsplit()、concatenate()
- win10激活失败遇到了0x8007007B错误