一、服务提供者boot-user-service-provider

服务提供者boot-user-service-provider代码结构如下:

1、服务提供者boot-user-service-provider引入spring-boot-starter以及dubbo和curator的依赖

注意starter版本适配:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
</dependency>

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

引入spring-boot-starter以及dubbo和curator的依赖后的pom.xml文件内容如下:
<?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> <groupId>com.lina02.gmall</groupId>
<artifactId>boot-user-service-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>boot-user-service-provider</name>
<description>服务提供者</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>com.lina02.gmall</groupId>
<artifactId>gmall-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

2、配置文件application.properties

#服务名,不能跟别的dubbo服务提供者重复
dubbo.application.name=boot-user-service-provider
#指定注册中心的地址和端口号
dubbo.registry.address=127.0.0.1:2181
#指定注册中心协议
dubbo.registry.protocol=zookeeper #指定通信规则(通信协议&通信端口)
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880 #连接监控中心
dubbo.monitor.protocol=registry

3、UserService接口实现UserServiceImpl

package com.lina02.gmall.service.impl;

import com.lina02.gmall.bean.UserAddress;
import com.lina02.gmall.service.UserService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import java.util.Arrays;
import java.util.List; @com.alibaba.dubbo.config.annotation.Service //暴露服务
//@Service
@Component
public class UserServiceImpl implements UserService { @Override
public List<UserAddress> getUserAddressList(String userId) {
System.out.println("UserServiceImpl..3.....");
UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y");
UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座3层(深圳分校)", "1", "王老师", "010-56253825", "N"); return Arrays.asList(address1,address2);
}
}

4、主程序

package com.lina02.gmall;

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 BootUserServiceProviderApplication { public static void main(String[] args) {
SpringApplication.run(BootUserServiceProviderApplication.class, args);
}
}

二、服务消费者boot-order-service-consumer

服务消费者boot-order-service-consumer代码结构如下:

1、pom.xml

<?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> <groupId>com.lina02.gmall</groupId>
<artifactId>boot-order-service-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>boot-order-service-consumer</name>
<description>服务消费者</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>com.lina02.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> <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>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

2、配置文件application.propertise

#配置服务端口
server.port=8081 #服务名,不能跟别的dubbo服务提供者重复
dubbo.application.name=boot-order-service-consumer
#指定注册中心协议、地址、端口号
dubbo.registry.address=zookeeper://127.0.0.1:2181 #连接监控中心
dubbo.monitor.protocol=registry

3、OrderService接口实现OrderServiceImpl

package com.lina02.gmall.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lina02.gmall.bean.UserAddress;
import com.lina02.gmall.service.OrderService;
import com.lina02.gmall.service.UserService;
import org.springframework.stereotype.Service; import java.util.List; /**
* 1、将服务提供者注册到注册中心(暴露服务)
* 1)、导入dubbo依赖(2.6.2)\操作zookeeper的客户端(curator)
* 2)、配置服务提供者
*
* 2、让服务消费者去注册中心订阅服务提供者的服务地址
*/
@Service
public class OrderServiceImpl implements OrderService { //@Autowired
@Reference //订阅的服务
UserService userService; @Override
public List<UserAddress> initOrder(String userId) {
System.out.println("用户id:"+userId);
//1、查询用户的收货地址
List<UserAddress> addressList = userService.getUserAddressList(userId);
return addressList;
}
}

4、OrderController

package com.lina02.gmall.controller;

import com.lina02.gmall.bean.UserAddress;
import com.lina02.gmall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller
public class OrderController { @Autowired
OrderService orderService; @ResponseBody
@RequestMapping("/initOrder")
public List<UserAddress> initOrder(@RequestParam("uid") String userId){
return orderService.initOrder(userId);
}
}

5、主程序

package com.lina02.gmall;

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 BootOrderServiceConsumerApplication { public static void main(String[] args) {
SpringApplication.run(BootOrderServiceConsumerApplication.class, args);
}
}

dubbo-springboot的更多相关文章

  1. ZooKeeper+Dubbo+SpringBoot 微服务Demo搭建

    1. 首先创建springBoot项目,springBoot是一堆组件的集合,在pom文件中对需要的组件进行配置.生成如下目录结构 创建test项目,同步在test创建dubbo-api,dubbo- ...

  2. Zookeeper+dubbo+Springboot集成总结

    1. 尽量用XML 集成,这也的Dubbo官方推荐的集成方式 自己在使用注解集成过程中发现有坑:Springmvc包扫描和dubbo包扫描冲突,导致消费端一直拿不到代理对象(null),非常蛋疼,所以 ...

  3. Springboot 整合 Dubbo/ZooKeeper 详解 SOA 案例

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢!   “看看星空,会觉得自己很渺小,可能我们在宇宙中从来就是一个偶然.所以,无论什么事情,仔细想一 ...

  4. Spring-boot:5分钟整合Dubbo构建分布式服务

    概述: Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合).从服务模型的角度来看,Dubbo采用的是一种非常 ...

  5. IDEA上创建 Maven SpringBoot + zookeeper +dubbo 实例

    概述 首先声明,本文是学习总结类型的博客内容,如有雷同纯属学习.本位主要结合zookeeper和dubbo做个简单实例.目前来说,一般网站架构随着业务的发展,逻辑越来越复杂,数据量越来越大,交互越来越 ...

  6. SpringBoot开发案例之整合Dubbo分布式服务

    前言 在 SpringBoot 很火热的时候,阿里巴巴的分布式框架 Dubbo 不知是处于什么考虑,在停更N年之后终于进行维护了.在之前的微服务中,使用的是当当维护的版本 Dubbox,整合方式也是使 ...

  7. SpringBoot整合dubbo

    Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成. 以上介绍来源于百度百科,具体dubbo相关可以自行查 ...

  8. SpringBoot 整合使用dubbo

    这里主要是按照teaey作者的spring-boot-starter-dubbo框架进行一些变化的使用 依赖包: <dependency> <groupId>com.aliba ...

  9. spring-boot 速成(7) 集成dubbo

    github上有一个开源项目spring-boot-starter-dubbo 提供了spring-boot与dubbo的集成功能,直接拿来用即可.(记得给作者点赞,以示感谢!) 下面是使用步骤,先看 ...

  10. springboot与dubbo结合

    转:http://www.cnblogs.com/Alandre/p/6490142.html  写的很好! 本文提纲 一.为啥整合 Dubbo 实现 SOA 二.运行 springboot-dubb ...

随机推荐

  1. hadoop集群异常问题总结

    1. Could not find or load main class java.library.path=.opt.hadoop.lib 我的环境上是 hadoopopts变量的配置问题,至于为啥 ...

  2. SQL:内连接、左外连接、右外连接、全连接、交叉连接区别

    有两个表A和表B.表A结构如下: Aid:int:标识种子,主键,自增ID Aname:varchar 数据情况,即用select * from A出来的记录情况如下图1所示: 图1:A表数据表B结构 ...

  3. 修改SO

    1.如果在jni中定义的是int型的数据,比如: JNIEXPORT jint JNICALL Java_com_ggndktest1_JniGg_getCoin (JNIEnv * env, job ...

  4. 「LuoguP3379」 【模板】最近公共祖先(LCA)

    题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...

  5. phpStorm的远端部署

    首先远端服务器的路径: /var/www -rwxrwxrwx jiangzhaowei jiangzhaowei 6月 index.html* lr-xr-xr-x root root 2月 php ...

  6. HDU2844(多重部分和)

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. servlet里的forward和redirect的区别

    forward方式:request.getRequestDispatcher("/somePage.jsp").forwardrequest, response);     red ...

  8. 【221】◀▶ IDL GUI 函数说明

    参考:GUI - Dialogs Routines参考:GUI - Widgets Routines参考:GUI - Compound Widgets Routines 01   DIALOG_MES ...

  9. CF-796C

    C. Bank Hacking time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  10. yzm10铺瓷砖 yzm10原创系列

    yzm10铺瓷砖 一天yzm10接到任务,要求用2×1大小的瓷砖,来铺2×4的地面,地面需要恰好被铺满.这对yzm10来说太容易了,于是他马上设计出了5种不同的铺法(旋转情况算不同种,如图示2.4). ...