dubbo入门学习-----dubbo整合springboot
Posted alimayun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dubbo入门学习-----dubbo整合springboot相关的知识,希望对你有一定的参考价值。
springboot节省了大量的精力去配置各种bean,因此通过一个简单的demo来整合springboot与dubbo
一、创建boot-user-service-provider
本篇博文基于上篇中的dubbo项目,整体工程如下:
1、pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.ty</groupId> <artifactId>boot-user-service-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <name>boot-user-service-provider</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.ty</groupId> <artifactId>gmall-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--引入springboot整合dubbo的jar包--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、application.properties
#dubbo项目名
dubbo.application.name=boot-user-service-providor
#注册中心地址
dubbo.registry.address=127.0.0.1:2181
#指定注册中心为zookeeper
dubbo.registry.protocol=zookeeper
#指定通信协议
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
#连接监控中心,去注册中心直接发现,不配地址
dubbo.monitor.protocol=registry
3、主类BootUserServiceProviderApplication
package com.ty; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; //开启基于注解的dubbo功能 @EnableDubbo @SpringBootApplication public class BootUserServiceProviderApplication public static void main(String[] args) SpringApplication.run(BootUserServiceProviderApplication.class, args);
4、UserServiceImpl
package com.ty.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.ty.bean.UserAddress; import com.ty.service.UserService; import java.util.Arrays; import java.util.List; //此@Service用的是dubbo的service注解 @Service public class UserServiceImpl implements UserService @Override public List<UserAddress> getUserAddressList(String userId) UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y"); UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座3层(深圳分校)", "1", "王老师", "010-56253825", "N"); return Arrays.asList(address1,address2);
5、测试
启动项目,另外还需要启动dubbo-admin,这样可以很直观的在监控中心查看provider、consumer的状态。
服务已经启动ok
二、创建boot-order-service-consumer
1、pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.ty</groupId> <artifactId>boot-order-service-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>boot-order-service-consumer</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <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> <dependency> <groupId>com.ty</groupId> <artifactId>gmall-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--引入springboot整合dubbo的jar包--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、application.properties
#应用名称
dubbo.application.name=boot-order-service-consumer
#注册中心地址(可以直接全地址)
dubbo.registry.address=zookeeper://127.0.0.1:2181
#在注册中心找监控
dubbo.monitor.protocol=registry
3、主类
package com.ty; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableDubbo @SpringBootApplication public class BootOrderServiceConsumerApplication public static void main(String[] args) SpringApplication.run(BootOrderServiceConsumerApplication.class, args);
4、OrderController
package com.ty.controller; import com.ty.bean.UserAddress; import com.ty.service.OrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class OrderController @Autowired private OrderService orderService; @RequestMapping("/initOrder") public List<UserAddress> initOrder(@RequestParam("uid") String uid) return orderService.initOrder(uid);
5、OrderServiceImpl
package com.ty.service.impl; import com.alibaba.dubbo.config.annotation.Reference; import com.ty.bean.UserAddress; import com.ty.service.OrderService; import com.ty.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * 1、将服务提供者注册到注册中心(暴露服务) * 1)、导入dubbo依赖(2.6.2)\\操作zookeeper的客户端(curator) * 2)、配置服务提供者 * * 2、让服务消费者去注册中心订阅服务提供者的服务地址 * @author lfy * */ @Service public class OrderServiceImpl implements OrderService //使用dubbo提供的@Reference注解引用远程服务 @Reference UserService userService; @Override public List<UserAddress> initOrder(String userId) // TODO Auto-generated method stub System.out.println("用户id:"+userId); //1、查询用户的收货地址 List<UserAddress> addressList = userService.getUserAddressList(userId); return addressList;
6、测试
启动服务,并且调用boot-order-service-consumer的controller,如下:
远程调用ok。
项目已经上传到了github上,地址:https://github.com/ali-mayun/dubbo
以上是关于dubbo入门学习-----dubbo整合springboot的主要内容,如果未能解决你的问题,请参考以下文章