欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring整合Dubbo框架過程及原理解析

 更新時間:2019年12月12日 10:01:54   作者:Runtimeing  
這篇文章主要介紹了Spring整合Dubbo框架過程及原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

這篇文章主要介紹了Spring整合Dubbo框架過程及原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

Dubbo作為一個RPC框架,其最核心的功能就是要實現(xiàn)跨網絡的遠程調用。演示過程創(chuàng)建兩個小工程,一個作為服務的提供者,一個作為服務的消費者。通過Dubbo來實現(xiàn)服務消費者遠程調用服務提供者的方法。

dubbo 的使用需要一個注冊中心,這里以Zookeeper為例來演示

1.Dubbo架構

Dubbo架構圖(Dubbo官方提供)如下:

節(jié)點角色說明:

節(jié)點 角色名稱
Provider 暴露服務的服務提供方
Consumer 調用遠程服務的服務消費方
Registry 服務注冊與發(fā)現(xiàn)的注冊中心
Monitor 統(tǒng)計服務的調用次數和調用時間的監(jiān)控中心
Container 服務運行容器

調用關系說明:

服務容器負責啟動,加載,運行服務提供者。

服務提供者在啟動時,向注冊中心注冊自己提供的服務。

服務消費者在啟動時,向注冊中心訂閱自己所需的服務。

注冊中心返回服務提供者地址列表給消費者,如果有變更,注冊中心將基于長連接推送變更數據給消費者。

服務消費者,從提供者地址列表中,基于軟負載均衡算法,選一臺提供者進行調用,如果調用失敗,再選另一臺調用。

服務消費者和提供者,在內存中累計調用次數和調用時間,定時每分鐘發(fā)送一次統(tǒng)計數據到監(jiān)控中心。

2.服務提供者開發(fā)

(1) 創(chuàng)建maven工程(打包方式為war)dubbodemo_provider,添加依賴pom.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>
  <packaging>war</packaging>
  
  <artifactId>dubbodemo_provider</artifactId>
 <dependencys>
 <!-- dubbo相關 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.6</version>
    </dependency>
    <!-- dubbo2.6.X以上的版本開始 2.8.4不需要 -->
    <dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-all</artifactId>
      <version>4.1.32.Final</version>
    </dependency>
    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-framework</artifactId>
      <version>4.0.0</version>
      <exclusions>
        <exclusion>
          <groupId>org.apache.zookeeper</groupId>
          <artifactId>zookeeper</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.4.7</version>
    </dependency>
    <dependency>
      <groupId>com.github.sgroschupf</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.1</version>
    </dependency>
    <dependency>
      <groupId>javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.12.1.GA</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.47</version>
    </dependency>
  </dependencys>
 
</project>

(2) 配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5">

 <!-- 監(jiān)聽器監(jiān)聽其他的spring配置文件 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring/applicationContext-provider.xml</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

</web-app>

(3) 創(chuàng)建服務接口

package com.test.dubbo.api;
public interface HelloService {
  public String sayHello(String name);
}

(4) 創(chuàng)建服務實現(xiàn)類

package com.test.service.impl;

import com.test.dubbo.api.HelloService;
//這個service并不是spring提供的,是dubbo的service代替的
import com.alibaba.dubbo.config.annotation.Service;

@Service //把此服務注冊到zookeeper
public class HelloServiceImpl implements HelloService {
  public String sayHello(String name) {
   return "hello " + name;
  }
}

==注意==:服務實現(xiàn)類上使用的Service注解是Dubbo提供的,用于對外發(fā)布服務

(5) 在src/main/resources下創(chuàng)建applicationContext-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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    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/mvc
     http://www.springframework.org/schema/mvc/spring-mvc.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">
  <!-- 當前應用名稱,用于注冊中心計算應用間依賴關系,注意:消費者和提供者應用名不要一樣 -->
  <dubbo:application name="dubbodemo_provider" />
  <!-- 連接服務注冊中心zookeeper ip為zookeeper所在服務器的ip地址-->
  <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
  <!-- 注冊 協(xié)議和port  端口默認是20880 -->
  <dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
  <!-- 掃描指定包,加入@Service注解的類會被發(fā)布為服務 -->
  <dubbo:annotation package="com.test.service.impl" />

</beans>

6)啟動服務 也就是把spring容器啟動即可

可以用tomcat啟動項目

也可以用main方法加載spring配置文件,也就是啟動了spring容器

在com.itheima包下創(chuàng)建一個DemoProvider類來啟動spring容器,代碼如下

package com.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class DemoProvider {

  public static void main(String[] args) throws IOException {
//    加載配置文件,啟動容器
    ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-provider.xml");
    app.start();
    System.in.read(); //等待控制臺回車。如果不回車就一直卡這兒不繼續(xù)
  }
}

3.服務消費者開發(fā)

開發(fā)步驟:

(1) 創(chuàng)建maven工程(打包方式為war)dubbodemo_consumer,pom.xml配置和上面服務提供者相同

(2) 配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5">

 <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- 指定加載的配置文件 ,通過參數contextConfigLocation加載-->
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:spring/springmvc.xml</param-value>
  </init-param>
 </servlet>

 <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>

</web-app>

(3) 將服務提供者工程中的HelloService接口復制到當前工程

(4) 編寫Controller

package com.test.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.dubbo.api.HelloService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/demo")
public class HelloController {
  @Reference//這里使用dubbo提供的,用來代替@Autowired來注入服務
  private HelloService helloService;

  @RequestMapping("/hello")
  @ResponseBody
  public String getName(String name){
   //遠程調用
   String result = helloService.sayHello(name);
   System.out.println(result);
   return result;
  }
}

==注意==:Controller中注入HelloService使用的是Dubbo提供的@Reference注解

(5) 在src/main/resources下創(chuàng)建spring文件夾,再創(chuàng)建springmvc.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:aop="http://www.springframework.org/schema/aop"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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">

    <!--spring的掃描包,掃描的是spring的注解-->
  <context:component-scan base-package="cn.test"/>

  <!--告訴zookeeper 當前項目是哪個-->
  <dubbo:application name="dubbodemo_consumer"/>
  <!--鏈接注冊中心-->
  <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
  <!--注解掃描 掃描的是dubbo的 @Reference注解-->
  <dubbo:annotation package="cn.test"/>

  <!--  timeout:每次請求都會等待3秒 retries失敗后重試次數-->
  <dubbo:consumer timeout="3000" retries="0"/>


</beans>

dubbo的使用小demo已經完成。大家可以嘗試一下

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • SpringBoot項目啟動時提示程序包不存在和找不到符號的處理方法

    SpringBoot項目啟動時提示程序包不存在和找不到符號的處理方法

    最近接手同事開發(fā)的一個Springboot工作項目,從svn上整體拉取下來后,構建完成后,啟動的時候遇到了程序包找不到的情況,所以本文記錄了SpringBoot項目啟動時提示程序包不存在和找不到符號的處理方法,需要的朋友可以參考下
    2024-05-05
  • 在Java中操作Zookeeper的示例代碼詳解

    在Java中操作Zookeeper的示例代碼詳解

    這篇文章主要介紹了在Java中操作Zookeeper的示例代碼詳解,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • Java程序包裝成桌面應用程序方式

    Java程序包裝成桌面應用程序方式

    這篇文章主要介紹了Java程序包裝成桌面應用程序方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Java?數據交換?Json?和?異步請求?Ajax詳解

    Java?數據交換?Json?和?異步請求?Ajax詳解

    Json(JavaScript Object Notation)是一種輕量級的數據交換格式,采用鍵值對的形式來表示數據,它廣泛應用于Web開發(fā)中,特別適合于前后端數據傳輸和存儲,這篇文章主要介紹了Java數據交換Json和異步請求Ajax,需要的朋友可以參考下
    2023-09-09
  • java前后端傳值,參數有集合類型的數據時的兩種操作方式

    java前后端傳值,參數有集合類型的數據時的兩種操作方式

    這篇文章主要介紹了java前后端傳值,參數有集合類型的數據時的兩種操作方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 詳解Java關于時間格式化的方法

    詳解Java關于時間格式化的方法

    這篇文章主要介紹了詳解Java關于時間格式化的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • springboot中@Value的工作原理說明

    springboot中@Value的工作原理說明

    這篇文章主要介紹了springboot中@Value的工作原理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java觀察者設計模式(Observable和Observer)

    Java觀察者設計模式(Observable和Observer)

    這篇文章主要介紹了 Java觀察者設計模式(Observable和Observer)的相關資料,需要的朋友可以參考下
    2015-12-12
  • Java中channel用法總結

    Java中channel用法總結

    這篇文章主要介紹了Java中channel用法,較為詳細的總結了channel的定義、類型及使用技巧,需要的朋友可以參考下
    2015-06-06
  • Java 開發(fā)的幾個注意點總結

    Java 開發(fā)的幾個注意點總結

    這篇文章主要介紹了Java開發(fā)的幾個注意點的相關資料,需要的朋友可以參考下
    2016-09-09

最新評論