1. 程式人生 > >Dubbo框架學習

Dubbo框架學習

系統架構演變

1. 單一應用框架(ORM)所有功能部署在一個應用上

2. 垂直應用框架(MVC) 將功能分到多個應用上,缺乏複用性

3. 分散式應用架構(RPC)功能作為服務抽離出來,形成服務中心

4. 流動計算架構(SOA)面向服務的體系結構

Dubbo簡介

Dubbo:

一款基於RPC的分散式服務框架,架構包含

1. Provider:服務提供方

2. Consumer:服務使用方

3. Registry:服務註冊中心

dubbo提供的:Multicast、ZooKeeper、Redis、Simple註冊中心

4. Monitor:服務監控中心

Dubbo優點

1. 呼叫遠端方法如同呼叫本地方法

2. 負載均衡及容錯機制

3.服務註冊中心自動註冊

4.服務介面監控與治理

 

Dubbo使用Demo

服務端:

1. pom.xml

<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.rambo</groupId>
  <artifactId>dubbotest</artifactId>
  <version>0.0.1-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>

2. DubboServiceProvider

package com.rambo.dubbotest.service;

public interface DubboServiceProvider {
	//宣告服務方法
    public String sayHello(String name);
}

 3. DubboServiceProvider實現

package com.rambo.dubbotest.service.impl;

import com.rambo.dubbotest.service.DubboServiceProvider;

@Component("dubboServiceProvider")
public class DubboServiceProviderImpl implements DubboServiceProvider {

	public String sayHello(String name) {
		// TODO Auto-generated method stub
		return "Hello, " + name;
	}

}

 4. 啟動類

package com.rambo.dubbotest;


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);
    }
}

5. 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.rambo.service.DubboServiceProvider" ref="dubboServiceProvider" />
</beans>

 客戶端

1.pom.xml 除開aircraftID不一樣

2.Service 包路徑相同的情況下相同的服務介面

3.使用類

package client;


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;

import com.rambo.dubbotest.service.DubboServiceProvider;

@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);
    }
}

4.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.rambo.dubbotest.service.DubboServiceProvider" id="dubboServiceProvider" />
</beans>

 

5. application.properties 修改客戶端啟動的埠

server.port=8081

演示:

1.啟動zookeeper

2.以SpringBoot方式啟動服務端主類

3.以SpringBoot方式啟動客戶端主類

5.http://localhost:8081/sayhello/Rambo

 

 連結:

DubboServer

DubboClient