栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot + Dubbo + Nacos 做Rpc调用的入门案例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot + Dubbo + Nacos 做Rpc调用的入门案例

文章目录
  • 前言
  • 项目结构
    • 服务提供者
      • pom.xml
      • application.properties
      • DemoService 接口
      • DemoServiceImpl 接口实现类
      • 启动类
    • 服务消费者
      • pom.xml
      • application.properties
      • 启动类(在其中调用接口)
    • 运行

前言

最近公司的结构要向微服务发展,最终定的是 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


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.7.RELEASE
         
    
    springboot-dubbo-starter-demo
    dubbo-starter-demo
    0.0.1-SNAPSHOT
    dubbo-starter-demo
    dubbo-starter-demo
    
        2.2.7.RELEASE
        2.7.8
    

    
        
            
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            

            
            
                org.apache.dubbo
                dubbo-dependencies-bom
                ${dubbo.version}
                pom
                import
            
        
    

    
        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
            2.7.8
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.alibaba.nacos
            nacos-client
            1.0.0
        
    




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}®istry-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
DemoService 接口
package org.feng.service;

public interface DemoService {
    String sayName(String name);
}
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);
    }
}

启动类
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);
    }

}
服务消费者

pom.xml


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.7.RELEASE
         
    
    org.feng
    dubbo-starter-demo-consumer
    0.0.1-SNAPSHOT
    dubbo-starter-demo-consumer
    dubbo-starter-demo-consumer
    
        2.2.7.RELEASE
        2.7.8
    


    
        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
            2.7.8
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.alibaba.nacos
            nacos-client
            1.0.0
        

		
        
            org.projectlombok
            lombok
            1.18.20
        

		
        
            springboot-dubbo-starter-demo
            dubbo-starter-demo
            0.0.1-SNAPSHOT
        
    


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}®istry-type=service
demo.service.version= 1.0.0

启动类(在其中调用接口)
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。。。"));
    }
}

运行

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

[demoService] : Hello, DubboStarterDemoConsumerApplication。。。

表示运行正常!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/336477.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号