dubbo springboot的整合
Posted 烂笔头收集者
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dubbo springboot的整合相关的知识,希望对你有一定的参考价值。
1. 新建springboot项目
在pom.xml中添加
<properties>
<java.version>1.8</java.version>
<spring-boot.version>2.1.1.RELEASE</spring-boot.version>
<dubbo.version>2.7.3</dubbo.version>
<revision>2.7.3</revision>
</properties>
<dependencyManagement>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Aapche Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<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>
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
</dependencies>
2.新建module起名:boot-mall-api
创建包bean和service接口
2.1 Bean--> UserAddress
package com.useable.mall.bootmallapi.bean;
import java.io.Serializable;
public class UserAddress implements Serializable {
private Integer id;
private String userAddress; // 用户地址
private String userId; // 用户id
private String consignee; // 收货人
private String phoneNum; // 联系电话
private String isDefault; // 是否默认地址 Y-是 N-否
public UserAddress() {
super();
}
public UserAddress(Integer id, String userAddress, String userId, String consignee, String phoneNum, String isDefault) {
this.id = id;
this.userAddress = userAddress;
this.userId = userId;
this.consignee = consignee;
this.phoneNum = phoneNum;
this.isDefault = isDefault;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getConsignee() {
return consignee;
}
public void setConsignee(String consignee) {
this.consignee = consignee;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getIsDefault() {
return isDefault;
}
public void setIsDefault(String isDefault) {
this.isDefault = isDefault;
}
@Override
public String toString() {
return "UserAddress{" +
"id=" + id +
", userAddress='" + userAddress + '\'' +
", userId='" + userId + '\'' +
", consignee='" + consignee + '\'' +
", phoneNum='" + phoneNum + '\'' +
", isDefault='" + isDefault + '\'' +
'}';
}
}
2.2 Service-->OrderService 和 UserService
package com.useable.mall.bootmallapi.service;
public interface OrderService {
/**
* 实现订单
* @param userId
*/
void initOrder(String userId);
}
package com.useable.mall.bootmallapi.service;
import com.useable.mall.bootmallapi.bean.UserAddress;
import java.util.List;
/**
* 用户接口
*/
public interface UserService {
/**
* 按照用户id返回收货地址
*
* @param userId
* @return
*/
public List<UserAddress> getUserAddressList(String userId);
}
3 创建新建module --> provider
3.1 其pom.xml中添加
<dependency>
<groupId>com.useable.mall</groupId>
<artifactId>boot-mall-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!-- Zookeeper dependencies -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.2</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
3.2 新建类UserServiceImpl 继承 UserService
这里的@Service 来自于dubbo 而不是传统的spring中的包 一定要注意
package com.useable.mall.dubbouserprovider.service;
import com.useable.mall.bootmallapi.bean.UserAddress;
import com.useable.mall.bootmallapi.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import java.util.Arrays;
import java.util.List;
// 这里一定要注意
@Service(version = "1.0.0")
public class UserServiceImpl implements UserService {
public List<UserAddress> getUserAddressList(String userId) {
UserAddress userAddress = new UserAddress(1, "北京昌平", "1", "李老师", "1300000000", "Y");
UserAddress userAddress2 = new UserAddress(2, "北京朝阳", "1", "王老师", "1300000001", "N");
return Arrays.asList(userAddress, userAddress2);
}
}
3.3 添加propertis内容
spring.application.name=boot-user-provider
dubbo.scan.base-packages=com.useable.mall.bootmallapi.service.UserService
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.registry.address=zookeeper://192.168.99.200:2181
3.4 修改启动类application
添加注解@EnableDubbo
package com.useable.mall.dubbouserprovider;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class DubboUserProviderApplication {
private final Logger logger = LoggerFactory.getLogger(DubboUserProviderApplication.class);
public static void main(String[] args) {
SpringApplication.run(DubboUserProviderApplication.class, args);
}
}
4. 创建新的module --> consumer
4.1 修改其pom.xml
<dependency>
<groupId>com.useable.mall</groupId>
<artifactId>boot-mall-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!-- Zookeeper dependencies -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.2</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
4.2 新建类OrderServiceImpl 继承接口OrderService
这里要暴露服务的话@Service还是需要使用dubbo的包
但此次示例并没有暴露服务 因此@Service使用的是spring的包
package com.useable.mall.bootorderconsumer.order.service;
import com.useable.mall.bootmallapi.bean.UserAddress;
import com.useable.mall.bootmallapi.service.OrderService;
import com.useable.mall.bootmallapi.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import java.util.List;
// 这里要暴露服务的话@Service还是需要使用dubbo的包
// 但此次示例并没有暴露服务 因此@Service使用的是spring的包
@Service
public class OrderServiceImpl implements OrderService {
@Reference(version = "1.0.0")
UserService userService;
public void initOrder(String userId) {
System.out.println("------------------开始调用远程服务接口");
// 查询用户的收货地址
List<UserAddress> userAddressList = userService.getUserAddressList(userId);
for (UserAddress ud :
userAddressList) {
System.out.println(ud);
}
System.out.println("--------------------调用完成");
}
}
4.3 修改properties文件
spring.application.name=boot-order-consumer
dubbo.registry.address=zookeeper://192.168.99.200:2181
server.port=8888
4.4 修改启动类Application
添加注解 @EnableDubbo
package com.useable.mall.bootorderconsumer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class BootOrderConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(BootOrderConsumerApplication.class, args);
}
}
以上是关于dubbo springboot的整合的主要内容,如果未能解决你的问题,请参考以下文章
dubbo远程调用(rpc)-->快速入门+管理控制台+整合Springboot开发
dubbo远程调用(rpc)-->快速入门+管理控制台+整合Springboot开发