• Dubbo基本使用SpringBoot方式


    1、zookeeper下载与配置:

    参见:zookeeper使用01--下载与配置 - Sempron2800+ - 博客园 (cnblogs.com)

    2、创建Dubbo程序

    1、使用IDEA建立一个空的Maven项目,名为DubboDemo。

    2、在项目中建立一个模块,用于存放公共接口,名为interface。

    3、建立测试用接口:

    1 package com.yas.api;
    2 
    3 public interface SiteService {
    4     String getName(String name);
    5 }

    4、dubbo服务提供方

    在项目中建立一个模块,用于作为dubbo服务的提供方,名为serviceprovider。

    并引入interface模块以及dubbo相关jar包

    4.1 pom文件如下:

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.5.6</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.yas</groupId>
        <artifactId>serviceprovider</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>serviceprovider</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.example</groupId>
                <artifactId>Interface</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>2.7.3</version>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-registry-zookeeper</artifactId>
                <version>2.7.3</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    4.2 application.yml文件:

    server:
      port: 8001
    dubbo:
      application:
        name: site-service-boot-provider
      registry:
        address: zookeeper://localhost:2181
      protocol:
        name: dubbo
        port: 20882

    4.3 接口实现类:

     1 package com.yas.serviceprovider.impl;
     2 
     3 import com.yas.api.SiteService;
     4 import org.apache.dubbo.config.annotation.Service;
     5 
     6 @Service
     7 public class SiteServiceImpl implements SiteService {
     8     @Override
     9     public String getName(String name) {
    10         return "hello:" + name;
    11     }
    12 }

    4.4 主配置类:

     1 package com.yas.serviceprovider;
     2 
     3 import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
     4 import org.springframework.boot.SpringApplication;
     5 import org.springframework.boot.autoconfigure.SpringBootApplication;
     6 
     7 @SpringBootApplication
     8 @EnableDubbo
     9 public class ServiceproviderApplication {
    10 
    11     public static void main(String[] args) {
    12         SpringApplication.run(ServiceproviderApplication.class, args);
    13     }
    14 
    15 }

    5、dubbo服务消费方

    在项目中建立一个模块,用于作为dubbo服务的消费方,名为serviceconsumer。

    并引入interface模块以及dubbo相关jar包

    5.1 pom文件如下:

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.5.6</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>serviceconsumer</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>serviceconsumer</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.example</groupId>
                <artifactId>Interface</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>2.7.3</version>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-registry-zookeeper</artifactId>
                <version>2.7.3</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    5.2 application.yml文件:

    server:
      port: 8000
    dubbo:
      application:
        name: site-service-boot-consumer
      registry:
        address: zookeeper://localhost:2181

    5.3 建立一个controller类,方便测试:

     1 package com.example.serviceconsumer.controller;
     2 
     3 import com.yas.api.SiteService;
     4 import org.apache.dubbo.config.annotation.Reference;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 import org.springframework.web.bind.annotation.RequestParam;
     7 import org.springframework.web.bind.annotation.RestController;
     8 
     9 @RestController
    10 public class SiteController {
    11 
    12     @Reference
    13     SiteService siteService;
    14 
    15     @RequestMapping("/site")
    16     public String getName(@RequestParam("name") String name){
    17         return siteService.getName(name);
    18     }
    19 }

    5.4 主配置类:

     1 package com.example.serviceconsumer;
     2 
     3 import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
     4 import org.springframework.boot.SpringApplication;
     5 import org.springframework.boot.autoconfigure.SpringBootApplication;
     6 
     7 @SpringBootApplication
     8 @EnableDubbo
     9 public class ServiceconsumerApplication {
    10 
    11     public static void main(String[] args) {
    12         SpringApplication.run(ServiceconsumerApplication.class, args);
    13     }
    14 
    15 }

    6、测试

    6.1 启动zookeeper服务端。

    6.2 启动serviceprovider项目,监听8001端口。

    6.3 启动serviceconsumer项目,监听8000端口。

    打开postman或使用浏览器访问:http://localhost:8000/site?name=zhangsan

    得到结果 [hello:zhangsan]。

  • 相关阅读:
    阶段5 3.微服务项目【学成在线】_day18 用户授权_18-微服务之间认证-需求分析
    阶段5 3.微服务项目【学成在线】_day18 用户授权_17-细粒度授权-获取当前用户信息
    阶段5 3.微服务项目【学成在线】_day18 用户授权_16-细粒度授权-我的课程细粒度授权-测试
    阶段5 3.微服务项目【学成在线】_day18 用户授权_15-细粒度授权-我的课程细粒度授权-实现
    阶段5 3.微服务项目【学成在线】_day18 用户授权_14-细粒度授权-我的课程细粒度授权-需求分析
    阶段5 3.微服务项目【学成在线】_day18 用户授权_13-细粒度授权-细粒度授权介绍
    阶段5 3.微服务项目【学成在线】_day18 用户授权_12-前端集成认证授权-携带JWT授权
    阶段5 3.微服务项目【学成在线】_day18 用户授权_11-前端集成认证授权-身份校验
    阶段5 3.微服务项目【学成在线】_day18 用户授权_10-前端集成认证授权-需求分析
    阶段5 3.微服务项目【学成在线】_day18 用户授权_09-动态查询用户的权限-认证服务查询用户权限
  • 原文地址:https://www.cnblogs.com/asenyang/p/15486944.html
Copyright © 2020-2023  润新知