dubbo服务简单搭建
一、初识dubbo:
架构图:
Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次调和调用时间的监控中心。
Container: 服务运行容器。
流程:
服务容器负责启动,加载,运行服务提供者。
服务提供者在启动时,向注册中心注册自己提供的服务。
服务消费者在启动时,向注册中心订阅自己所需的服务。
注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
注册中心选择:
dubbo支持多种类型的注册中心:
- Multicast注册中心
- Zookeeper注册中心
- Redis注册中心
- Simple注册中心
二、ZooKeeper
下载:https://archive.apache.org/dist/zookeeper/
windows安装:https://blog.csdn.net/tlk20071/article/details/52028945
三、服务Demo搭建
1、新建一个maven工程:结构如下
其中模块dubbo-api用于给提供服务模块dubbo-provider和消费服务dubbo-cosumer提供公用接口,各目录结构为:
模块dubbo-api简单测试代码如下:
package com.example.demo; public interface DemoService {
String testService();
}
提供服务模块dubbo-provider:
pom文件:
<?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.example</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>dubbo-provider</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- dubbo-api依赖 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- dubbo依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.10</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.3</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
实现接口类:
package com.example.demo.impl; import com.example.demo.DemoService; public class DemoServiceimpl implements DemoService {
@Override
public String testService() {
return "provider testService";
}
}
启动类:
package com.example.demo.provider; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class Provider {
public static void main(String[] args) {
//测试
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"classpath*:provider.xml");
context.start(); System.out.println("dubbo service begin to start");
try {
System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
配置文件:
<?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-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->
<dubbo:application name="demo-provider" owner="programmer" organization="dubbox"/>
<!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!--使用 dubbo 协议实现定义好的 api.PermissionService 接口-->
<dubbo:service interface="com.example.demo.DemoService" ref="demoService" protocol="dubbo" />
<!--具体实现该接口的 bean-->
<bean id="demoService" class="com.example.demo.impl.DemoServiceimpl"/>
</beans>
消费服务dubbo-cosumer:
pom文件:
<?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.example</groupId>
<artifactId>duboo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>duboo-consumer</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- dubbo-api -->
<dependency>
<groupId>com.example</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- dubbo依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.10</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- zkclient依赖 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.3</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
启动类:
package com.example.demo.consumer; import com.example.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Consumer {
public static void main(String[] args) {
//测试服务
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("consumer.xml");
context.start();
System.out.println("consumer start");
DemoService demoService = context.getBean(DemoService.class);
System.out.println("consumer");
System.out.println(demoService.testService());
}
}
配置文件:
<?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="demo-consumer" owner="programmer" organization="dubbox"/>
<!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--使用 dubbo 协议调用定义好的 api.PermissionService 接口-->
<dubbo:reference id="demoService" interface="com.example.demo.DemoService"/>
</beans>
四、测试:
zookeeper服务启动后,先启动服务提供方dubbo-provider,再运行dubbo-consuner,效果如下:
源码参照Github
dubbo服务简单搭建的更多相关文章
- dubbo服务自动化测试搭建
java实现dubbo的消费者服务编写:ruby实现消费者服务的接口测试:通过消费者间接测试dubbo服务接口的逻辑 内容包括:dubbo服务本地调用环境搭建,dubbo服务启动,消费者部署,脚本编写 ...
- Dubbo服务的搭建
dubbo框架主要作用是基于RPC的远程调用服务管理,但是注册中心是用的zookeeper,搭建dubbo,首先要安装zookeeper,配置zookeeper... 实现功能如图所示:(存在2个系统 ...
- Dubbo服务的搭建与使用
官方地址Dubbo.io Dubbo 主要功能 高并发的负载均衡,多系统的兼容合并(理解不深,不瞎掰了) Dubbo 主要组成有四部分 Zookeeper(服务注册中心) Consumer(服务消费方 ...
- DHCP服务简单搭建步骤
服务端:sishen_63 IP:192.168.1.63 客户端:sishen_64 IP:192.168.1.64 此外,因为本实验实在虚拟机中做的,所以对虚拟机还要做如下设置: 服务 ...
- Dubbo——基于Zookeeper服务框架搭建及案例演示
一.了解SOA微服务架构 在大规模服务化之前,应用可能只是通过RMI或Hessian等工具,简单的暴露和引用远程服务,通过配置服务的URL地址进行调用,通过F5等硬件进行负载均衡. (1) 当服务越来 ...
- Dubbo学习笔记2:Dubbo服务提供端与消费端应用的搭建
Demo结构介绍 Demo使用Maven聚合功能,里面有三个模块,目录如下: 其中Consumer模块为服务消费者,里面TestConsumer和consumer.xml组成了基于Spring配置方式 ...
- Dubbo入门介绍---搭建一个最简单的Demo框架
Dubbo入门---搭建一个最简单的Demo框架 置顶 2017年04月17日 19:10:44 是Guava不是瓜娃 阅读数:320947 标签: dubbozookeeper 更多 个人分类: D ...
- windows环境搭建dubbo服务
windows环境搭建dubbo服务 1:首先需要下载dubbo的注册中心 zookeeper zookeeper注册中心下载地址链接:http://mirror.bit.edu.cn/apache/ ...
- 简单搭建dubbo
为什么要用dubbo? 当网站规模达到了一定的量级的时候,普通的MVC框架已经不能满足我们的需求,于是分布式的服务框架和流动式的架构就凸显出来了. 单一应用架构 当网站流量很小时,只需一个应用 ...
随机推荐
- Java中的throw和throws的区别
Java中的throw和throws的区别 1.throw关键字用于方法体内部,而throws关键字用于方法体部的方法声明部分: 2.throw用来抛出一个Throwable类型的异常,而throws ...
- nest exception is java.sql.SQLException:ORA-01476:除数为0
1.错误描述 nest exception is java.sql.SQLException:ORA-01476:除数为0 2.错误原因 3.解决办法
- 7.C++类与封装的概念
类通常分为以下两部分 -类的内部具体实现 -类的外部使用方法 比如: 用户使用手机,只需要知道如何使用. 而手机开发者,则需要考虑手机内部的实现细节. 类的封装 并不是类的每个成员变量和成员函数都要对 ...
- Python与Mongodb交互
MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统 MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案 MongoDB 将数据存储为一个文档,数据结构由键值 ...
- 机器学习相关的tutorial
1. MRF 马尔可夫随机场 http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/AV0809/ORCHARD/ 从MRF,讲到Gibbs分 ...
- 在Visual Studio 2012中使用GSL
1. 下载GSL http://gnuwin32.sourceforge.net/packages/gsl.htm 下载 Complete package, except sources和Source ...
- 异常-----freemarker.core.ParseException: Unexpected end of file reached
freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Unexpected end of file reached. at freemarker ...
- Javascript设计模式(2)-单体模式
单体模式 1. js最简单的单体模式 对象字面量:把一批有一定关联的方法和属性组织在一起 // 对象字面量 var Singleton = { attr1: true, attr2: 10, meth ...
- type="submit" button的用法
原因:关于页面刷新,或者点击某个按钮,开始能现实值,但几秒钟就会刷新掉的原因,这是因为sunmit 和button自带刷新属性,所以说少用就好,或者用returen fales 来断掉接下来的操作,当 ...
- css3动画结束捕捉事件整理
//捕捉webkitAnimationEnd事件 element.addEventListener('webkitAnimationEnd', end, false); //捕捉webkitTrans ...