Spring Boot和Dubbo整合

时间:2023-12-22 20:01:32

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

参考资料