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

Spring整合Dubbo框架過(guò)程及原理解析

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

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

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

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

1.Dubbo架構(gòu)

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

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

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

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

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

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

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

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

服務(wù)消費(fèi)者,從提供者地址列表中,基于軟負(fù)載均衡算法,選一臺(tái)提供者進(jìn)行調(diào)用,如果調(diào)用失敗,再選另一臺(tái)調(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ù)提供者開(kāi)發(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以上的版本開(kāi)始 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)聽(tīng)器監(jiān)聽(tīng)其他的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ù)注冊(cè)到zookeeper
public class HelloServiceImpl implements HelloService {
  public String sayHello(String name) {
   return "hello " + name;
  }
}

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

</beans>

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

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

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

在com.itheima包下創(chuàng)建一個(gè)DemoProvider類來(lái)啟動(dòng)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 {
//    加載配置文件,啟動(dòng)容器
    ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-provider.xml");
    app.start();
    System.in.read(); //等待控制臺(tái)回車(chē)。如果不回車(chē)就一直卡這兒不繼續(xù)
  }
}

3.服務(wù)消費(fèi)者開(kāi)發(fā)

開(kāi)發(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>
  <!-- 指定加載的配置文件 ,通過(guò)參數(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) 編寫(xiě)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提供的,用來(lái)代替@Autowired來(lái)注入服務(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"/>
  <!--鏈接注冊(cè)中心-->
  <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
  <!--注解掃描 掃描的是dubbo的 @Reference注解-->
  <dubbo:annotation package="cn.test"/>

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


</beans>

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

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

相關(guān)文章

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

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

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

    springBoot 與neo4j的簡(jiǎn)單整合示例

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

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

    本文介紹了如何使用SpringBoot、Deepseek和SpringAI構(gòu)建一個(gè)簡(jiǎn)單的問(wèn)答系統(tǒng),并通過(guò)Postman調(diào)用API接口實(shí)現(xiàn)問(wèn)答功能,通過(guò)本文,你將學(xué)習(xí)如何整合這些技術(shù),快速實(shí)現(xiàn)一個(gè)高效的問(wèn)答系統(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ù)間的解耦,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • Spring MVC利用Swagger2如何構(gòu)建動(dòng)態(tài)RESTful API詳解

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

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

    java為什么不建議用equals判斷對(duì)象相等

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

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

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

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

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

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

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

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

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

最新評(píng)論

安阳县| 香河县| 张北县| 喜德县| 沙洋县| 临潭县| 南通市| 堆龙德庆县| 尉犁县| 陵水| 鹤山市| 克东县| 新和县| 搜索| 曲靖市| 邹平县| 台中县| 恩施市| 房产| 金寨县| 宝清县| 上高县| 黔江区| 天津市| 龙山县| 买车| 宜阳县| 孝感市| 武穴市| 嘉禾县| 雅江县| 保山市| 贡觉县| 垫江县| 罗甸县| 辽源市| 柯坪县| 大理市| 普安县| 敦化市| 绥棱县|