springboot+dubbo+zookeeper微服务实践demo

微服务化越来越火,实际上是应互联网时代而生的,微服务化带来的不仅是性能上的提升,更带来了研发组织的更加便利,协作更加轻松,团队效能更高。

当然不能为了技术而技术,我们需要切合实际的对业务进行划分,降低模块间的耦合度,在加上容器时代的便利性,让我们开发,部署更加便利。

关于微服务的好处和演化请自行百度,这里给一个简单的demo,是基础的实现“微服务dubbo整合”,本地windows环境演示,记录以便不时回顾,也方便初学者。

1.本地安装配置zookeeper

  配置:

复制zoo_sample.cfg改名为zoo.cfg修改其中的:

2.idea中创建两个项目 :client和server,分别代表消费端和生产端

具体功能自己定了,这个demo中主要是实现了一个简单的数据查询功能,模拟现实中的微服务调用场景。

3.重点看一下生产端和消费短的dubbo配置文件:

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://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="provider">
        <dubbo:parameter key="qos.enable" value="true"/>
        <dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
        <dubbo:parameter key="qos.port" value="33333"/>
    </dubbo:application>
    <!-- 使用zookeeper做为注册中心 -->
    <dubbo:registry  protocol="zookeeper" address="zookeeper://192.168.94.1:2181"/>
    <!-- 用dubbo协议在20880端口暴露服务,默认:20880 -->
    <dubbo:protocol name="dubbo" port="20881" />

    <!-- 缺省配置 -->
    <dubbo:provider timeout="30000" threadpool="fixed" threads="100" accepts="1000" id="payload" payload="11557050"/>

    <!-- ref中的值要跟服务实现类中的@Server的值一致 -->
    <dubbo:service interface="com.example.dubbo.service.StockPurchaseService" ref="stockPurchaseService"></dubbo:service>
    <!--<bean id="stockPurchaseService" clacom.example.dubbover.service.impl.StockPurchaseServiceImpl"/>-->
</beans>

dubbo-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://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="consumer">
        <dubbo:parameter key="qos.enable" value="true"/>
        <dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
        <dubbo:parameter key="qos.port" value="33334"/>
    </dubbo:application>
    <!-- 使用zookeeper做为注册中心 -->
    <dubbo:registry  protocol="zookeeper" address="zookeeper://192.168.94.1:2181"/>
    <!-- 用dubbo协议在20880端口暴露服务,默认:20880 -->
    <dubbo:protocol name="dubbo" port="20881" />

    <!-- 缺省配置 -->
    <dubbo:consumer timeout="1800000" retries="0"/>

    <!-- ref中的值要跟服务实现类中的@Server的值一致 -->
    <dubbo:reference interface="com.example.dubbo.service.StockPurchaseService" id="stockPurchaseService" check="false"/>
    <!--<bean id="stockPurchaseService" class="com.example.server.service.impl.StockPurchaseServiceImpl"/>-->
</beans>

重点关注:

生产和消费端的配置的接口路径要保持一致,ref中的值要跟服务实现类中的@Service的值一致

4.注意:使用Dubbo进行数据传递时,需让作为消息传递的类序列化。

5.测试地址:http://127.0.0.1:8002/client?name=qwe

 一个简单的例子,最后提供源码在下面:(包含client,server以及数据库脚本)

demo

猜你喜欢

转载自www.cnblogs.com/miketwais/p/10216956.html