本篇主要是來分享從頭開始搭建一個dubbo+zookeeper平台的過程,其中會簡要介紹下dubbo服務的作用。
注冊中心的選擇
dubbo支持多種類型的注冊中心:
這裡我們選擇zookeeper,其實類型的優點缺點可詳細查看文檔。
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工程,注意以下幾點:
<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(); }
@Service public 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) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "classpath*:applicationContext.xml"); context.start(); logger.info("dubbo service begin to start"); try { System.in.read(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
<?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" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="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.xml"/> </beans>
dubbo消費者
<?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-of-jim-app" /> <dubbo:registry protocol="zookeeper" address="192.168.21.128:2181"/> <dubbo:reference interface="jim.IProduct" id="productService" /> </beans>
@Controller public 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 admin
有一個UI工具可以針對dubbo服務進行管理,可惜我沒有在官方文檔提供的鏈接中下載成功,隨後從其它地方雖然下載完成了,但在安裝部署方面暫時遇到了一定的問題,需要手續研究解決。
正常應該可以看到如下界面:
服務治理
經過上面的步驟後,就可以啟動服務端以及客戶端來驗證了。上面只是簡單的搭建了dubbo環境以及實現了一個hello world的服務接口,要想使用好dubbo還有好多提供的最佳實踐,比如服務治理:
本文引用: