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

相关文章
|
4月前
|
前端开发 Java 数据库
SpringBoot入门 - 对Hello world进行MVC分层
SpringBoot入门 - 对Hello world进行MVC分层
82 3
SpringBoot入门 - 对Hello world进行MVC分层
|
4月前
|
Java 数据库连接 测试技术
SpringBoot入门 - 添加内存数据库H2
SpringBoot入门 - 添加内存数据库H2
164 3
SpringBoot入门 - 添加内存数据库H2
|
4月前
|
Java 应用服务中间件 数据库连接
SpringBoot入门(2) - SpringBoot HelloWorld
SpringBoot入门(2) - SpringBoot HelloWorld
59 2
SpringBoot入门(2) - SpringBoot HelloWorld
|
4月前
|
Java 中间件
SpringBoot入门(6)- 添加Logback日志
SpringBoot入门(6)- 添加Logback日志
146 5
|
4月前
|
前端开发 Java 数据库
SpringBoot入门(3) - 对Hello world进行MVC分层
SpringBoot入门(3) - 对Hello world进行MVC分层
54 4
|
3月前
|
SpringCloudAlibaba Dubbo Java
【SpringCloud Alibaba系列】Dubbo基础入门篇
Dubbo是一款高性能、轻量级的开源Java RPC框架,提供面向接口代理的高性能RPC调用、智能负载均衡、服务自动注册和发现、运行期流量调度、可视化服务治理和运维等功能。
【SpringCloud Alibaba系列】Dubbo基础入门篇
|
3月前
|
Java 开发者 微服务
Spring Boot 入门:简化 Java Web 开发的强大工具
Spring Boot 是一个开源的 Java 基础框架,用于创建独立、生产级别的基于Spring框架的应用程序。它旨在简化Spring应用的初始搭建以及开发过程。
121 7
Spring Boot 入门:简化 Java Web 开发的强大工具
|
4月前
|
Java 应用服务中间件 数据库连接
SpringBoot入门 - SpringBoot HelloWorld
SpringBoot入门 - SpringBoot HelloWorld
SpringBoot入门 - SpringBoot HelloWorld
|
4月前
|
Java Spring
SpringBoot入门 - 定制自己的Banner
SpringBoot入门 - 定制自己的Banner
55 2
SpringBoot入门 - 定制自己的Banner
|
4月前
|
缓存 IDE Java
SpringBoot入门(7)- 配置热部署devtools工具
SpringBoot入门(7)- 配置热部署devtools工具
250 1
SpringBoot入门(7)- 配置热部署devtools工具