作者:故事我忘了
个人微信公众号:程序猿的月光宝盒

[toc]
# 1.新建普通maven工程

2.在父级pom中按需修改

3.删除父级src目录

4.创建公共模块common,里面只有service接口和实体类


![图片](http://blog-cc.nos-eastchina1.126.net/9b0bebc4-687e-4767-9e55-1d86192e973b)

5.构建微服务模块,provider


![图片](http://blog-cc.nos-eastchina1.126.net/0a2b72d3-2b7b-453f-9ee2-2199f39e6fdc)


![图片](http://blog-cc.nos-eastchina1.126.net/8d0be12b-7d82-4918-b412-ec4736689550)

6.引用Zookeeper和Dubbo的依赖

在这个provider中修改pom文件坐标

  <dependencies>
<!--引入公共模块项目-->
<dependency>
<groupId>cn.kgc</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--这是微服dubbo的核心服务包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<!--这是zookeeper的服务操作包 但主要这里的版本最好和你的zookeeper版本一致-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!--这里是zookeeper的zkCli的操作包 我们读写服务全靠这个包-->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

7.在provider中写mapper和service的实现

8.在启动类上添加mapper扫描注解

9.在resources下建这两个文件,用于注册

9.1application.properties

修改需要的url参数和分页插件参数

server.port=9090

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/kgc
spring.datasource.username=root
spring.datasource.password=ok mybatis.type-aliases-package=cn.kgc.vo mybatis.mapper-locations=mapper/*.xml pagehelper.helper-dialect=mysql

9.2spring-provider.xml

根据需要修改用户服务接口中的参数,作用是将service的接口注入到Zookeeper中去

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="myprovider" /> <!-- 使用zookeeper注册中心暴露服务地址,我的zookeeper是架在本地的 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" timeout="60000"/> <!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" /> <!-- 用户服务接口 -->
<dubbo:service interface="cn.kgc.service.TableToOneService" ref="tableToOneService"/>
<bean id="tableToOneService" class="cn.kgc.service.TableToOneServiceImpl"/> <dubbo:service interface="cn.kgc.service.TableToManyService" ref="tableToManyService"/>
<bean id="tableToManyService" class="cn.kgc.service.TableToManyServiceImpl"/>
</beans>

10再修改启动项,添加导入资源注解

11再创建微服务模块,consumer


![图片](http://blog-cc.nos-eastchina1.126.net/c7b3212e-8c9f-4d63-ade7-91fe84df0a8e)


![图片](http://blog-cc.nos-eastchina1.126.net/8d0be12b-7d82-4918-b412-ec4736689550)

12.把provider中pom的坐标拷贝到consumer

13.把provider中的application文件拷贝到consumer,并按需修改(端口号等)

14.编写controller类和添加spring-consumer.xml

14.1spring-consumer.xml

按需修改service名

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="dubbo-consumer"/>
<dubbo:registry check="false" address="zookeeper://127.0.0.1:2181"/>
<dubbo:reference interface="cn.kgc.service.TableToOneService" id="tableToOneService"/>
<dubbo:reference interface="cn.kgc.service.TableToManyService" id="tableToManyService"/>
</beans>

15再启动类上添加导入资源注解



全部代码写完

然后开启Zookeeper服务器,然后开启provider启动类,再开启consumer启动类,最后用postman检测接口是否通畅,根据错误信息修改代码..

整体流程

结束

SpringBoot+idea搭建微服务简化流程的更多相关文章

  1. Spring-boot:快速搭建微服务框架

    前言: Spring Boot是为了简化Spring应用的创建.运行.调试.部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过多关注XML的配置. 简单来说,它提供了一堆依赖打包,并 ...

  2. 十分钟搭建微服务框架(SpringBoot +Dubbo+Docker+Jenkins源码)

    本文将以原理+实战的方式,首先对“微服务”相关的概念进行知识点扫盲,然后开始手把手教你搭建这一整套的微服务系统. 这套微服务框架能干啥? 这套系统搭建完之后,那可就厉害了: 微服务架构 你的整个应用程 ...

  3. 【译文】用Spring Cloud和Docker搭建微服务平台

    by Kenny Bastani Sunday, July 12, 2015 转自:http://www.kennybastani.com/2015/07/spring-cloud-docker-mi ...

  4. Spring Cloud和Docker搭建微服务平台

    用Spring Cloud和Docker搭建微服务平台 This blog series will introduce you to some of the foundational concepts ...

  5. spring cloud+dotnet core搭建微服务架构:Api授权认证(六)

    前言 这篇文章拖太久了,因为最近实在太忙了,加上这篇文章也非常长,所以花了不少时间,给大家说句抱歉.好,进入正题.目前的项目基本都是前后端分离了,前端分Web,Ios,Android...,后端也基本 ...

  6. spring cloud+.net core搭建微服务架构:Api授权认证(六)

    前言 这篇文章拖太久了,因为最近实在太忙了,加上这篇文章也非常长,所以花了不少时间,给大家说句抱歉.好,进入正题.目前的项目基本都是前后端分离了,前端分Web,Ios,Android...,后端也基本 ...

  7. 【微服务】使用spring cloud搭建微服务框架,整理学习资料

    写在前面 使用spring cloud搭建微服务框架,是我最近最主要的工作之一,一开始我使用bubbo加zookeeper制作了一个基于dubbo的微服务框架,然后被架构师否了,架构师曰:此物过时.随 ...

  8. 手把手教你使用spring cloud+dotnet core搭建微服务架构:服务治理(-)

    背景 公司去年开始使用dotnet core开发项目.公司的总体架构采用的是微服务,那时候由于对微服务的理解并不是太深,加上各种组件的不成熟,只是把项目的各个功能通过业务层面拆分,然后通过nginx代 ...

  9. spring cloud+dotnet core搭建微服务架构:服务发现(二)

    前言 上篇文章实际上只讲了服务治理中的服务注册,服务与服务之间如何调用呢?传统的方式,服务A调用服务B,那么服务A访问的是服务B的负载均衡地址,通过负载均衡来指向到服务B的真实地址,上篇文章已经说了这 ...

随机推荐

  1. spring boot 一个项目启动多个实例

    0.前言 在开发中,我们经常需要以不同端口启动同一个项目的多个实例,IDEA中启动多个实例很简单 1.方法 1.1.在项目中,选择编辑配置,然后点选允许并行运行,如下图: 1.2.调出RunDashb ...

  2. 【华为云网络技术分享】HTTP重定向HTTPS配置指南

    [摘要] 本文介绍使用华为云弹性负载均衡配置Http重定向到Https的方法. 1. HTTP.HTTPS 头部标识 ELB 对 HTTPS 进行代理,无论是 HTTP 还是 HTTPS 请求,到了  ...

  3. Swoole高效跟传统的web开发有什么区别?

    一.swoole的运行模式 Swoole高效跟传统的web开发有什么区别,除了传统的LAMP/LNMP同步开发模式,swoole的异步开发模式是怎么样的. 1.1.传统web开发模式 PHP web开 ...

  4. HDU4109-instruction agreement(差分约束-最长路+建立源点,汇点)

    Ali has taken the Computer Organization and Architecture course this term. He learned that there may ...

  5. Mybatis代码生成器Mybatis-Generator使用详解

    前提 最近在做创业项目的时候因为有比较多的新需求,需要频繁基于DDL生成Mybatis适合的实体.Mapper接口和映射文件.其中,代码生成器是MyBatis Generator(MBG),用到了My ...

  6. USB摄像头之130w像素 OV9655配置,ov9650,ov7725,ov7670

    USB摄像头之130w像素 OV9655配置 为了usb2.0采集达到足够的速率,不得不将采用raw格式输出. // 20150411 XVGA 1280*1024 实际上位机需要2560*1024 ...

  7. wxxcx_learn订单

    自动写入时间戳 protected $autoWriteTimestamp = true: 事务的使用 Db::startTrans();....... Db::commit();.. Db::rol ...

  8. irules

    BIG-IP系统iRules基本概念_v11.6.1 2017年10月10日 00:35:16 ifelif 阅读数:1097   1 iRules介绍 什么是iRule iRule是BIG-IP本地 ...

  9. 【Ubuntu 16.04.2_64】安装配置SVN

    [Ubuntu 16.04.2_64]安装配置SVN 转载:https://www.cnblogs.com/yangchongxing/p/10190549.html 检查是否已安装svn # svn ...

  10. 面试完腾讯,总结了这12道Zookeeper面试题!

    前言 ZooKeeper 是一个开源的分布式协调服务,可以基于 ZooKeeper 实现诸如:数据发布/订阅.负载均衡.命名服务.分布式协调/通知.集群管理.Master 选举.配置维护,名字服务.分 ...