Dubbo框架設計一共劃分了10個層,而最上面的Service層是留給實際想要使用Dubbo開發分佈式服務的開發者實現業務邏輯的接口層。圖中左邊淡藍背景的爲服務消費方使用的接口,右邊淡綠色背景的爲服務提供方使用的接口, 位於中軸線上的爲雙方都用到的接口。
下面,結合Dubbo官方文檔,咱們分別理解一下框架分層架構中,各個層次的設計要點:java
從上圖能夠看出,Dubbo對於服務提供方和服務消費方,從框架的10層中分別提供了各自須要關心和擴展的接口,構建整個服務生態系統(服務提供方和服務消費方自己就是一個以服務爲中心的)。
根據官方提供的,對於上述各層之間關係的描述,以下所示:git
https://github.com/alibaba/dubbo github
解壓incubator-dubbo-dubbo-2.6.0.zip, 進入dubbo-admin 目錄,執行mvn install -Dmaven.test.skip=true ,在dubbo-admin/target下找到dubbo-admin-2.6.0.war,扔到tomcat/webapps下便可運行。web
服務端和客戶端代碼:spring
pom.xmlapache
<?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>study-dubbo-server</groupId> <artifactId>study-dubbo-server</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <springfox.version>2.2.2</springfox.version> <skipTests>true</skipTests> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java --> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>2.6.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo --> <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.8</version> </dependency> <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> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
服務端json
@Service(version = "1.0.0")tomcat
import java.util.ArrayList; import java.util.List; import com.study.center.emtity.Student; import com.study.center.service.IStudentService; import com.alibaba.dubbo.config.annotation.Service; @Service(version = "1.0.0") public class StudentServiceImpl implements IStudentService { public List<Student> getAll(){ List<Student> reult=new ArrayList<Student>(); reult.add(new Student(1L,"aaa",22)); reult.add(new Student(2L,"aaa",22)); reult.add(new Student(3L,"aaa",22)); return reult; } }
配置文件springboot
server.port=7063
spring.application.name=study-dubbo-server
spring.dubbo.application.name=study-dubbo-server
spring.dubbo.registry.address=zookeeper://192.168.200.238:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.study.center.service
消費端服務器
@Reference(version = "1.0.0")
package com.study.center.controller; import java.util.List; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.alibaba.dubbo.config.annotation.Reference; import com.study.center.emtity.Student; import com.study.center.service.IStudentService; @RestController public class StudentController { @Reference(version = "1.0.0") IStudentService studentService; @RequestMapping("/test") public void getAll() { List<Student> students= studentService.getAll(); for (int i = 0; i < students.size(); i++) { System.out.println(students.get(i).getName()); } } }
配置文件
server.port=7055
spring.application.name=study-dubbo-server
spring.dubbo.application.name=study-dubbo-client
spring.dubbo.registry.address=zookeeper://192.168.200.238:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.study.center.controller
注意項:
1.配置中 spring.dubbo.scan,服務端和客戶端註解所在包是不一樣的,否則消費端會報空異常
2.服務端和消費端 接口定義的包路徑須要一致(
dubbo://192.168.20.70:20880/com.study.center.service.IStudentService?anyhost=true&application=study-dubbo-server&dubbo=2.6.0&generic=false&interface=com.study.center.service.IStudentService&methods=getAll&pid=1652&revision=1.0.0&side=provider×tamp=1535696412672&version=1.0.0 |
)