【四】Dubbo整合Springboot

本文介绍了如何将Dubbo与Springboot进行整合,首先讲述了预备工作,包括项目背景和相关组件的部署。接着,详细说明了如何建立Consumer和Provider项目,分离服务接口与实现。在Provider项目中配置了pom文件和applicationProvider.xml,而在Consumer项目中同样配置并更改了端口。最后,展示了项目运行的效果,包括Consumer成功订阅Zookeeper服务以及用户查询功能的实现。

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

1. 预备工作

这篇是基于之前springboot+mybatis+shiro+redis等完成的,也就目录的置顶系列

同时关于Dubbo、Zookeeper和Dubbo-Admin部署可以看点击这里

2. 建立Consumer项目和Provider项目

将原有的项目中的service实现和所依赖的Dao层,Po实体抽出,当作Provider项目。

将原有的项目中的service实现删去,因为之后实现是由Provider项目实现的。

3. Provider项目代码

首先是pom代码

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.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">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.kfit</groupId>
  <artifactId>springboot-provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springboot-provider</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.7</java.version>
	<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
	<mysql-connector>5.1.39</mysql-connector>
	<spring-boot-starter-redis-version>1.3.2.RELEASE</spring-boot-starter-redis-version>
  </properties>
	
	<!-- 
		spring boot 父节点依赖,
		引入这个之后相关的引入就不需要添加version配置,
		spring boot会自动选择最合适的版本进行添加。
	 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.1.RELEASE</version>
	</parent>		

  <dependencies>
    
    	<!-- spring boot web支持:mvc,aop... -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
        <!--dubbo start-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.8</version>
            <exclusions>
                <exclusion>
                    <artifactId>activation</artifactId>
                    <groupId>javax.activation</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>mail</artifactId>
                    <groupId>javax.mail</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- dubbo end -->
        <!-- Spring Boot Redis 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
            <version>${spring-boot-starter-redis-version}</version>
        </dependency>	
		<!-- thmleaf模板依赖. -->
		<dependency>
		  <groupId>org.springframework.boot</groupId>
		  <artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>  
    		<groupId>com.github.theborakompanioni</groupId>  
    		<artifactId>thymeleaf-extras-shiro</artifactId>  
   			<version>1.2.1</version>  
		</dependency>  		
		
		<!-- shiro spring. -->
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-spring</artifactId>
			<version>1.2.2</version>
		</dependency>
		<!-- Spring Boot Mybatis 依赖 -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>${mybatis-spring-boot}</version>
		</dependency>		
		
		<!-- MySQL 连接驱动依赖 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql-connector}</version>
		</dependency>
		
		
		<!-- shiro ehcache -->
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-ehcache</artifactId>
			<version>1.2.2</version>
		</dependency>
		

		
  </dependencies>
</project>

在这里强调一下,有两种整合方式,xml/注解。但是我用注解方式试了很多次都失败,应该是包冲突问题。所以这里我用xml配置方式。

在Resource下创建一个applicationProvider.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://code.alibabatech.com/schema/dubbo"
	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-demo" />
	<!-- zookeeper注册中心 -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181" />
	<dubbo:protocol name="dubbo" port="20880" />
	
	<!-- 和本地bean一样实现服务 --> 
	<bean id="userInfoService" class="com.kfit.core.service.impl.UserInfoServiceImpl" />

	<!-- 向注册中心注册暴漏服务地址,注册服务 -->
	<dubbo:service interface="com.kfit.core.service.UserInfoService"
		ref="userInfoService" executes="10"  />

</beans>
创建完之后,在主程序添加ImportResource,如下
package com.kfit;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;


@SpringBootApplication
@ImportResource(
        locations = {"classpath*:applicationProvider.xml"}
)
public class provider {
	

	public static void main(String[] args) {
		SpringApplication.run(provider.class, args);
	}
	
}

然后启动Provider,看是否注册到zookeeper(记得打开zookeeper,链接里有说明)可以打开Dubbo Admin界面,看服务者和消费者数量,如果成功的话继续配置Consumer。成功的Dubbo Admin界面显示如下


4. Consumer项目代码

pom代码和Provider一样。记得改下Consumer端口,避免和Provider重复

同样在Resource下创建applicationConsumer.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://code.alibabatech.com/schema/dubbo"
	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="consumer-of-dubbo-demo" />

	<dubbo:registry address="zookeeper://127.0.0.1:2181" />
	<dubbo:annotation package="com.kfit.*"/>

	<!-- 向注册中心订阅服务 -->
	<dubbo:reference id="userInfoService" interface="com.kfit.core.service.UserInfoService" />
</beans> 
同样,在主程序更改如下

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;


@SpringBootApplication
@ImportResource(
        locations = {"classpath*:applicationConsumer.xml"}
)
public class Consumer {
	

	public static void main(String[] args) {
		SpringApplication.run(Consumer.class, args);
	}
	
}

获取bean和以前一样。

	@Autowired
	private UserInfoService userInfoService;

注意Consumer的接口是保留的,接口的实现是从Provider拿的。如果不保留接口,就在pom里把Provider的jar打包进来。

启动之后,再观察Consumer是否从Zookeeper订阅,Dubbo-Admin如下,可以看到消费者订阅服务了。



5. 项目效果

首先是登陆界面


登陆进去之后选择用户查询,输入id,输出查询结果


看Provider后台,显示

INFO 3244 --- [:20880-thread-4] c.k.c.service.impl.UserInfoServiceImpl   : 用户插入缓存 >> id: 1, username: admin,password: 123456

再次查询,显示

INFO 3244 --- [:20880-thread-6] c.k.c.service.impl.UserInfoServiceImpl   : 从缓存中获取了用户 >> id: 1, username: admin,password: 123456

说明Consumer和Provider利用Zookeeper通信成功。







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值