Spring Boot和Dubbo整合

本文介绍如何在Spring Boot项目中整合Dubbo实现微服务架构。包括Provider端和服务实现、Consumer端实现,以及如何配置application.properties文件。通过具体的代码示例展示了如何进行服务提供与消费,并提供了测试方法和GitHub项目地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

provider端

  1. POM依赖
<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </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>

    <!--interfaces-->
    <dependency>
        <groupId>com.lovefly</groupId>
        <artifactId>pms-interfaces</artifactId>
    </dependency>

    <!--dubbo-->
    <dependency>
        <groupId>io.dubbo.springboot</groupId>
        <artifactId>spring-boot-starter-dubbo</artifactId>
    </dependency>
</dependencies>
  1. service实现
package com.lovefly.pms.services.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.lovefly.pms.interfaces.service.TestService;

@Service
public class TestServiceImpl implements TestService {
    @Override
    public String echo(String input) {

        return input + ": pong from service";
    }
}
  1. application.properties
#server
server.port=8001

#dubbo
spring.dubbo.registry.group=pms
spring.dubbo.application.name=pms-services
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.lovefly.pms.services.service
spring.dubbo.consumer.retries=0
spring.dubbo.consumer.timeout=30000
spring.dubbo.consumer.check=false

consumer端

  1. POM依赖
<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </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>

    <!--interfaces-->
    <dependency>
        <groupId>com.lovefly</groupId>
        <artifactId>pms-interfaces</artifactId>
    </dependency>
    <!--dubbo-->
    <dependency>
        <groupId>io.dubbo.springboot</groupId>
        <artifactId>spring-boot-starter-dubbo</artifactId>
    </dependency>

</dependencies>
  1. consumer实现
package com.lovefly.pms.portal.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lovefly.pms.interfaces.service.TestService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/echo")
public class EchoController {

    @Reference
    private TestService echoService;

    @GetMapping("")
    public String echo(@RequestParam("input") String input){
        String pong = echoService.echo(input);
        return pong;
    }
}
  1. application.properties
#server
server.port=8000

#dubbo
spring.dubbo.registry.group=pms
spring.dubbo.application.name=pms-portal
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=com.lovefly.pms.portal
spring.dubbo.consumer.retries=0
spring.dubbo.consumer.timeout=30000
spring.dubbo.consumer.check=false
  1. 测试
  • 启动zookeeper
  • 使用curl或PostMan测试服务调用
curl -X GET http://localhost:8000/api/echo/?input=test
#输出:
test: pong from service

将目录添加到github项目中

  1. 进入工作目录
  2. 执行以下命令
$ mvn clean
$ git init
$ git add remote origin https://github.com/fuhongwei041/lovefly-pms.git
$ git branch --set-upstream-to=origin/master master
$ git pull origin master
$ # 编辑.gitignore文件
$ git add .
$ git commit -m "first commit"

项目地址为https://github.com/fuhongwei041/lovefly-pms.git

参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值