zoukankan      html  css  js  c++  java
  • 【SpringBoot】SpringBoot 与Dubbo(二十五)

      本章介绍SpringBoot与与Dubbo。

      Apache Dubbo 是一个基于Java的高性能,轻量级的RPC框架。Dubbo提供了三个关键功能,包括基于接口的远程呼叫,容错和负载平衡以及自动服务注册和发现。

    Dubbo服务提供商

      1、新建一个项目SpringBoot Web项目,引入Dubbo依赖

    1 <!-- Dubbo Spring Boot Starter -->
    2 <dependency>
    3     <groupId>org.apache.dubbo</groupId>
    4     <artifactId>dubbo-spring-boot-starter</artifactId>
    5     <version>2.7.5</version>
    6 </dependency>

        完整pom文件如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     5     <modelVersion>4.0.0</modelVersion>
     6 
     7     <groupId>com.test</groupId>
     8     <artifactId>test-springboot-dubbo-provider</artifactId>
     9     <version>1.0-SNAPSHOT</version>
    10 
    11     <parent>
    12         <groupId>org.springframework.boot</groupId>
    13         <artifactId>spring-boot-starter-parent</artifactId>
    14         <version>2.1.8.RELEASE</version>
    15     </parent>
    16 
    17     <properties>
    18         <dubbo.version>2.7.5</dubbo.version>
    19         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    20         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    21         <java.version>1.8</java.version>
    22     </properties>
    23 
    24     <dependencies>
    25 
    26         <dependency>
    27             <groupId>org.springframework.boot</groupId>
    28             <artifactId>spring-boot-starter-web</artifactId>
    29         </dependency>
    30 
    31         <!-- Dubbo Spring Boot Starter -->
    32         <dependency>
    33             <groupId>org.apache.dubbo</groupId>
    34             <artifactId>dubbo-spring-boot-starter</artifactId>
    35             <version>${dubbo.version}</version>
    36         </dependency>
    37 
    38         <dependency>
    39             <groupId>org.springframework.boot</groupId>
    40             <artifactId>spring-boot-starter-test</artifactId>
    41             <scope>test</scope>
    42         </dependency>
    43 
    44     </dependencies>
    45 
    46 
    47     <!-- SpringBoot打包插件,可以将代码打包成一个可执行的jar包 -->
    48     <build>
    49         <plugins>
    50             <plugin>
    51                 <groupId>org.springframework.boot</groupId>
    52                 <artifactId>spring-boot-maven-plugin</artifactId>
    53             </plugin>
    54         </plugins>
    55     </build>
    56 
    57 </project>
    View Code

        目录如下:

          

      2、编写配置文件application.properties

     1 # spring应用名
     2 # Spring boot application
     3 spring.application.name=dubbo-auto-configuration-provider-demo
     4 # Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
     5 dubbo.scan.base-packages=com.test.springboot.dubbo.provider.service
     6 
     7 # Dubbo应用:dubbo应用吗默认值是spring应用名
     8 # Dubbo Application
     9 ## The default value of dubbo.application.name is ${spring.application.name}
    10 ## dubbo.application.name=${spring.application.name}
    11 
    12 # Dubbo协议
    13 # Dubbo Protocol
    14 dubbo.protocol.name=dubbo
    15 dubbo.protocol.port=12345
    16 
    17 # 不在zookeeper上注册服务
    18 ## Dubbo Registry
    19 dubbo.registry.address=N/A

        其中采用了dubbo协议,在12345端口通信,不再zookeeper上注册服务

      3、编写DemoService接口

    1 package com.test.springboot.dubbo.provider.service;
    2 
    3 public interface DemoService {
    4 
    5     String sayHello(String name);
    6 
    7 }

      4、编写实现接口DefaultDemoService,它实现了接口DemoService

     1 package com.test.springboot.dubbo.provider.service;
     2 
     3 import org.apache.dubbo.config.annotation.Service;
     4 import org.springframework.beans.factory.annotation.Value;
     5 
     6 // 被扫描为dubbo服务
     7 @Service(version = "1.0.0")
     8 public class DefaultDemoService implements DemoService {
     9 
    10     /**
    11      * The default value of ${dubbo.application.name} is ${spring.application.name}
    12      */
    13     @Value("${dubbo.application.name}")
    14     private String serviceName;
    15 
    16     public String sayHello(String name) {
    17         return String.format("[%s] : Hello, %s", serviceName, name);
    18     }
    19 
    20 }

      5、启动项目,将服务部署起来

    Dubbo服务使用者

      1、新建一个新项目SpringBoot Web项目,引入Dubbo依赖

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     5     <modelVersion>4.0.0</modelVersion>
     6 
     7     <groupId>com.test</groupId>
     8     <artifactId>test-springboot-dubbo-consumer</artifactId>
     9     <version>1.0-SNAPSHOT</version>
    10 
    11 
    12     <parent>
    13         <groupId>org.springframework.boot</groupId>
    14         <artifactId>spring-boot-starter-parent</artifactId>
    15         <version>2.1.8.RELEASE</version>
    16     </parent>
    17 
    18     <properties>
    19         <dubbo.version>2.7.5</dubbo.version>
    20         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    21         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    22         <java.version>1.8</java.version>
    23     </properties>
    24 
    25     <dependencies>
    26 
    27         <dependency>
    28             <groupId>org.springframework.boot</groupId>
    29             <artifactId>spring-boot-starter-web</artifactId>
    30         </dependency>
    31 
    32         <!-- Dubbo Spring Boot Starter -->
    33         <dependency>
    34             <groupId>org.apache.dubbo</groupId>
    35             <artifactId>dubbo-spring-boot-starter</artifactId>
    36             <version>${dubbo.version}</version>
    37         </dependency>
    38 
    39         <dependency>
    40             <groupId>org.springframework.boot</groupId>
    41             <artifactId>spring-boot-starter-test</artifactId>
    42             <scope>test</scope>
    43         </dependency>
    44 
    45     </dependencies>
    46 
    47 
    48     <!-- SpringBoot打包插件,可以将代码打包成一个可执行的jar包 -->
    49     <build>
    50         <plugins>
    51             <plugin>
    52                 <groupId>org.springframework.boot</groupId>
    53                 <artifactId>spring-boot-maven-plugin</artifactId>
    54             </plugin>
    55         </plugins>
    56     </build>
    57 </project>
    pom.xml

        目录结构如下:

            

      2、编写配置文件application.properties

    1 spring.application.name: dubbo-auto-configure-consumer-sample

      3、拷贝服务提供者的DemoService接口,接口的类路径不变

    1 // 类路径不变
    2 package com.test.springboot.dubbo.provider.service;
    3 
    4 public interface DemoService {
    5 
    6     String sayHello(String name);
    7 
    8 }

      4、编写实测试类

     1 @RunWith(SpringRunner.class)
     2 @SpringBootTest
     3 public class TestApplication {
     4 
     5     @Reference(version = "1.0.0", url = "dubbo://127.0.0.1:12345")
     6     private DemoService demoService;
     7 
     8     @Test
     9     public void sayHello(){
    10         System.out.println(demoService.sayHello("XX"));
    11     }
    12 
    13 }

      5、运行测试类sayHello方法(),效果如下:

        

  • 相关阅读:
    layui table中省略号展开,弹框拖动会错位问题
    layui table分页 page为false时,limit问题
    layui 表格在排序之后没有重新渲染问题
    基于jQuery的控件:弹框
    layui layer弹框中表格的显示
    layui select使用问题
    页面强制横屏
    linux下常用命令
    文字超出省略号显示
    Web Notification简单实现桌面消息通知(右下角提示)
  • 原文地址:https://www.cnblogs.com/h--d/p/12513799.html
Copyright © 2011-2022 走看看