1.注意starter版本适配
2.服务提供者
创建Maven项目 boot-user-service-provider 服务提供者
2.1.通用模块依旧照用
2.2.POM
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 通用模块 -->
<dependency>
<groupId>com.tianxia</groupId>
<artifactId>api-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--dubbo的starter-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
2.3.application.properites
## 指定当前服务
dubbo.application.name=boot-user-service-provider
## 指定注册中心的位置
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
## 指定通信规则
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
## 连接监控中心
dubbo.monitor.protocol=registry
2.4.启动类
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 启动类
* @author liqb
* @date 2023-05-28 21:36
*/
@EnableDubbo // 开启基于注解的dubbo功能
@SpringBootApplication
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
2.5.业务类
import com.tianxia.dubbo.api.UserService;
import com.tianxia.dubbo.entity.UserAddress;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
/**
* 用户服务实现类
* @author liqb
* @date 2023-05-28 16:48
*/
@Service
@com.alibaba.dubbo.config.annotation.Service //
public class UserServiceImpl implements UserService {
/**
* 按照用户id返回所有的收货地址
* @author liqb
* @date 2023-05-28 16:48
* @param userId 用户id
* @return
*/
@Override
public List<UserAddress> getUserAddressList(String userId) {
UserAddress address1 = new UserAddress(1, "河南省郑州巩义市宋陵大厦2F", "1", "安然", "150360313x", "Y");
UserAddress address2 = new UserAddress(2, "北京市昌平区沙河镇沙阳路", "1", "情话", "1766666395x", "N");
return Arrays.asList(address1,address2);
}
}
启动注册中心,启动当前服务提供者,可以在浏览器看到一个服务提供者。
3.服务消费者
创建Maven项目 boot-order-service-consumer 服务消费者
3.1.通用模块依旧照用
3.2.POM
<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 通用模块 -->
<dependency>
<groupId>com.tianxia</groupId>
<artifactId>api-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--dubbo的starter-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
3.3.application.properties
## 端口号
server.port=8081
## 指定当前服务
dubbo.application.name=boot-order-service-consumer
## 指定注册中心的位置
dubbo.registry.address=zookeeper://127.0.0.1:2181
#连接监控中心 注册中心协议
dubbo.monitor.protocol=registry
3.4.启动类
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 启动类
* @author liqb
* @date 2023-05-28 21:46
*/
@EnableDubbo // 开启基于注解的dubbo功能
@SpringBootApplication
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
}
3.5.业务类
import com.tianxia.dubbo.api.OrderService;
import com.tianxia.dubbo.entity.UserAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 订单控制器
* @author liqb
* @date 2023-05-28 21:29
*/
@RestController
public class OrderController {
@Autowired
OrderService orderService;
/**
* 初始化订单
* @author liqb
* @date 2023-05-28 21:35
* @param userId 用户id
* @return
*/
@GetMapping("/initOrder")
public List<UserAddress> initOrder(@RequestParam("uid") String userId) {
return orderService.initOrder(userId);
}
}
import com.alibaba.dubbo.config.annotation.Reference;
import com.tianxia.dubbo.api.OrderService;
import com.tianxia.dubbo.api.UserService;
import com.tianxia.dubbo.entity.UserAddress;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 订单服务实现类
* @author liqb
* @date 2023-05-28 16:54
*/
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
// @Autowired
@Reference
public UserService userService;
/**
* 初始化订单
* @author liqb
* @date 2023-05-28 16:52
* @param userId 用户id
*/
@Override
public List<UserAddress> initOrder(String userId) {
log.info("用户id:{}", userId);
// 查询用户的收货地址
List<UserAddress> userAddressList = userService.getUserAddressList(userId);
return userAddressList;
}
}
配置完毕,此时启动zookeeper注册中心及监控。
启动springboot配置的服务提供者和消费者
在浏览器输入 localhost:7001 查看结果
查询到地址信息
duboo的springboot整合配置完成。