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

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

主要内容以及实现的功能和上一篇博客类似,只是以springboot的配置来实现完成用户信息的获取。
可查看上一篇博客了解功能需求。
https://blog.csdn.net/qq_36654629/article/details/90052908
现在说一下整合的思想,这里一样是分别创建三个springboot工程
在这里插入图片描述
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模块
在这里插入图片描述

点击+号以后选择import Modules
在这里插入图片描述

在这里插入图片描述
点击ok,保存即可.
在这里插入图片描述
导入成功后
在这里插入图片描述

  • 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管理控制台
    在这里插入图片描述

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导入步骤一样。

在这里插入图片描述

  • 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);
    }

}

运行入口类以后,去浏览器访问接口
在这里插入图片描述
并可以在dubbo-admin中查看到
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

收破烂的小熊猫~

你的鼓励将是我创造最大的东西~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值