溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Dubbo如何實(shí)現(xiàn)調(diào)用

發(fā)布時(shí)間:2020-08-10 11:24:06 來(lái)源:億速云 閱讀:151 作者:Leah 欄目:編程語(yǔ)言

今天就跟大家聊聊有關(guān)Dubbo如何實(shí)現(xiàn)調(diào)用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

 

1、創(chuàng)建maven工程(打包方式為wardubbodemo_provider,在pom.xml文件中導(dǎo)入如下坐標(biāo)

 

~~~xml

<properties>

  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  <maven.compiler.source>1.8</maven.compiler.source>

  <maven.compiler.target>1.8</maven.compiler.target>

  <spring.version>5.0.5.RELEASE</spring.version>

</properties>

<dependencies>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    <version>${spring.version}</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-beans</artifactId>

    <version>${spring.version}</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-webmvc</artifactId>

    <version>${spring.version}</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-jdbc</artifactId>

    <version>${spring.version}</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-aspects</artifactId>

    <version>${spring.version}</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-jms</artifactId>

    <version>${spring.version}</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context-support</artifactId>

    <version>${spring.version}</version>

  </dependency>

  <!-- dubbo相關(guān) -->

  <dependency>

    <groupId>com.alibaba</groupId>

    <artifactId>dubbo</artifactId>

    <version>2.6.0</version>

  </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>

</dependencies>

<build>

  <plugins>

    <plugin>

      <groupId>org.apache.maven.plugins</groupId>

      <artifactId>maven-compiler-plugin</artifactId>

      <version>2.3.2</version>

      <configuration>

        <source>1.8</source>

        <target>1.8</target>

      </configuration>

    </plugin>

    <plugin>

      <groupId>org.apache.tomcat.maven</groupId>

      <artifactId>tomcat7-maven-plugin</artifactId>

      <configuration>

        <!-- 指定端口 -->

        <port>8081</port>

        <!-- 請(qǐng)求路徑 -->

        <path>/</path>

      </configuration>

    </plugin>

  </plugins>

</build>

~~~

 

2、配置web.xml文件

 

~~~xml

<!DOCTYPE web-app PUBLIC

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>

  <display-name>Archetype Created Web Application</display-name>

  <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:applicationContext*.xml</param-value>

  </context-param>

  <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

</web-app>

 

~~~

 

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

 

~~~java

package com.itheima.service;

public interface HelloService {

    public String sayHello(String name);

}

~~~

 

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

 

~~~java

package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;

import com.itheima.service.HelloService;

 

@Service

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-service.xml

 

~~~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 ipzookeeper所在服務(wù)器ip地址-->

         <dubbo:registry address="zookeeper://192.168.134.129:2181"/>

         <!-- 注冊(cè)  協(xié)議和port   端口默認(rèn)是20880 -->

         <dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>

         <!-- 掃描指定包,加入@Service注解的類會(huì)被發(fā)布為服務(wù)  -->

         <dubbo:annotation package="com.itheima.service.impl" />

</beans>

~~~

 

6、啟動(dòng)服務(wù)

 

tomcat7:run

看完上述內(nèi)容,你們對(duì)Dubbo如何實(shí)現(xiàn)調(diào)用有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI