springBoot整合dubbo
- 2019 年 12 月 30 日
- 筆記
文章目录
SpringBoot整合dubbo
搭建项目
- 创建springBoot项目,导入dubbo依赖
123456 |
<!– 添加dubbo的启动器, 其中已经添加了zookepper的依赖–><dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version></dependency> |
---|
- 配置dubbo的连接和zookeeper的连接,在application.properties中添加如下配置
1234567 |
# 启动的端口号server.port=8001server.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)即可自动暴露。如下:
1234567891011121314151617 |
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
即可引用对应的服务
12345678910 |
@RestControllerpublic 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
1234567891011121314151617181920212223242526 |
<!–之前的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已经管理了版本):
12345678910 |
<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、新建一个启动类
123456 |
@SpringBootApplication //标记为springBoot的启动类public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }} |
---|
maven打包springBoot注意
1、在pom文件中添加如下依赖(如果不添加如下依赖,可能打出的jar包运行将会报错找不到主程序清单):
12345678910111213141516 |
<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> |
---|