springboot、springMVC整合dubbo服务

本文详细介绍了如何使用Dubbo框架搭建微服务架构,包括创建项目、模块划分、依赖配置、接口定义、服务提供与消费等关键步骤。通过具体示例,展示了Spring Boot、Spring MVC项目中Dubbo的集成配置与应用。

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

项目

新建一个项目,里面建三个Module分别代表暴露接口、服务提供者、消费者
在这里插入图片描述
在这里插入图片描述

引用依赖

  • springboot
    在consumer提供者项目引用pom依赖
	<dependency>
			<groupId>com.alibaba.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>0.2.0</version>
	</dependency>
  • springmvc
		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.10</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>

生产者

在interface中写几个接口,并有consumer项目实现这个接口

实现类需要注入@service(使用dubbo的包)和@component实现类代码如下:

import com.alibaba.dubbo.config.annotation.Service;
import com.zang.gmall.bean.UserAddress;
import com.zang.gmall.service.UserService;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Service   //属于Dubbo的@Service注解,非Spring  作用:暴露服务
@Component
public class UserServiceImpl implements UserService {

    @Override
    public List<UserAddress> getUserAddressList(String userId) {
    	//省略
	}
}

生产者配置application.properties

## dubbo
#当前服务/应用的名字
dubbo.application.name=user-service-provider
#注册中心的协议和地址
dubbo.registry.protocol=zookeeper
dubbo.registry.address=IP:2181
#通信规则(通信协议和接口)
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
#连接监控中心
dubbo.monitor.protocol=registry
  • 如果是mvc项目需要一个xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
					        http://www.springframework.org/schema/beans/spring-beans.xsd
					        http://code.alibabatech.com/schema/dubbo
			 		        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
			 		        ">

    <!-- 应用信息,用于计算依赖关系 -->
    <dubbo:application name="authorizationManagement-web"/>

    <dubbo:protocol name="dubbo" port="20870"/>
    
    <!-- 使用zookeeper注册中心暴露服务地址  -->
    <dubbo:registry protocol="zookeeper" address="IP:2181" register="true"/>
    <!--  不需要依赖注册中心  -->
    <!--  <dubbo:registry address="N/A"/>  -->
    
	<!-- 需要暴露包地址  -->
    <dubbo:service interface="暴露接口包路径" ref="helloService"/>

    <bean id="helloService" class="暴露实现包接口路径"/>    

    <dubbo:provider timeout="300000" group="local"/>
    <dubbo:consumer check="false" timeout="300000" group="local"/>
    
</beans>

并在启动类上加@EnableDubbo注解
在这里插入图片描述

消费者配置文件

和提供者引用的引用pom一样

#避免和监控中心端口冲突,设为8081端口访问
server.port=8081  

dubbo.application.name=order-service-consumer
dubbo.registry.address=zookeeper://IP:2181
dubbo.monitor.protocol=registry
  • 如果是mvc项目需要一个xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
					        http://www.springframework.org/schema/beans/spring-beans.xsd
					        http://code.alibabatech.com/schema/dubbo
			 		        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
			 		        ">

    <!-- 应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo-client"/>

    <dubbo:protocol name="dubbo" port="20870"/>
    <!-- 使用zookeeper注册中心暴露服务地址  -->
	<!--    <dubbo:registry protocol="zookeeper" address="IP:2181" register="true"/>-->
    <!--  不需要依赖注册中心  -->
    <!--  <dubbo:registry address="N/A"/>  -->
    
	<!--    &lt;!&ndash; 需要暴露包地址    -->
    <dubbo:reference id="helloService" interface="引用服务提供者暴露接口包路径"/>

	<!--    &lt;!&ndash; 需要暴露包地址,不需要依赖注册中心添加url   -->
    <!--  <dubbo:reference id="helloService" interface="引用服务提供者暴露接口包路径"  url="dubbo://192.168.20.236:20870/服务提供者暴露接口包路径"/>  -->


</beans>

需要引用dubbo服务采用注入方式。

import com.alibaba.dubbo.config.annotation.Reference;
import com.zang.gmall.bean.UserAddress;
import com.zang.gmall.service.OrderService;
import com.zang.gmall.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Reference
    UserService userService;

    @Override
    public List<UserAddress> initOrder(String userId) {
  //代码省略
   }
}

在启动类添加@EnableDubbo注解
在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值