本篇主要是來分享從頭開始搭建一個dubbo+zookeeper平臺的過程,其中會簡要介紹下dubbo服務的做用。java
註冊中心的選擇redis
dubbo支持多種類型的註冊中心:spring
這裏咱們選擇zookeeper,其實類型的優勢缺點可詳細查看文檔。docker
1:zookeeper的安裝,仍是採用docker這一招鮮的run命令來安裝zookeeper緩存
docker run -dit --name zookeeper --hostname zookeeper-host -v /data:/data -p 2181:2181 jplock/zookeeper:latest
2:安裝zkui,能夠參考zkui的項目地址來安裝,它提供了一個管理界面,能夠針對zookeepr的節點值進行CRUD操做,同時也提供了安全認證,按照以下幾步就能夠完成安裝。安全
下面是建立dubbo服務以及使用dubbo服務的過程:
dubbo提供者,建立一個java工程,注意如下幾點:mybatis
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-framework.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.4.10</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.3</version> </dependency>
public interface IProduct { String getProductName();}
@Servicepublic class ProductService implements IProduct{ public String getProductName() { return "jim"; }}
public class App { private final static Logger logger = LoggerFactory.getLogger(App.class); public static void main(String[] args) { ClassPath= new ClassPath"classpath*:applicationContext.); context.start(); logger.info("dubbo service begin to start"); try { System.in.read(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
<??><beans ="http://www.springframework.org/schema/beans" ="http://www.w3.org/2001/ ="http://code.alibabatech.com/schema/dubbo" ="http://www.springframework.org/schema/context" ="http://www.springframework.org/schema/util" ="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd" > <context:property-placeholder location="classpath*:config.properties"/> <dubbo:application name="jim" logger="slf4j" /> <dubbo:registry protocol="zookeeper" address="192.168.21.128:2181" /> <dubbo:protocol accesslog="true" name="dubbo" port="20880" /> <dubbo:annotation package="jim" /> <dubbo:service interface="jim.IProduct" ref="productService"/> <context:component-scan base-package="jim" /> <import resource="redis-context./></beans>
dubbo消費者架構
<??><beans ="http://www.springframework.org/schema/beans" ="http://www.w3.org/2001/ ="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-of-jim-app" /> <dubbo:registry protocol="zookeeper" address="192.168.21.128:2181"/> <dubbo:reference interface="jim.IProduct" id="productService" /></beans>
@Controllerpublic class HomeController { private static final Logger logger = LoggerFactory.getLogger(HomeController.class); @Autowired private IProduct productService; @RequestMapping(value = "/", method = RequestMethod.GET) public String home(Locale locale, Model model) { logger.info("Welcome home! The client locale is {}.", locale); String productName=this.productService.getProductName(); model.addAttribute("name", productName); return "home"; }}
dubbo adminmvc
有一個UI工具能夠針對dubbo服務進行管理,惋惜我沒有在官方文檔提供的連接中下載成功,隨後從其它地方雖然下載完成了,但在安裝部署方面暫時遇到了必定的問題,須要手續研究解決。app
正常應該能夠看到以下界面:
服務治理
通過上面的步驟後,就能夠啓動服務端以及客戶端來驗證了。上面只是簡單的搭建了dubbo環境以及實現了一個hello world的服務接口,要想使用好dubbo還有好多提供的最佳實踐,好比服務治理:
特別提醒:開發人員在開發的時候能夠將本身的業務REST服務化或者Dubbo服務化
2. 項目依賴介紹
2.1 後臺管理系統、Rest服務系統、Scheculer定時調度系統依賴以下圖:
2.2 Dubbo獨立服務項目依賴以下圖:
評論這張