文章目錄
- 前言
-
-
- 分布式架構
- dubbo架構
-
- 基礎架構
- 入門demo簡介
- dubbo遠端調用(RPC)實作
-
前言
分布式架構
分布式系統是指若幹個獨立系統的集合,但是在使用者使用中就像是用同一個系統。在實際項目中随着項目規模逐漸擴大以及業務的複雜度在不斷提升,單台伺服器是扛不住,并且各種業務之間互相互動,為了友善後期維護,一些核心業務可以被提取出來作為獨立的服務被提供,逐漸也就會形成一個穩定的服務中心。
dubbo架構
dubbo是一個高性能的RPC架構,可以解決分布式系統總互相調用的問題。
RPC架構主要就是為了實作遠端方法調用,那麼在提升性能的關鍵就得從比較耗時的方面入手:序列化和網絡通信。
序列化:在本地的對象在網絡上傳輸必須要實作Serializable接口,也就是必須序列化。常見的序列化方案:xml、json、二進制流等,效率最高的就是二進制流,dubbo采用的就是二進制流
網絡通信:不同于HTTP協定,需要三次握手和四次揮手等,dubbo采用的Socket通信機制,提升了通信效率,并且可以建立長連接配接,不需要反複連接配接,直接傳輸資料。
基礎架構
服務提供者(Provider): 服務的提供方,服務提供者在啟動的時候,想注冊中心注冊自己提供的服務。
服務消費者(Consumer): 調用遠端服務的消費者,服務消費者啟動時向注冊中心訂閱自己所需的服務,從提供者位址清單中,基于負載均衡算法,選擇一台服務提供者進行調用。
注冊中心(Register): 注冊中心傳回服務提供者位址清單給消費者,如果變更,注冊中心将基于長連接配接推送變更資料給消費者。
監控中心(Monitor): 服務消費者和提供者,在記憶體中累計調用次數和調用時間,定時每分鐘發送一次統計資料到監控中心。
dubbo服務調用流程
1、啟動項目的服務提供、服務消費、服務接口子產品
2、服務提供者啟動,向注冊中心注冊自己提供的服務
3、服務消費者啟動,向注冊中心訂閱自己所需的服務
4、注冊中心傳回服務提供者位址給消費者,如果變更,注冊中心将基于長連接配接推送變更資料給消費者
5、服務消費者,從提供的位址清單,基于負載均衡算法,選擇一台服務提供者調用,如果失敗,再選擇另一台服務調用
6、服務消費者和提供者,将調用資訊發送給監控中心
入門demo簡介
該demo包含三個子產品,分别是:
服務提供端子產品:zkprovider
服務消費端子產品:zkconsumer
共用的服務接口子產品:zkinterface
dubbo遠端調用(RPC)實作
1、啟動zookeeper
官網下載下傳完zookeeper,将conf檔案夾中的配置檔案修改為zoo.cfg
下圖是我整個項目的結構:
服務提供者:
public class UserServiceImpl implements UserService{
public User queryUserById(Integer id) {
User user = new User();
user.setId(id);
user.setName("test");
return user;
}
}
服務提供者的配置檔案:dubbo-userservice-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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 服務提供者聲明名稱:保證服務名稱的唯一性 -->
<dubbo:application name="zkprovider"/>
<!-- 通路服務協定名稱和端口号-->
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 暴露服務接口-->
<dubbo:service interface="com.yy.dubbo.service.UserService" ref="userServiceImpl"/>
<!--将接口的實作類加載到spring容器中 -->
<bean id="userServiceImpl" class="com.yy.dubbo.service.impl.UserServiceImpl">
</bean>
</beans>
服務提供者子產品的pom檔案:pox.xml
<?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.yy.dubbo</groupId>
<artifactId>zkprovider</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>com.yy.dubbo</groupId>
<artifactId>zkinterface</artifactId>
<version>1.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
服務消費者:
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/userDetail")
public String userDetail(Model model, Integer id){
User user = userService.queryUserById(id);
model.addAttribute("user",user);
return "userDetail";
}
}
服務消費者子產品的ApplicationContext配置檔案:
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.yy.dubbo.web"/>
<!--注解驅動-->
<mvc:annotation-driven />
<!--視圖解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
dubbo-userservice-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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 服務提供者聲明名稱:保證服務名稱的唯一性 -->
<dubbo:application name="zkconsumer"/>
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 應用遠端接口-->
<dubbo:reference id="userService" interface ="com.yy.dubbo.service.UserService" />
</beans>
消費者子產品的pom檔案:
<?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.yy.dubbo</groupId>
<artifactId>zkconsumer</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>com.yy.dubbo</groupId>
<artifactId>zkinterface</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
注冊中心子產品:
package com.yy.dubbo.entity;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
import com.yy.dubbo.entity.User;
public interface UserService {
User queryUserById(Integer id);
}
注冊中心的pom檔案:
<?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.yy.dubbo</groupId>
<artifactId>zkinterface</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
我是一隻在網際網路苟且偷生并努力奮鬥的小菜鳥。
文章持續,可以微信搜尋【程式員小菜鳥】,回複【資料】有我準備的一線大廠面試資料和履歷模闆。