微服務化越來越火,實際上是應互聯網時代而生的,微服務化帶來的不僅是性能上的提升,更帶來了研發組織的更加便利,協作更加輕松,團隊效能更高。
當然不能為了技術而技術,我們需要切合實際的對業務進行划分,降低模塊間的耦合度,在加上容器時代的便利性,讓我們開發,部署更加便利。
關於微服務的好處和演化請自行百度,這里給一個簡單的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以及數據庫腳本)