dubbo的入门学习(三)springboot整合dubbo

简介: dubbo的入门学习(三)springboot整合dubbo

dubbo的入门学习(三)springboot整合dubbo

主要内容以及实现的功能和上一篇博客类似,只是以springboot的配置来实现完成用户信息的获取。

可查看上一篇博客了解功能需求。

https://blog.csdn.net/qq_36654629/article/details/90052908

现在说一下整合的思想,这里一样是分别创建三个springboot工程

20200401134307494.png

gmall-interface和之前一样,仍然是存放公共接口,这里就不提供代码了,和上一篇博客一样。所以创建另外两个项目的时候,得导入这个模块,不然添加依赖会报错。

boot-user-service:用户服务模块

  • POI配置
<?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.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springboot.dubbo</groupId>
    <artifactId>boot-user-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot-user-service</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.atguigu.gmall</groupId>
            <artifactId>gmall-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <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>

导入gmall-interface模块

20200401134307494.png

点击+号以后选择import Modules

20200401134307494.png

20200401134307494.png

点击ok,保存即可.

20200401134307494.png

导入成功后

20200401134307494.png

  • service
    注意下面的@Service 导包时引入的是dubbo的路径
package com.springboot.dubbo.bootuserservice.service.impl;
import java.util.Arrays;
import java.util.List;
import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.gmall.bean.UserAddress;
import com.atguigu.gmall.service.UserService;
import org.springframework.stereotype.Component;
@Service //暴露接口
@Component
public class UserServiceImpl implements UserService{
    public List<UserAddress> getUserAddressList(String userId) {
        // TODO Auto-generated method stub
        System.out.println("UserServiceImpl.....old...");
        // TODO Auto-generated method stub
        UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y");
        UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座3层(深圳分校)", "1", "王老师", "010-56253825", "N");
    /*try {
      Thread.sleep(4000);
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }*/
        return Arrays.asList(address1,address2);
    }
}
  • application.properties配置文件
#指定当前服务/应用名字(同样的服务名字相同,不要和别的服务同名
dubbo.application.name=user-service-provider
#指定注册中心的位置
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
#指定通信规则(通信协议、通信端口)
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
#表示从注册中心发现监控中心地址,否则直连监控中心
dubbo.monitor.protocol=registry
#dubbo.scan.base-packages=com.spring
  • BootUserServiceApplication.java 启动入口
package com.springboot.dubbo.bootuserservice;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo //开启基于注解的dubbo功能
@SpringBootApplication
public class BootUserServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(BootUserServiceApplication.class, args);
    }
}

运行后查看 dubbo-admin管理控制台

20200401134307494.png

boot-order-service:订单服务模块

  • POI配置
<?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.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springboot.order-service</groupId>
    <artifactId>boot-order-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot-order-service</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.atguigu.gmall</groupId>
            <artifactId>gmall-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <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>

然后导入gmall-interface模块,和boot-user-service导入步骤一样。

20200401134307494.png

  • service
    使用 @Reference 替换了之前的@Autowired注解
package com.springboot.orderservice.bootorderservice.service.impl;
import java.util.List;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import com.atguigu.gmall.bean.UserAddress;
import com.atguigu.gmall.service.OrderService;
import com.atguigu.gmall.service.UserService;
@Service
public class OrderServiceImpl implements OrderService{
  //@Autowired
  @Reference //远程引用userService服务
  UserService userService;
  public List<UserAddress> initOrder(String userId) {
    // TODO Auto-generated method stub
    //1、查询用户的收货地址
    List<UserAddress> addressList = userService.getUserAddressList(userId);
    return addressList;
  }
}
  • controller
package com.springboot.orderservice.bootorderservice.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.gmall.bean.UserAddress;
import com.atguigu.gmall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
 * @author JP
 * @title: OrderController
 * @projectName boot-order-service
 * @description:
 * @date 2019/5/10 0010
 */
@RestController
public class OrderController {
    @Autowired
    OrderService orderService;
    @RequestMapping("/getOrder")
    public List<UserAddress> getList(String uid){
        System.out.println("用户id:"+uid);
        List<UserAddress> addressList = orderService.initOrder(uid);
        for (UserAddress userAddress : addressList) {
            System.out.println(userAddress.getUserAddress());
        }
        return addressList;
    }
}
  • application.properties
#申明消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样
dubbo.application.name=order-service-consumer
#使用zookeeper广播注册中心暴露发现服务地址
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper
#表示从注册中心发现监控中心地址,否则直连监控中心。
dubbo.monitor.protocol=registry
#dubbo.scan.base-packages=com.spring
#这里改了一下tomcat启动端口,因为dubbo监控中心启动时使用了jetty启动,占用了8080端口
server.port=8081
  • BootOrderServiceApplication 启动入口
package com.springboot.orderservice.bootorderservice;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo //开启dubbo注解
@SpringBootApplication
public class BootOrderServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(BootOrderServiceApplication.class, args);
    }
}

运行入口类以后,去浏览器访问接口

20200401134307494.png

并可以在dubbo-admin中查看到

20200401134307494.png

相关文章
|
3天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的学习自律养成小程序的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的学习自律养成小程序的详细设计和实现(源码+lw+部署文档+讲解等)
|
2天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue的学生网课学习效果评价的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue的学生网课学习效果评价的详细设计和实现(源码+lw+部署文档+讲解等)
16 7
|
8天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue的中医学习服务管理系统的详细设计和实现
基于SpringBoot+Vue的中医学习服务管理系统的详细设计和实现
29 13
|
3天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue的学习网站系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue的学习网站系统的详细设计和实现(源码+lw+部署文档+讲解等)
19 5
|
8天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue的在线学习过程管理系统软件的详细设计和实现
基于SpringBoot+Vue的在线学习过程管理系统软件的详细设计和实现
38 12
|
8天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的诗词学习系统的详细设计和实现
基于SpringBoot+Vue+uniapp的诗词学习系统的详细设计和实现
28 11
|
8天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue的大学生国学自主学习平台的详细设计和实现
基于SpringBoot+Vue的大学生国学自主学习平台的详细设计和实现
32 10
|
1天前
|
设计模式 前端开发 Java
【Spring MVC】快速学习使用Spring MVC的注解及三层架构
【Spring MVC】快速学习使用Spring MVC的注解及三层架构
6 1
|
3天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的高校学习助手小程序的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的高校学习助手小程序的详细设计和实现(源码+lw+部署文档+讲解等)
|
9天前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp小程序的线上学习资源智能推荐系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp小程序的线上学习资源智能推荐系统附带文章源码部署视频讲解等
19 5