微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

2021-04-05-SpringBoot整合Dubbo

前言

  • 服务提供者是一个项目,服务消费者是一个项目,提供者没有controller层,消费者只有controller
  • 使用消费者层调用提供者层展现dubbo是一个RPC远程通信框架的本质

服务提供者

<dependency>
   <groupId>com.alibaba.boot</groupId>
   <artifactId>dubbo-spring-boot-starter</artifactId>
   <version>0.2.0</version>
</dependency>

  • 服务接口
public interface IUserService {
    public List<User> getUserAll();
}

  • 服务实现类
@com.alibaba.dubbo.config.annotation.Service // 发布服务要用dubbo下面的注解
public class UserServieImpl implements IUserService {
  
    @Override
    public List<User> getUserAll() {
        System.out.println("UserServieImpl.getUserAll 4 1 2");
        return null;
    }
}

spring:
  datasource:
    password: root
    username: root
    driver-class-name: com.MysqL.jdbc.Driver
    url: jdbc:MysqL://localhost:3306/web_1
mybatis-plus:
  type-aliases-package: com.qf.entity
dubbo:
  application:
    name: user-service
  registry:
    address: zookeeper://192.168.148.200:2181
  protocol:
    port: -1
  • 主启动类
@SpringBootApplication(scanBasePackages = "com.qf")
//暴露的服务要让dubbo扫到
@dubboComponentScan(basePackages = "com.qf.service.impl")
public class UserServiceUserApplication {

   public static void main(String[] args) {
      SpringApplication.run(UserServiceUserApplication.class, args);
   }
}

服务消费者

<dependency>
   <groupId>com.alibaba.boot</groupId>
   <artifactId>dubbo-spring-boot-starter</artifactId>
   <version>0.2.0</version>
</dependency>

@Controller
public class UserController {

    @Reference
    private IUserService userService;

    @RequestMapping(value = "/getUserList")
    public String  getUserList(Model model) throws Exception {
        System.out.println("UserController.getUserList");
     //   userService.getUserAll();
        return "userList";
    }
}

  • 主启动类上面加上忽略数据源的,因为数据源服务提供者层配了,不关消费者的事情
    @SpringBootApplication(scanBasePackages = “com.qf”,exclude = DataSourceAutoConfiguration.class)
  • 服务消费者配置
dubbo:
  application:
    name: user-web
  registry:
    address: zookeeper://192.168.148.200:2181
  consumer:
    check: false # 关闭所有服务的启动时检查:(认没有提供者时报错)  写在定义服务消费者一方
    retries: 3 #失败重试次数

运行项目

  • 先运行提供者项目,再运行消费者项目,在访问消费者的controller,看看有没有调用服务提供者的方法

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐