最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

Dubbo作為一個(gè)RPC框架,其最核心的功能就是要實(shí)現(xiàn)跨網(wǎng)絡(luò)的遠(yuǎn)程調(diào)用。演示過程創(chuàng)建兩個(gè)小工程,一個(gè)作為服務(wù)的提供者,一個(gè)作為服務(wù)的消費(fèi)者。通過Dubbo來實(shí)現(xiàn)服務(wù)消費(fèi)者遠(yuǎn)程調(diào)用服務(wù)提供者的方法。

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

1.Dubbo架構(gòu)

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

節(jié)點(diǎn)角色說明:

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

調(diào)用關(guān)系說明:

服務(wù)容器負(fù)責(zé)啟動,加載,運(yùn)行服務(wù)提供者。

服務(wù)提供者在啟動時(shí),向注冊中心注冊自己提供的服務(wù)。

服務(wù)消費(fèi)者在啟動時(shí),向注冊中心訂閱自己所需的服務(wù)。

注冊中心返回服務(wù)提供者地址列表給消費(fèi)者,如果有變更,注冊中心將基于長連接推送變更數(shù)據(jù)給消費(fèi)者。

服務(wù)消費(fèi)者,從提供者地址列表中,基于軟負(fù)載均衡算法,選一臺提供者進(jìn)行調(diào)用,如果調(diào)用失敗,再選另一臺調(diào)用。

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

2.服務(wù)提供者開發(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相關(guān) -->
    <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)建服務(wù)接口

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

(4) 創(chuàng)建服務(wù)實(shí)現(xiàn)類

package com.test.service.impl;

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

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

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

(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">
  <!-- 當(dāng)前應(yīng)用名稱,用于注冊中心計(jì)算應(yīng)用間依賴關(guān)系,注意:消費(fèi)者和提供者應(yīng)用名不要一樣 -->
  <dubbo:application name="dubbodemo_provider" />
  <!-- 連接服務(wù)注冊中心zookeeper ip為zookeeper所在服務(wù)器的ip地址-->
  <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
  <!-- 注冊 協(xié)議和port  端口默認(rèn)是20880 -->
  <dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
  <!-- 掃描指定包,加入@Service注解的類會被發(fā)布為服務(wù) -->
  <dubbo:annotation package="com.test.service.impl" />

</beans>

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

可以用tomcat啟動項(xiàng)目

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

在com.itheima包下創(chuàng)建一個(gè)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.服務(wù)消費(fèi)者開發(fā)

開發(fā)步驟:

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

(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>
  <!-- 指定加載的配置文件 ,通過參數(shù)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) 將服務(wù)提供者工程中的HelloService接口復(fù)制到當(dāng)前工程

(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來注入服務(wù)
  private HelloService helloService;

  @RequestMapping("/hello")
  @ResponseBody
  public String getName(String name){
   //遠(yuǎn)程調(diào)用
   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 當(dāng)前項(xiàng)目是哪個(gè)-->
  <dubbo:application name="dubbodemo_consumer"/>
  <!--鏈接注冊中心-->
  <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
  <!--注解掃描 掃描的是dubbo的 @Reference注解-->
  <dubbo:annotation package="cn.test"/>

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


</beans>

dubbo的使用小demo已經(jīng)完成。大家可以嘗試一下

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動態(tài)加載

    Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動態(tài)加載

    本文主要介紹了Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動態(tài)加載,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • springBoot 與neo4j的簡單整合示例

    springBoot 與neo4j的簡單整合示例

    這篇文章主要介紹了springBoot 與neo4j的簡單整合示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • Deepseek整合SpringAI詳細(xì)流程

    Deepseek整合SpringAI詳細(xì)流程

    本文介紹了如何使用SpringBoot、Deepseek和SpringAI構(gòu)建一個(gè)簡單的問答系統(tǒng),并通過Postman調(diào)用API接口實(shí)現(xiàn)問答功能,通過本文,你將學(xué)習(xí)如何整合這些技術(shù),快速實(shí)現(xiàn)一個(gè)高效的問答系統(tǒng),感興趣的朋友一起看看吧
    2025-02-02
  • Spring?Boot小型項(xiàng)目如何使用異步任務(wù)管理器實(shí)現(xiàn)不同業(yè)務(wù)間的解耦

    Spring?Boot小型項(xiàng)目如何使用異步任務(wù)管理器實(shí)現(xiàn)不同業(yè)務(wù)間的解耦

    這篇文章主要介紹了Spring?Boot小型項(xiàng)目如何使用異步任務(wù)管理器實(shí)現(xiàn)不同業(yè)務(wù)間的解耦,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • Spring MVC利用Swagger2如何構(gòu)建動態(tài)RESTful API詳解

    Spring MVC利用Swagger2如何構(gòu)建動態(tài)RESTful API詳解

    這篇文章主要給大家介紹了關(guān)于在Spring MVC中利用Swagger2如何構(gòu)建動態(tài)RESTful API的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10
  • java為什么不建議用equals判斷對象相等

    java為什么不建議用equals判斷對象相等

    本文主要介紹了java為什么不建議用equals判斷對象相等,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Idea如何使用System.getenv()獲取環(huán)境變量的值

    Idea如何使用System.getenv()獲取環(huán)境變量的值

    文章介紹了如何在Java中使用`System.getenv()`方法讀取環(huán)境變量的值,并提供了兩種配置環(huán)境變量的方法:啟動項(xiàng)配置和系統(tǒng)環(huán)境變量配置,對于系統(tǒng)環(huán)境變量,文章特別指出需要重啟電腦或程序才能使其生效
    2024-11-11
  • springboot如何使用yml文件方式配置shardingsphere

    springboot如何使用yml文件方式配置shardingsphere

    這篇文章主要介紹了springboot如何使用yml文件方式配置shardingsphere問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • spring使用redis操作key-value的示例代碼

    spring使用redis操作key-value的示例代碼

    這篇文章主要介紹了spring使用redis操作key-value的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Spring MVC異常處理機(jī)制示例詳解

    Spring MVC異常處理機(jī)制示例詳解

    這篇文章主要給大家介紹了關(guān)于Spring MVC異常處理機(jī)制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring MVC具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11

最新評論

隆安县| 宁波市| 海门市| 攀枝花市| 敖汉旗| 闽清县| 武平县| 永康市| 白城市| 涿州市| 油尖旺区| 星子县| 漠河县| 台北市| 黄平县| 溧阳市| 博野县| 乐昌市| 祁阳县| 韩城市| 奉化市| 楚雄市| 昆山市| 光山县| 利辛县| 麟游县| 唐山市| 光山县| 简阳市| 台北市| 襄汾县| 平湖市| 江安县| 泽库县| 微山县| 岳阳市| 喀喇| 竹溪县| 滦平县| 涿州市| 威宁|