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]。

 

posted on 2021-10-30 19:03  Sempron2800+  阅读(107)  评论(0编辑  收藏  举报