当前位置:   article > 正文

SpringBoot + Dubbo + Nacos 做Rpc调用的入门案例_dubbo.registry.address=nacos://${spring.cloud.naco

dubbo.registry.address=nacos://${spring.cloud.nacos.server-addr}

前言

最近公司的结构要向微服务发展,最终定的是 SpringBoot + Dubbo 的架构组合。使用Nacos 做配置中心和Rpc调用,协议使用 dubbo协议,然后使用某种方式序列化。

主要参考官方案例:
https://github.com/apache/dubbo-spring-boot-project/tree/master/dubbo-spring-boot-samples/service-introspection-samples

现在在这里做一个HelloWorld的案例,用以熟悉流程结构。
本案例的版本使用的是(注意最好不要修改版本,谁改谁知道):

  • SpringBoot 版本:2.2.7.RELEASE
  • dubbo-spring-boot-starter 版本:2.7.8
  • nacos-client 版本:1.0.0

项目结构

服务提供者

在这里插入图片描述

pom.xml

<?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.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>springboot-dubbo-starter-demo</groupId>
    <artifactId>dubbo-starter-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-starter-demo</name>
    <description>dubbo-starter-demo</description>
    <properties>
        <spring-boot.version>2.2.7.RELEASE</spring-boot.version>
        <dubbo.version>2.7.8</dubbo.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- Spring Boot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- Apache Dubbo  -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-bom</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- Dubbo Spring Boot Starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

</project>


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

application.properties

#nacos.config.server-addr=genfjs.com:80

##dubbo config
spring.application.name=springboot-dubbo-nacos-provider
server.port=10086
# nacos所在地址
nacos.server-address = gnefjs.com
nacos.port = 80
nacos.username=nacos
nacos.password=nacos

dubbo.application.name=${spring.application.name}
dubbo.registry.address=nacos://${nacos.server-address}:${nacos.port}/?username=${nacos.username}&password=${nacos.password}&registry-type=service


# dubbo接口所在的包
dubbo.scan.base-packages=org.feng.service
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

# Provider @Service version
demo.service.version=1.0.0
demo.service.name = demoService
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

DemoService 接口

package org.feng.service;

public interface DemoService {
    String sayName(String name);
}
  • 1
  • 2
  • 3
  • 4
  • 5

DemoServiceImpl 接口实现类

package org.feng.service.impl;

import org.apache.dubbo.config.annotation.DubboService;
import org.feng.service.DemoService;
import org.springframework.beans.factory.annotation.Value;


@DubboService(version = "1.0.0")
public class DemoServiceImpl implements DemoService {

    @Value("${demo.service.name}")
    private String serviceName;

    @Override
    public String sayName(String name) {
        return String.format("[%s] : Hello, %s", serviceName, name);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

启动类

package org.feng;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DubboStarterDemoApplication {

    public static void main(String[] args) {

        SpringApplication.run(DubboStarterDemoApplication.class, args);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

服务消费者

在这里插入图片描述

pom.xml

<?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.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.feng</groupId>
    <artifactId>dubbo-starter-demo-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-starter-demo-consumer</name>
    <description>dubbo-starter-demo-consumer</description>
    <properties>
        <spring-boot.version>2.2.7.RELEASE</spring-boot.version>
        <dubbo.version>2.7.8</dubbo.version>
    </properties>


    <dependencies>
        <!-- Dubbo Spring Boot Starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.0.0</version>
        </dependency>

		<!--本项目中使用它来输出日志-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>

		<!--服务提供者:一般引入接口所在模块-->
        <dependency>
            <groupId>springboot-dubbo-starter-demo</groupId>
            <artifactId>dubbo-starter-demo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

application.properties

spring.application.name: dubbo-nacos-service-introspection-consumer-sample

nacos.server-address = gnefjs.com
nacos.port = 80
nacos.username=nacos
nacos.password=nacos

dubbo.registry.address=nacos://${nacos.server-address}:${nacos.port}/?username=${nacos.username}&password=${nacos.password}&registry-type=service
demo.service.version= 1.0.0

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

启动类(在其中调用接口)

package org.feng;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.feng.service.DemoService;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;


@Slf4j
@SpringBootApplication
public class DubboStarterDemoConsumerApplication {

    @DubboReference(version = "1.0.0")
    DemoService demoService;

    public static void main(String[] args) {
        SpringApplication.run(DubboStarterDemoConsumerApplication.class, args);
    }

    @Bean
    public ApplicationRunner runner() {
        return args -> log.info(demoService.sayName("DubboStarterDemoConsumerApplication。。。"));
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

运行

先保证自己的Nacos已经正常启动。
然后启动服务提供者。
在启动服务消费者。
运行结果是,在服务消费者这端,会输出日志:

[demoService] : Hello, DubboStarterDemoConsumerApplication。。。

表示运行正常!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/165639
推荐阅读
相关标签
  

闽ICP备14008679号