springBoot+dubbo消费端的使用

springBoot+dubbo消费端的使用

相关引用:

springBoot+dubbo服务提供者的使用
zookeeper安装

  • 基于全注解方式

  1. pom.xml文件(引入springBoot+dubbo服务提供者的使用里创建的接口项目和dubbo依赖)

    
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.1.8.RELEASEversion>
            <relativePath/> 
        parent>
        <groupId>com.dubbogroupId>
        <artifactId>consumerartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>consumername>
        <description>Demo project for Spring Bootdescription>
    
        <properties>
            <java.version>1.8java.version>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
    
            
            <dependency>
                <groupId>com.dubbogroupId>
                <artifactId>serviceartifactId>
                <version>1.0-SNAPSHOTversion>
            dependency>
    
            
            <dependency>
                <groupId>com.alibaba.bootgroupId>
                <artifactId>dubbo-spring-boot-starterartifactId>
                <version>0.2.0version>
            dependency>
    
        dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
            plugins>
        build>
    
    project>
    
    
  2. application.properties配置文件

    #提供方应用信息,用于计算依赖关系
    dubbo.application.name=dubboConsumer
    #使用zookeeper注册中心
    dubbo.registry.address=zookeeper://127.0.0.1:2181
    springBoot+dubbo消费端的使用_第1张图片

  3. springBoot启动类添加@EnableDubbo注解
    springBoot+dubbo消费端的使用_第2张图片

  4. 引用服务提供者提供的接口
    springBoot+dubbo消费端的使用_第3张图片

  5. 访问DubboTestController
    springBoot+dubbo消费端的使用_第4张图片




  • 基于XML方式

    1. 使用上面的pom.xml文件

    2. 创建xml配置文件
      springBoot+dubbo消费端的使用_第5张图片

      
      <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
             xmlns="http://www.springframework.org/schema/beans"
             xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
             http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
      
          
          <dubbo:application name="dubboConsumer"/>
      
          
          <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
      
          
          <dubbo:reference interface="com.dubbo.service.DubboTestService" id="dubboTestService" group="test" version="1.0.0"/>
      beans>
      
      
    3. 执行

      import com.dubbo.service.DubboTestService;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      
      public class ConsumerApplication {
      
          public static void main(String[] args) {
             
              ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
              DubboTestService dubboTestService = context.getBean("dubboTestService", DubboTestService.class);
              System.out.println(dubboTestService.getTestResult());
          }
      }
      
      

你可能感兴趣的:(分布式)