分布式中台实践--Dubbo的简单实践

       Dubbo是在Spring基础之上做的扩展,也就是说阿里巴巴对于spring的性能、易用性还是非常的认可的。从使用角度来讲,即使是大公司对于流行技术框架,也都细心研究。废话不多说,下面我们从Dubbo实践的角度,讲解Dubbo的具体应用。
1、环境配置
      Dubbo实践最核心是构造服务端、消费端和配置中心,而这三者的基础依赖于Maven构建。因此实践的第一步是配置Maven环境。配置完成之后,新建一个Maven项目,将Dubbo和Spring依赖配置好,程序将自动下载其依赖jar包。 如果服务端类型为Web服务,则还需要配置Web服务容器,例如Tomcat。
      在Dubbo框架中,Spring作为配置文件的解析框架和服务提供框架。Dubbo具体扩展了配置文件的解析逻辑,能够使得注册配置文件按照类似于Spring配置文件的形式配置。另外,Spring可以高效的对外提供Restful接口。Tomcat作为对外服务的一种容器,服务端通过它对外提供服务。
      Dubbo框架是一种服务形式的抽象,可以结合不同的业务形式具体使用。服务端可以采用Tomcat部署,也可以采用其他的项目形式提供部署。服务端自动实现服务注册、服务传输协议等,用户仅需要提供服务接口和服务内容即可。消费端实现了负载均衡,服务代理等,用户只需要提供具体的业务引用即可。
2、项目实践
根据Dubbo的架构,具体的项目实践分为以下步骤:
  1)定义服务端和消费端共同遵守的约定接口;
   2)定义服务类,其实现约定接口。
   3)将服务端注册到配置端。配置端如果采用组播的形式,则不需要单独配置。如果采用其他的形式,如zookeeper则需要提前配置服务器。
   4)定义消费端调用,并注册消费端服务。服务端实现服务的负载均衡调度。
3、具体代码
    1)定义接口
       package   com . alibaba . dubbo . demo ;

public interface DemoService {
    String sayHello(String name);
}
2、实现服务
package com.alibaba.dubbo.demo.provider;
import com.alibaba.dubbo.demo.DemoService;

public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hello " + name;
    }
}
3、配置服务实现
<?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-provider"/>
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <dubbo:protocol name="dubbo" port="20880"/>
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
</beans>
4、启动服务
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] {"META-INF/spring/dubbo-demo-provider.xml"});
        context.start();
        // press any key to exit
        System.in.read();
    }
}
5、配置服务消费者
<?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"/>
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/>
</beans>
6、启动服务消费者
import com.alibaba.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
        context.start();
        // obtain proxy object for remote invocation
        DemoService demoService = (DemoService) context.getBean("demoService");
        // execute remote invocation
        String hello = demoService.sayHello("world");
        // show the result
        System.out.println(hello);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值