spring/springboot整合dubbo详细教程

网友投稿 307 2022-10-23


spring/springboot整合dubbo详细教程

一、基本使用

需求:

某个电商系统,订单服务需要调用用户服务获取某个用户的所有地址;

我们现在需要创建两个服务模块进行测试

模块

功能

订单服务web模块

创建订单等

用户服务service模块

查询用户地址等

测试预期结果:

订单服务web模块在A服务器,用户服务模块在B服务器,A可以远程调用B的功能。

二、spring整合dubbo

以下使用XML 配置的方式,更多配置方式见官方文档

2.1 spring-common模块:

公共接口层(model,service,exception…),定义公共接口,也可以导入公共依赖

UserAddress.java:用户地址实体类

public class UserAddress implements Serializable {

private Integer id;

private String userAddress;

private String userId;

private String consignee;

private String phoneNum;

private String isDefault;

....

}

IOrderService.java:订单接口

public interface IOrderService {

/**

* 用户下单

* @param userId

*/

UserAddress placeOrder(int userId);

}

IUserService.java:用户接口

public interface IUserService {

/**

* 根据用户id获取用户地址

* @param userId

* @return

*/

http:// UserAddress getUserAddById(int userId);

}

pom.xml:通用的依赖,引入dubbo和zkclient

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

dubboStudy

org.example

1.0-SNAPSHOT

4.0.0

spring-common

8

8

com.alibaba

dubbo

2.5.10

org.apache.zookeeper

zookeeper

3.4.6

com.github.sgroschupf

zkclient

0.1

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

dubboStudy

org.example

1.0-SNAPSHOT

4.0.0

spring-common

8

8

com.alibaba

dubbo

2.5.10

org.apache.zookeeper

zookeeper

3.4.6

com.github.sgroschupf

zkclient

0.1

2.2 spring-user模块:

用户业务,作为服务提供者

pom.xml:引入通用模块,可以使用定义的接口

org.example

spring-common

1.0-SNAPSHOT

UserServiceImpl.xml:IUserService的实现类,供远程调用

public class UserServiceImpl implements IUserService {

@Override

public UserAddress getUserAddById(int userId) {

UserAddress userAddress = new UserAddress();

userAddress.setUserAddress("上海市宝山区");

return userAddress;

}

}

provider.xml:dubbo配置信息

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

ConsumerRun.java:启动:

public class ConsumerRun {

public static void main(String[] args) throws IOException {

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");

IOrderService orderService = applicationContext.getBean(IOrderService.class);

orderService.placeOrder(1);

System.in.read();

}

}

2.3 spring-order模块:

订单业务,作为服务消费者

pom.xml:引入通用模块,可以使用定义的接口,同1.2

OrderServiceImpl.xml:IOrderService的实现类,其中远程调用userService

@Service

public class OrderServiceImpl implements IOrderService {

@Autowired

private IUserService userService;

@Override

public UserAddress placeOrder(int userId) {

// 远程调用,获取用户地址

UserAddress userAddById = userService.getUserAddById(userId);

// 下单

System.out.println("用户地址:" + userAddById.getUserAddress());

Systemhttp://.out.println("下单成功");

return null;

}

}

consumer.xml:dubbo配置信息

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.3.xsd

http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.3.xsd

http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

ProviderRun.java:启动

public class ProviderRun {

public static void main(String[] args) throws IOException {

ClassPathXmlApplicationContext context =

new ClassPathXmlApplicationContext("classpath:provider.xml");

System.in.read();

}

}

2.4 效果

ProviderRun启动之后阻塞,

ConsumerRun启动之后调用orderService.placeOrder(1),placeOrder中远程调用spring-user模块中的userService.getUserAddById(userId)获取用户地址:

用户地址:上海市宝山区

下单成功

三、springboot整合dubbo

3.1 boot-common模块:

公共接口层(model,service,exception…),定义公共接口,也可以导入公共依赖

UserAddress.java:用户地址实体类

public class UserAddress implements Serializable {

private Integer id;

private String userAddress;

private String userId;

private String consignee;

private String phoneNum;

private String isDefault;

....

}

IOrderService.java:订单接口

public intehttp://rface IOrderService {

/**

* 用户下单

* @param userId

*/

UserAddress placeOrder(int userId);

}

IUserService.java:用户接口

public interface IUserService {

/**

* 根据用户id获取用户地址

* @param userId

* @return

*/

UserAddress getUserAddById(int userId);

}

pom.xml:通用的依赖,引入dubbo的starter

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.5.0

com.example

boot-common

0.0.1-SNAPSHOT

boot-common

1.8

org.springframework.boot

spring-boot-starter

com.alibaba.boot

dubbo-spring-boot-starter

0.2.0

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.5.0

com.example

boot-common

0.0.1-SNAPSHOT

boot-common

1.8

org.springframework.boot

spring-boot-starter

com.alibaba.boot

dubbo-spring-boot-starter

0.2.0

3.2 boot-user模块:

用户业务,作为服务提供者

pom.xml:引入通用模块,可以使用定义的接口

org.example

spring-common

1.0-SNAPSHOT

UserServiceImpl.xml:IUserService的实现类,供远程调用

@Service 暴露dubbo的服务

@Service

@Component

public class UserServiceImpl implements IUserService {

@Override

public UserAddress getUserAddById(int userId) {

UserAddress userAddress = new UserAddress();

userAddress.setUserAddress("上海市宝山区");

return userAddress;

}

}

application.properties:dubbo配置信息

dubbo.application.name=boot-user

dubbo.registry.address=192.168.31.136:2181

dubbo.registry.protocol=zookeeper

dubbo.protocol.name=dubbo

dubbo.protocol.port=20880

BootUserApplication.java:启动:

@EnableDubbo : 开启基于注解的dubbo功能

@EnableDubbo

@SpringBootApplication

public class BootUserApplication {

public static void main(String[] args) {

SpringApplication.run(BootUserApplication.class, args);

}

}

3.3 boot-order模块:

订单业务,作为服务消费者

pom.xml:引入通用模块,可以使用定义的接口,同1.2

com.example

boot-common

0.0.1-SNAPSHOT

org.springframework.boot

spring-boot-starter-web

OrderServiceImpl.xml:IOrderService的实现类,其中远程调用userService

@Reference:引用远程提供者服务

@Service

public class OrderServiceImpl implements IOrderService {

//引用远程提供者服务

@Reference

private IUserService userService;

@Override

public UserAddress placeOrder(int userId) {

// 远程调用,获取用户地址

UserAddress userAddById = userService.getUserAddById(userId);

return userAddById;

}

}

OrderController.java:web接口,调用OrderService:

@Controller

public class OrderController {

@Autowired

IOrderService orderService;

@RequestMapping("/initOrder")

@ResponseBody

public UserAddress initOrder(@RequestParam("uid")int userId) {

return orderService.placeOrder(userId);

}

}

application.properties:dubbo配置信息

server.port=8081

dubbo.application.name=boot-order

dubbo.registry.address=zookeeper://192.168.31.136:2181

BootOrderApplication.java:启动

@EnableDubbo

@SpringBootApplication

public class BootOrderApplication {

public static void main(String[] args) {

SpringApplication.run(BootOrderApplication.class, args);

}

}

3.4 效果


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:大型混合虚拟化环境的专业备份方案
下一篇:理解HTTP协议
相关文章

 发表评论

暂时没有评论,来抢沙发吧~