springboot集成dubbo的实例

本文详细介绍了如何使用Dubbo框架实现微服务的提供者与消费者角色,包括必要的依赖配置、XML配置、接口定义及其实现类,以及Spring Boot启动类的配置。通过具体的代码示例,展示了服务的注册、发现和调用过程。

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

provider:服务方

1.引用jar包

<!-- dubbo依赖 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.6.8</version>
            </dependency>
            <dependency>
	            <groupId>org.apache.curator</groupId>
	            <artifactId>curator-recipes</artifactId>
	            <version>2.13.0</version>
       		 </dependency>
		<!-- 引入zookeeper的依赖 -->
		<dependency>
		    <groupId>com.101tec</groupId>
		    <artifactId>zkclient</artifactId>
		    <version>0.10</version>
		</dependency>

2.spring-dubbo.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="hello-world-app"  />
 
    <!-- 使用zookeeper广播注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
 
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.imooc.service.TestService" ref="testService" />
 
    <!-- 和本地bean一样实现服务 -->
    <bean id="testService" class="com.imooc.service.impl.TestServiceImpl" />
</beans>

3.接口代码:

package com.imooc.service;



public interface TestService {

	String sayHello(String str);


}

4.实现类代码

package com.imooc.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.alibaba.dubbo.config.annotation.Service;
import com.imooc.service.TestService;
//这里的注解为dubbo的service
@Service
public class TestServiceImpl implements TestService {

	@Override
	public String sayHello(String str) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return dateFormat.format(new Date()) + ": " + str;
	}



}

5.springboot启动类新增代码

@ImportResource({"classpath:config/spring-dubbo.xml"})

Consumer:客户端

1.引用jar包

<!-- Dubbo依赖 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
            
	         <dependency>
				<groupId>org.apache.curator</groupId>
				<artifactId>curator-framework</artifactId>
				<version>4.0.1</version>
			</dependency>
			 <!-- zookeeper的客户端依赖 -->
            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>${zkclient.version}</version>
            </dependency>
			<dependency>
			    <groupId>io.netty</groupId>
			    <artifactId>netty-all</artifactId>
			    <version>4.1.32.Final</version>
			</dependency>

2.spring-dubbo.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="consumer-of-helloworld-app"  />
 
    <!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
 
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="testService" interface="com.imooc.service.TestService"  lazy="true" />   
</beans>

3.新建与服务端同样的接口,用于在接口调用前生成代理

4.在启动类同样新增扫描配置

@ImportResource({"classpath:config/spring-dubbo.xml"})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值