首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springBoot整合dubbo

springBoot整合dubbo

作者头像
爱撒谎的男孩
修改2019-12-31 11:45:02
1.4K0
修改2019-12-31 11:45:02
举报
文章被收录于专栏:码猿技术专栏码猿技术专栏

文章目录

  1. 1. SpringBoot整合dubbo
    1. 1.1. 搭建项目
    2. 1.2. 暴露服务
    3. 1.3. 引用服务
    4. 1.4. maven聚合springBoot项目
      1. 1.4.1. maven创建springBoot工程
      2. 1.4.2. maven打包springBoot注意

SpringBoot整合dubbo

搭建项目

  • 创建springBoot项目,导入dubbo依赖
<!-- 添加dubbo的启动器, 其中已经添加了zookepper的依赖-->
<dependency>
	<groupId>com.alibaba.boot</groupId>
	<artifactId>dubbo-spring-boot-starter</artifactId>
	<version>0.2.0</version>
</dependency>
  • 配置dubbo的连接和zookeeper的连接,在application.properties中添加如下配置
# 启动的端口号
server.port=8001
server.servlet.context-path=/provider
# 暴露的服务名称
dubbo.application.name=user-service
# zookeeper注册中心的地址
dubbo.registry.address=zookeeper://39.105.123.197:2181
  • 在主配置类上添加@EnableDubbo注解,开启dubbo

暴露服务

  • 在spring中使用的<dubbo:service>暴露服务,但是在springBoot中只需要使用dubbo的注解@Service(com.alibaba.dubbo.config.annotation.Service)即可自动暴露。如下:
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
import cn.tedu.demo.beans.User;
import cn.tedu.demo.service.UserService;
@Service //暴露服务,只需要在对应的服务类上添加这个注解即可
@Component   //注入到IOC容器中
public class UserServiceImpl implements UserService{
	@Override
	public User getUser(Integer userId) {
		User user=new User();
		user.setAge(22);
		user.setUserId(userId);
		user.setUserName("陈加兵");
		return user;
	}
}

引用服务

  • 在spring的配置文件中使用<dubbo:reference>引用服务,但是在springBoot中只需要使用dubbo的注解@Reference即可引用对应的服务
@RestController
public class UserController {
	@Reference  //消费者引用提供者提供的服务,相当于<dubbo:reference>
	private UserService userService;
	
	@GetMapping("/user/get/{id}")
	public User get(@PathVariable("id")Integer userId){
		return userService.getUser(userId);
	}
}

maven聚合springBoot项目

  • 项目地址-点击下载
  • 创建demo-parent父项目管理版本,但是在springBoot项目中也是使用父项目管理的,因此我们需要在父项目中使用springBoot的依赖管理的starter来替代之前的parent
<!--之前的parent,在springBoot创建的时候将会添加,但是这里不需要
<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.5.RELEASE</version>
		<relativePath /> 
</parent>
-->
	<properties>
		<springBoot-version>2.0.5.RELEASE</springBoot-version>
	</properties>
	<dependencyManagement>
		<dependencies>
			<!-- 直接使用这个依赖管理springBoot的版本即可 -->
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<version>${springBoot-version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
  • demo-parent新建module
  • 在新的module的pom.xml直接添加springBoot的启动器即可,不用指定版本

maven创建springBoot工程

1、新建一个module,打包方式为jar

2、添加依赖,如下(直接添加依赖,因为父工程demo-parent已经管理了版本):

 <dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

3、在src/main/resources包的下新建springBoot的配置文件application.properties

4、新建一个启动类

@SpringBootApplication  //标记为springBoot的启动类
public class DemoApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

maven打包springBoot注意

1、在pom文件中添加如下依赖(如果不添加如下依赖,可能打出的jar包运行将会报错找不到主程序清单):

<build>
		<!-- <finalName>batman-web</finalName> -->
		<plugins>           
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>             
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-10-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SpringBoot整合dubbo
    • 搭建项目
      • 暴露服务
        • 引用服务
          • maven聚合springBoot项目
            • maven创建springBoot工程
            • maven打包springBoot注意
        相关产品与服务
        项目管理
        CODING 项目管理(CODING Project Management,CODING-PM)工具包含迭代管理、需求管理、任务管理、缺陷管理、文件/wiki 等功能,适用于研发团队进行项目管理或敏捷开发实践。结合敏捷研发理念,帮助您对产品进行迭代规划,让每个迭代中的需求、任务、缺陷无障碍沟通流转, 让项目开发过程风险可控,达到可持续性快速迭代。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档