博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
dubbo之基础应用
阅读量:7071 次
发布时间:2019-06-28

本文共 7905 字,大约阅读时间需要 26 分钟。

  一、Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看,Dubbo采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。关于注册中心、协议支持、服务监控等内容,详见后面描述。

  二、dubbo的基本原理(这个是用于dubbo的工作原理比较重要的一个环节)

  

  这是图中的几个节点的意义

  • Provider:暴露服务的服务提供方,或者直白点说就是服务生产者
  • Consumer:调用远程服务的服务消费方,也就是服务消费者
  • Registry:服务注册与发现的注册中心
  • Monitor:统计服务的调用次数和调用时间的监控中心
  • Container:服务(生产者)运行容器

   具体的调用步骤

  • 0:服务容器负责启动、加载、运行服务提供者(生产者)
  • 1:服务提供者(生产者)在启动时,向注册中心注册自己提供的服务
  • 2:服务消费者在启动时,向注册中心订阅自己所需的服务
  • 3:注册中心返回服务提供者地址列表给消费者,如果有变更,注册中细腻将基于长连接推送变更数据给消费者
  • 4:服务消费者从服务生产者地址列表中,基于软负载均衡算法,选择一台提供者(生产者)进行调用,如果调用失败,再选另一台调用
  • 5:服务消费者和提供者(生产者),在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心

   三、下面我会通过一个例子来说明具体的实现过程,以及配置

  1)说明:dubbo是用来做分布式工程而提出来的一个框架。相对于原来的单体工程来说,dubbo的灵活性,以及调整的方式都更加灵活。dubbo的方式是将service一下的单独提出来做成一个项目,也就是我们提到的SOA模式,面向服务的框架。在设计中,web端只看得到对应的接口,而不需要知道实现过程。具体的实现过程在另外一个项目实现。中间的通行通过zookeeper来实现传输。

  2)我这里写了一个简单的例子来说明这个服务提供的过程,和相对应用独立使用的过程。(目录)

  

  1、do-parent用来加载公用的jar包,没有实际代码

  a、pom.xml

4.0.0
com.troy
do-parent
1.0-SNAPSHOT
do-interface
do-service
do-web
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
org.springframework.boot
spring-boot-starter-web
1.5.9.RELEASE
org.springframework.boot
spring-boot-starter-data-jpa
1.5.9.RELEASE
com.alibaba
dubbo
2.5.7
mysql
mysql-connector-java
5.1.9
org.apache.zookeeper
zookeeper
3.4.9
com.101tec
zkclient
0.10

  2、do-interface用来提供暴露的接口,提供给do-web和do-service。共同的调用方法

  a、目录结构

  

  b、User共同使用的实体类

package com.troy.domain;import javax.persistence.*;import java.io.Serializable;@Entity@Table(name = "USER")public class User implements Serializable{    @Id    @GeneratedValue(strategy = GenerationType.AUTO)    private Long id;    private String name;    private Integer age;    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }}

  c、IUserService只用来提供接口,不做任何实现

package com.troy.service;import com.troy.domain.User;import java.util.List;public interface IUserService {    public List
findAll();}

  d、application-dao.yml的配置用来连接数据库,这个因为我用的是一套数据库,所以公用的数据库配置

spring:  datasource:    driver-class-name: com.mysql.jdbc.Driver    url: jdbc:mysql://localhost:3306/model?useUnicode=true&characterEncoding=UTF-8    password: root    username: root

  3、do-service用来提供具体的接口实现

  a、目录结构

  

 

  b、repository用来,设置基本的数据库访问层

package com.troy.repository;import org.springframework.data.jpa.repository.JpaSpecificationExecutor;import org.springframework.data.repository.NoRepositoryBean;import org.springframework.data.repository.PagingAndSortingRepository;import java.io.Serializable;@NoRepositoryBeanpublic interface BaseRepository
extends PagingAndSortingRepository
,JpaSpecificationExecutor
{}
package com.troy.repository;import com.troy.domain.User;public interface UserRepository extends BaseRepository
{}

  c、service用来写具体的实现过程

package com.troy.service;import com.troy.domain.User;import com.troy.repository.UserRepository;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import javax.transaction.Transactional;import java.util.ArrayList;import java.util.Iterator;import java.util.List;@Service(value = "userService")@Transactionalpublic class UserServiceImpl implements IUserService {    @Autowired    private UserRepository userRepository;    public List
findAll() { List
users = new ArrayList
(); Iterator
iterator = this.userRepository.findAll().iterator(); while (iterator.hasNext()) { users.add(iterator.next()); } return users; }}

  d、config下面是进行相关的配置,对于服务提供者

package com.troy.config;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.ImportResource;@Configuration@ImportResource("classpath:dubbo/*.xml")public class DubboProvider {}

  dubbo-provider.xml的具体配置

  e、具体的启动过程

package com.troy;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import java.util.HashMap;import java.util.Map;@SpringBootApplicationpublic class ApplicationService {    public static void main(String[] args) {        SpringApplication application = new SpringApplication(ApplicationService.class);        Map
map = new HashMap
(); //这里的目的是加入相关配置 map.put("spring.profiles.default","service,dao"); application.setDefaultProperties(map); application.run(args); }}

  4、do-web的目的是用来获取数据,提供数据,当然也可以用来具体页面的呈现

  a、目录接口

  

  b、web,具体的数据提供者

package com.troy.web;import com.troy.service.IUserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Scope;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/api/user")@Scope("prototype")public class UserResource {    @Autowired    private IUserService userService;    @RequestMapping(value = "/findAll")    public Object findAll() {        return this.userService.findAll();    }}

  c、config消费者配置

package com.troy.config;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.ImportResource;@Configuration@ImportResource("classpath:dubbo/*.xml")public class DubboConsumer {}

  dubbo-consumer.xml

  d、启动配置

package com.troy;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import java.util.HashMap;import java.util.Map;@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        SpringApplication application = new SpringApplication(Application.class);        Map
map = new HashMap
(); map.put("spring.profiles.default","web,dao"); application.setDefaultProperties(map); application.run(args); }}

  四、中间需要用到zookeeper,来做注册中心。

  zookeeper的安装部署:

  zookeeper的展示界面:

   五、dubbo的方式,目前各大网站上面应用的非常普遍。当然我这边只是做的一个例子用来,了解dubbo的分布式实现过程,和具体的应用方式。

  代码层面存在不理想的地方,请见谅!!!

  源码下载:

转载地址:http://rbell.baihongyu.com/

你可能感兴趣的文章
update使用inner join
查看>>
Vue2.x中的父子组件相互通信
查看>>
多种替身邮方法总结!
查看>>
沟通比文档更有力
查看>>
[Unity3D]Unity3D游戏开发之角色控制漫谈
查看>>
git branch merge到master
查看>>
EJB--事务管理 .
查看>>
在vmware里面免费安装纯净的xp虚拟机
查看>>
什么是RESTfull?理解RESTfull架构【转】
查看>>
linux lsof命令详解
查看>>
MySQL中concat函数
查看>>
代理模式
查看>>
Linux命令 cat命令
查看>>
poj1007 逆序数 排序
查看>>
周末轻松话卷积(上)
查看>>
【转】对C# 中堆栈,堆,值类型,引用类型的理解
查看>>
perl脚本调用
查看>>
gcc 0长数组学习
查看>>
经方时方接轨之――茵陈蒿汤合甘露饮
查看>>
MATLAB中取整函数(fix, floor, ceil, round)的使用
查看>>