Dubbo

Dubbo

微服务

在介绍Dubbo之前先讲解一下微服务。微服务的理念就是,将产品或项目分解为众多独立的服务,这些服务独立地部署,并且不依赖于其它服务。因此,微服务没有集中的数据库,每个模块都具有自己的存储系统,可能是MySQL,也可能是Redis,技术选型可以根据模块的特点来选择。

微服务的优势
① 单系统只用一种开发语言,但微服务每个服务独立,每个服务可以看成一个项目,因此每个服务可以选择最合适服务特色的技术开发。
② 开发集中在一个服务,业务和代码量都不大,开发人员能很好地把握代码。
③ 服务间进行调用时,可以通过API来进行通讯,如REST(HTTP),也可以使用分布式服务管理框架如Dubbo(rpc) 、SpringCloud(HTTP)等。

什么是Dubbo

DUBBO是一个分布式服务治理框架,致力于提供高性能和透明化的RPC远程服务调用方案,可以和 Spring框架无缝集成,是阿里巴巴SOA服务化治理方案的核心框架

Dubbo核心要点

① 服务定义
服务是围绕服务提供方和服务消费方的,服务提供方实现服务,而服务消费方调用服务。
② 服务注册
对于服务提供方,它需要发布服务,而且由于应用系统的复杂性,服务的数量、类型也不断膨胀;对于服务消费方,它最关心如何获取到它所需要的服务,而面对复杂的应用系统,需要管理大量的服务调用。而且,对于服务提供方和服务消费方来说,他们还有可能兼具这两种角色,即既需要提供服务,有需要消费服务。
Dubbo提供的注册中心有如下几种类型可供选择:
Multicast(多播/组播)注册中心(开发测试用)、Zookeeper注册中心(生产环境用官方推荐)、Redis注册中心、Simple注册中心
③ 服务监控
无论是服务提供方,还是服务消费方,他们都需要对服务调用的实际状态进行有效的监控,从而改进服务质量。
④ 远程通信与信息交换
远程通信需要指定通信双方所约定的协议,在保证通信双方理解协议语义的基础上,还要保证高效、稳定的消息传输。Dubbo继承了当前主流的网络通信框架,主要包括如下几个:Mina、Netty、Grizzly
⑤ 服务调用
在这里插入图片描述
节点角色说明:
Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次调和调用时间的监控中心。
Container: 服务运行容器。
调用关系说明:

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

Dubbo的获取(官网hello world)

先建立父类maven

<!-- dubbo包 -->
<dependencies>
   <dependency>
       <groupId>com.alibaba</groupId>
       <artifactId>dubbo</artifactId>
       <version>2.6.2</version>
   </dependency>
</dependencies>
<!-- junit测试包 -->
<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.12</version>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-test</artifactId>
	<version>4.3.16.RELEASE</version>
</dependency>

建立子类公共maven

建立公共接口
public interface DemoService {
    String sayHello(String name);
}

建立子类服务提供者maven:dobbo_provider
dubbo-provider.xml

<?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://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dobbo_provider"  />

    <!-- 使用multicast广播注册中心暴露服务地址 用zookeeper-->
    <dubbo:registry address="zookeeper://localhost:2181" client="zkclient" />
    
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- dubbo注解配置-->
    <dubbo:annotation package="cn.lw" />
</beans>
依赖公共maven(导入公共类中的类)
    <dependencies>
        <dependency>
            <groupId>dobbo_api</groupId>
            <artifactId>dobbo_api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
实现接口
@Service
public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hello " + name;
    }
}
//测试dubbo服务提供者
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:provider.xml")
public class Provider {
    @Test
    public void test() throws Exception{
        System.out.println("启动");
        System.in.read(); // 阻塞进程,按任意键退出
    }
}

建立子类服务消费者maven:dobbo_user
consumer.xml

<?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://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="dobbo_user"  />

    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <dubbo:registry address="zookeeper://localhost:2181" client="zkclient" />

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <!--<dubbo:reference id="demoService" interface="cn.lw.dao.DemoService" />-->
 	<!-- dubbo注解配置-->
    <dubbo:annotation package="cn.lw" />
</beans>
测试消费方
public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"});
        context.start();
        DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
        String hello = demoService.sayHello("world"); // 执行远程方法
        System.out.println( hello ); // 显示调用结果
    }

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:consumer.xml")
public class Consumer {
    //dubbo注解引用服务
    @Reference
    DemoService service;
    @Test
    public void test() throws Exception{
        String hello=service.sayHello("lw");
        System.out.println(hello);
    }
依赖公共maven(导入公共类中的类)
    <dependencies>
        <dependency>
            <groupId>dobbo_api</groupId>
            <artifactId>dobbo_api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

Zookeeper注册中心

什么是Zookeeper

ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
去官网下载Zookeeper
修改注册中心地址:
将集成好的项目,使用zookeeper替换multicast广播方式,作为dubbo服务的发布注册中心。
<dubbo:registry address=“zookeeper://127.0.0.1:2181”></dubbo:registry>
把conf目录下的zoo_sample.cfg改名成zoo.cfg
点击bin目录下的zkServer.cmd 这时候出现下面的提示就说明配置成功了。在这里插入图片描述
最后按照之前的步骤,先启动服务提供者,再启动服务消费者(前面已经配好了)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值