dubbo+Springboot入門例項
阿新 • • 發佈:2019-01-02
dubbo介紹
dubbo是一套基於java的高可用的分散式服務框架。由阿里巴巴開發,並對外開源。和其他RPC框架一樣,在服務端宣告介面服務,並實現,同時將服務註冊到服務註冊中心(開源使用zookeeper作為註冊中心);客戶端宣告與服務端一致的介面,然後呼叫即可。
dubbo概念
provider:服務提供者,也就是宣告服務介面,並暴露服務的一方
consumer:服務消費者,也就是呼叫服務的一方
registry:服務註冊中心,用於服務發現與註冊,開源基於zookeeper
monitor:服務監控,用於dubbo服務呼叫的監控
container:執行容器,個人理解這不屬於dubbo體系中的概念。是在paas類平臺中宣告的基礎執行環境。
官方dubbo架構圖:
1、安裝zookeeper
從官網下載zookeeper伺服器,並在本地執行。這裡我們下載的是3.4.6版本。複製zoo_sample.cfg為zoo.cfg。zookeeper啟動時預設載入zoo.cfg配置。zoo.cfg裡面配置了資料檔案、日誌檔案、心跳、埠等資訊。進入zookeeper安裝目錄/bin,雙擊zkServer.cmd啟動zookeeper。預設zookeeper執行在2181埠。
2、建立dubbo服務provider
在idea中建立maven專案,並在pom.xml檔案中引入springboot和dubbo依賴。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.nmm.study</groupId>
<artifactId>dubboprovider</artifactId>
<version>1.0-SNAPSHOT</version>
<!--引入springboot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入dubbot-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.7</version>
</dependency>
<!--引入zookeeper-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<!--引入zookeeper客戶端-->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
宣告服務介面,並實現。
package com.nmm.study.service;
/**
* @description 宣告服務介面
* @date 2017/12/7
* @author Niemingming
*/
public interface DubboServiceProvider {
//宣告服務方法
public String sayHello(String name);
}
package com.nmm.study.service.impl;
import com.nmm.study.service.DubboServiceProvider;
import org.springframework.stereotype.Component;
/**
* @description 實現介面服務
* @date 2017/12/7
* @author Niemingming
*/
@Component("dubboServiceProvider")
public class DubboServiceProviderImpl implements DubboServiceProvider {
@Override
public String sayHello(String name) {
return "hello " + name;
}
}
建立dubbo配置檔案。dubbo.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="dubboproviderhello" />
<!--配置服務註冊中心,dubbo不僅僅支援zookeeper-->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!--宣告對外暴露的服務-->
<dubbo:service interface="com.nmm.study.service.DubboServiceProvider" ref="dubboServiceProvider" />
</beans>
建立服務啟動類:
package com.nmm.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource("classpath:/dubbo.xml")
public class ProviderApplication {
public static void main(String[] args){
SpringApplication.run(ProviderApplication.class,args);
}
}
3、dubbo客戶端呼叫consumer
類似2,建立maven專案。在pom.xml檔案中引入相同內容,同時建立與
2一樣的介面宣告。在dubbo.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="dubboproviderhello" />
<!--配置服務註冊中心,dubbo不僅僅支援zookeeper-->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!--宣告服務引用,與服務宣告介面型別一致-->
<dubbo:reference interface="com.nmm.study.service.DubboServiceProvider" id="dubboServiceProvider" />
</beans>
我們這裡偷懶,在服務啟動類中直接宣告服務呼叫和注入。
package com.nmm.study;
import com.nmm.study.service.DubboServiceProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@ImportResource("classpath:/dubbo.xml")
@RestController
public class ConsumerApplication {
//注入宣告的服務
@Autowired
private DubboServiceProvider dubboServiceProvider;
public static void main(String[] args){
SpringApplication.run(ConsumerApplication.class,args);
}
//測試服務呼叫
@ResponseBody
@RequestMapping("/sayhello/{name}")
public String hello(@PathVariable String name){
return dubboServiceProvider.sayHello(name);
}
}