当前位置: 首页 > 图灵资讯 > 技术篇> SpringBoot + Dubbo + Nacos

SpringBoot + Dubbo + Nacos

来源:图灵教育
时间:2023-08-16 09:38:54

通过Dubo远程调用,写一个小项目来练习

架构
  1. SpringBoot
  2. Spring Cloud
  3. Spring Cloud Alibaba
  4. Dubbo
  5. Nacos
依赖
 <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.6.3</version>    </parent>    <dependencyManagement>        <dependencies>            <dependency>                <groupId>com.alibaba.cloud</groupId>                <artifactId>spring-cloud-alibaba-dependencies</artifactId>                <version>2021.0.1.0</version>                <type>pom</type>                <scope>import</scope>            </dependency>            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>2021.0.1</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies>    </dependencyManagement><dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter</artifactId>        </dependency>        <dependency>            <groupId>com.alibaba.cloud</groupId>            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.apache.dubbo</groupId>            <artifactId>dubbo-spring-boot-starter</artifactId>            <version>3.0.2.1</version>        </dependency>        <dependency>            <groupId>org.apache.dubbo</groupId>            <artifactId>dubbo</artifactId>            <version>3.0.2.1</version>        </dependency>        <dependency>            <groupId>org.apache.dubbo</groupId>            <artifactId>dubbo-registry-nacos</artifactId>            <version>3.0.2.1</version>        </dependency>

注意Spring Boot 与 Spring Cloud 、 Cloud 依赖版AlibabaSpring Boot 2.6.3 、Spring Cloud 2021.0.1.0 、 Spring Cloud Alibaba 2021.0.1

编写API层

Spring-boot-dubbo-demo-interface

package com.xin.demo.service;public interface IUserService {    String sayHi (String name);}
编写提供者

Spring-boot-demo-provider

提供者需要依靠API层来实现其方法

引入API依赖

<dependency>            <groupId>com.xin</groupId>            <artifactId>spring-dubbo-interface</artifactId>            <version>1.0-SNAPSHOT</version>        </dependency>

实现API接口 实现类添加 @注释Duboservice

@DubboServicepublic class IUserImpl implements IUserService {    @Override    public String sayHi(String name) {        return "你好, " + name;    }}

编写启动类 添加 @Enableduboconfig注释

@SpringBootApplication@EnableDubboConfigpublic class ProviderApplication {    public static void main(String[] args) {        SpringApplication.run(ProviderApplication.class, args);    }}

编制配置文件

#  为项目命名spring.application.name=spring-dubbo-provider# 为当前Dubo暴露的接口提供了唯一的标识.application.id=spring-dubbo-provider# 在注册中心存储当前服务的名称dubo.application.name=spring-dubbo-provider# dubbo注册中心地址.registry.address=nacos://192.168.1.72:8848# 必须配置以下供应商# dubobo协议暴露接口.protocol.name=dubbo# 端口暴露接口 dubbo.protocol.port=20880
消费者

spring-dubbo-demo-consumer

需要引入供应商的依赖

   <dependency>            <groupId>com.xin</groupId>            <artifactId>spring-dubbo-provider</artifactId>            <version>1.0-SNAPSHOT</version>        </dependency>

编写Controller , 用@Dubboreference引入消费者

@RestController@RequestMapping("/user")public class UserController {    @DubboReference    private IUserService userService;    @GetMapping    public String sayHi () {        return userService.sayHi("zhangsan");    }}

编写启动类

@SpringBootApplication@EnableDubboConfigpublic class ConsumerApplication {    public static void main(String[] args) {        SpringApplication.run(ConsumerApplication.class, args);    }}

编制配置文件

spring.application.name=spring-dubbo-consumerdubbo.application.id=spring-dubbo-consumerdubbo.application.name=spring-dubbo-consumerdubbo.registry.address=nacos://192.168.1.72:848.erver.port=9090