以下是springboot整合dubbo实现rpc远程调用的简单示例。
jdk1.8
springboot-2.3.0
dubbo-2.6
zookeeper-3.4.9
按照dubbo官方开发建议,创建一个接口项目,该项目只定义接口和entity类;
<dependency>
<groupId>com.thpower</groupId>
<artifactId>dubbo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
package com.thpower.dubbointerface.entity;
import java.io.Serializable;
/**
* 模拟量Entity
* @author xiezk
* @version 2020-06-03
*/
public class Analog implements Serializable {
private static final long serialVersionUID = 1L;
private String aliasName; // alias_name
private String description; // description
protected String mrid; // mrid
private String name; // name
public Analog() {
super();
}
public Analog(String mrid){
this.mrid = mrid;
}
public String getAliasName() {
return aliasName;
}
public void setAliasName(String aliasName) {
this.aliasName = aliasName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getMrid() {
return mrid;
}
public void setMrid(String mrid) {
this.mrid = mrid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.thpower.dubbointerface.service;
import com.thpower.dubbointerface.entity.Analog;
import java.util.List;
public interface AnalogService {
public List<Analog> lists(Analog analog);
}
<!-- 加入springboot与dubbo集成的起步依赖 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<!-- 由于使用了zookeeper作为注册中心,则需要加入zookeeper的客户端jar包: -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!-- 添加接口服务 -->
<dependency>
<groupId>com.thpower</groupId>
<artifactId>dubbo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
# 访问端口
server.port=8881
# dubbo配置
dubbo.application.name=springboot-dubbo-service
dubbo.registry.address=zookeeper://localhost:2181
package com.thpower.dubboservice.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.thpower.dubbointerface.entity.Analog;
import com.thpower.dubbointerface.service.AnalogService;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component //注册为spring bean
@Service // 这注解是dubbo提供的
public class AnalogServiceImpl implements AnalogService {
@Override
public List<Analog> lists(Analog analog) {
Analog analog1 = new Analog();
analog1.setAliasName("服务端返回SUCCESS");
List<Analog> listAnalog = new ArrayList<>();
listAnalog.add(analog1);
return listAnalog;
}
}
package com.thpower.dubboservice;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class DubboServiceApplication {
public static void main(String[] args) {
SpringApplication.run(DubboServiceApplication.class, args);
}
}
# 访问端口
server.port=8882
# dubbo配置
dubbo.application.name=springboot-dubbo-consumer
dubbo.registry.address=zookeeper://localhost:2181
package com.thpower.dubboclient.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.thpower.dubbointerface.entity.Analog;
import com.thpower.dubbointerface.service.AnalogService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class AnalogController {
@Reference//该注解是dubbo提供
private AnalogService analogService;
@RequestMapping("/lists")
public List<Analog> lists(Analog analog){
List<Analog> listAnalog = analogService.lists(analog);
System.out.println("客户端接收服务端返回SUCCESS");
return listAnalog;
}
}
package com.thpower.dubboclient;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo//该注解由dubbo提供
public class DubboClientApplication {
public static void main(String[] args) {
SpringApplication.run(DubboClientApplication.class, args);
}
}
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest