美文网首页
基于SpringBoot的Dubbo启动

基于SpringBoot的Dubbo启动

作者: 攻城老狮 | 来源:发表于2021-09-23 10:54 被阅读0次

dubbo框架


image.png

1 模块添加

  1. 新建三个模块,分别表示消息的提供者,消息的消费者和公共模块
image-20210923104204300.png
  • dubbo-parent的pom

说明:加入springboot的父依赖,作为版本控制

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.6.RELEASE</version>
</parent>
  • service-provider的pom
 <dependencies>
   <dependency> <!-- 公共模块的依赖 -->
     <groupId>com.yqj</groupId>
     <artifactId>common</artifactId>
     <version>1.0-SNAPSHOT</version>
   </dependency>
   <dependency> <!-- dubbo的依赖 -->
     <groupId>com.alibaba.boot</groupId>
     <artifactId>dubbo-spring-boot-starter</artifactId>
     <version>0.2.0</version>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
   </dependency>
</dependencies>
  • service-consumer的pom
<dependencies>
  <dependency> <!-- 公共模块的依赖 -->
    <groupId>com.yqj</groupId> 
    <artifactId>common</artifactId>
    <version>1.0-SNAPSHOT</version>
  </dependency>
  <dependency> <!-- dubbo的依赖 -->
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>0.2.0</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>

2 消息提供者模块

  1. 主启动类
@EnableDubbo //扫描dubbo的组件
@SpringBootApplication
public class UserServiceProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserServiceProviderApplication.class,args);
    }
}
  1. 服务接口(定义在common公共模块里面)目的是可以让其他模块得到依赖
public interface UserService {
    public String getUserAddress(String userId);
}
  1. 服务实现
@Service //dubbo的service注解,说明该类是一个资源
@Component
public class UserServiceImpl implements UserService {

    @Override
    public String getUserAddress(String userId) {
        String url = "dubbo localhost " + userId;
        return url;
    }
}
  1. 配置文件
server:
  port: 9001

dubbo:
  application:
    name: user-service-provider
  registry:
    protocol: zookeeper
    address: localhost:2181
  protocol:
    name: dubbo
    port: 20880
  monitor:
    protocol: registry

3 消息消费者

  1. 主启动类
@EnableDubbo
@SpringBootApplication
public class ServiceConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceConsumerApplication.class,args);
    }
}
  1. 服务接口
public interface OrderService {
    public String getRemoteUserAddress(String userId);
}
  1. 服务实现
@Service
public class OrderServiceImpl implements OrderService {

    @Reference //远程注入
    private UserService userService; //从公共模块中导入 UserService

    @Override
    public String getRemoteUserAddress(String userId) {
        return userService.getUserAddress(userId);
    }
}
  1. controller调用服务的方法
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/{userId}")
    public String getRemoteUserAddress(@PathVariable("userId") String userId) {
        return orderService.getRemoteUserAddress(userId);
    }
}
  1. 配置文件
server:
  port: 8001
dubbo:
  application:
    name: order-service-consumer
  registry:
    address: zookeeper://localhost:2181
  monitor:
    protocol: registry

4 测试

  1. 启动zookeeper
  2. 访问网页测试 http://localhost:8001/order/12

相关文章

网友评论

      本文标题:基于SpringBoot的Dubbo启动

      本文链接:https://www.haomeiwen.com/subject/wjnngltx.html