.png)
图1-3
data-service 可以理解为既要获取学生数据也要获取餐厅食堂数据的服务层, 在这一层里,将数据放入了缓存,进行刷新啊,删除啊,等其它操作
eureka-server 不解释了,这是服务注册中心,你可以理解为一个盆,用来装其它服务的,其它服务理解为毛巾,肥皂,洗发液
third-part-data-service 可以理解为数据库提供层,只是在其static静态资源下放了一些.json文件
util-boot 这个是用来单独放工具类的,公用的工具类,不然都放入各微服务中即可 (可以省略)
图1-3表示了通过maven和Spring Initializr建立出来的微服务分包分层结构
第一步要将几个模块进行pom文件的改造.
首先看之前的我整理的项目的外层pom文件: (父级)
紫色的modelVersion开始指定了该项目标识 (父级的)
往下的parent指定了父级的父级是谁,(是springboot的模块)
再往下浅绿的是指定版本配置文件
再往下蓝色的很清晰,是依赖 (这里有的其它子项目都可以享有)
再往下的蓝色指定springcloud的依赖
最下面橘红色指明了要共享cloud服务的子模块名词 (他们的artifactId)
<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.zq.springcloud</groupId>
<artifactId>springcloudzq</artifactId>
<version>1.0-SNAPSHOT</version>
<name>springcloudzq</name>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.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>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<modules>
<module>eureka-server</module>
<module>data-service</module>
<module>third-part-index-data-proj</module>
<module>util-boot</module>
</modules>
</project>
<modules>
<module>eureka-server</module>
<module>data-service</module>
<module>third-part-index-data-proj</module>
<module>util-boot</module>
<module>the-code-service</module>
<module>the-json-service</module>
</modules>
.png)
图1-4
之后需要将每个子模块的pom文件进行更改,以和父pom文件对应起来.
图1-4表示了加入单独的两个模块后的分层结构,父pom文件即图中可见的pom.xml文件
第三步操作将每个子Modules中的pom文件进行修改,和父pom关联
首先看到父pom文件中最上面的artifactId为 springcloudzq
<artifactId>springcloudzq</artifactId>
那在子Modules中,声明的parent就指明这个artifactId
<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>com.zq.springcloud</groupId>
<artifactId>springcloudzq</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>data-service</artifactId>
红色为xml文件依赖
蓝色是版本信息
绿色即要进行和父项目关联的parent
紫色为子模块的artifactId
之后还有dependencies 和</project>结束标签
现在拿新增的两个模块的pom文件作为演示:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zq.springcloud</groupId>
<artifactId>springcloudzq</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>the-code-service</artifactId>
<dependencies>
<!-- springboot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
</project>
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zq.springcloud</groupId>
<artifactId>springcloudzq</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>the-json-service</artifactId>
<dependencies>
<!-- springboot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
</project>
?为什么两个pom中依赖文件是相同的,因为有些模块例如eureka server,util-boot所依赖的不是web或是client,redis之类.
第四步
将两个新模块 the-code-service, the-json-service 新加业务层
启动测试:
启动eureka-server, third-part, data-service, the-code, the-json
图1-5
图1-5表示启动eureka-server后访问其域名端口查看eureka监控,此时没有服务注册进来.
启动redis,如果不启动,因为在third-part中进行端口的监测,会报错:
Connected to the target VM, address: '127.0.0.1:50599', transport: 'socket'
Disconnected from the target VM, address: '127.0.0.1:50599', transport: 'socket'
检查到端口6379 未启用,判断redis服务器没有启动,本服务无法使用,故退出
检查到端口8760已启用,eureka服务运行中.
Process finished with exit code 1
启动后正常:
Connected to the target VM, address: '127.0.0.1:50691', transport: 'socket'
检查到端口6379已启用,redis服务运行中.检查到端口8760已启用,eureka服务运行中.[限时2秒输入]第三方数据默认端口8090启动[默认端口:8090]
2019-08-28 09:42:30.934 INFO 12692 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$f4e8d998] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.6.RELEASE)
启动成功后,再次查看eureka页面监控:
.png)
图1-6
图1-6表示新启动的服务已经注册到了eureka服务中心.
.png)
图1-7
当data-service注册进eureka后可以调用端口8001的服务请求了.
.png)
图1-8
图1-7表示数据提供层注册进了eureka服务中心.
图1-8表示通过8001端口获取数据.
第五步查看新增的模块获取redis数据是否正常
.png)
图1-9
图1-9表示此时redis中已经加入了code和json的数据
继续启动the-code-service,the-json-service,看数据是否获取正常:
.png)
.png)
图2-1
有bug,莫慌,只要是 resolve 或是 cast 或是 type 出现的都是转型的错误,而这里不是转型错误,而是redis中的数据的包结构和获取时的包结构名称不一致导致.
com.zq.dataservice.bean //获取数据的data-service包结构
com.zq.thecodeservice.bean //the-code-service包结构
com.zq.thejsonservice.bean //the-json-service包结构
改为一致的
com.zq.dataservice.bean
.png)
图2-2
图2-1表示有bug了,此时获取数据出现了错误.
图2-2表示redis中当时存入时通过data-service时的实体类Index所在的包结构.
.png)
图2-3
图2-3表示修复bug后获取数据正常.
工具:diffmerge
提取码:s24m
推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。
这个软件很棒的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来
- 基于Spring Cloud和Netflix OSS构建微服务,Part 2
在上一篇文章中,我们已使用Spring Cloud和Netflix OSS中的核心组件,如Eureka.Ribbon和Zuul,部分实现了操作模型(operations model),允许单独部署的微 ...
- 基于Spring Cloud和Netflix OSS 构建微服务-Part 1
前一篇文章<微服务操作模型>中,我们定义了微服务使用的操作模型.这篇文章中,我们将开始使用Spring Cloud和Netflix OSS实现这一模型,包含核心部分:服务发现(Servic ...
- 今天介绍一下自己的开源项目,一款以spring cloud alibaba为核心的微服务架构项目,为给企业与个人提供一个零开发基础的微服务架构。
LaoCat-Spring-Cloud-Scaffold 一款以spring cloud alibab 为核心的微服务框架,主要目标为了提升自己的相关技术,也为了给企业与个人提供一个零开发基础的微服务 ...
- spring cloud 入门,看一个微服务框架的「五脏六腑」
Spring Cloud 是一个基于 Spring Boot 实现的微服务框架,它包含了实现微服务架构所需的各种组件. 注:Spring Boot 简单理解就是简化 Spring 项目的搭建.配置.组 ...
- SpringCloud(10)使用Spring Cloud OAuth2和JWT保护微服务
采用Spring Security AOuth2 和 JWT 的方式,避免每次请求都需要远程调度 Uaa 服务.采用Spring Security OAuth2 和 JWT 的方式,Uaa 服务只验证 ...
- spring cloud(学习笔记)微服务启动错误(1)
今天下午在启动spring cloud微服务的时候,报了这个错误: Error starting ApplicationContext. To display the auto-configurati ...
- Spring Cloud(Dalston.SR5)--Zuul 网关-微服务集群
通过 url 映射的方式来实现 zuul 的转发有局限性,比如每增加一个服务就需要配置一条内容,另外后端的服务如果是动态来提供,就不能采用这种方案来配置了.实际上在实现微服务架构时,服务名与服务实例地 ...
- Spring Cloud(1):微服务简介
架构的演进: 1.十年前:用户->单一服务器->单一数据库(支持十万级用户) 2.五年前:用户->负载均衡器->多台服务器->缓存集群->主从数据库(支持百万级用户 ...
- 使用Spring Cloud OAuth2和JWT保护微服务
采用Spring Security AOuth2 和 JWT 的方式,避免每次请求都需要远程调度 Uaa 服务.采用Spring Security OAuth2 和 JWT 的方式,Uaa 服务只验证 ...
随机推荐
- 「NOI2018」冒泡排序
「NOI2018」冒泡排序 考虑冒泡排序中一个位置上的数向左移动的步数 \(Lstep\) 为左边比它大的数的个数,向右移动的步数 \(Rstep\) 为右边比它大的数的个数,如果 \(Lstep,R ...
- 整理:WPF中XmlDataProvider的用法总结
原文:整理:WPF中XmlDataProvider的用法总结 一.目的:了解XmlDataProvider中绑定数据的方法 二.绑定方式主要有三种: 1.Xaml资源中内置: <!--XPath ...
- 关于Java链接c#的webapi的注意事项
最近写了一个关于ad域的项目,ad域我也是第一次接触,对ad域的总结我会晚一些时间写出来.在此我先总结一下关于Java调用c#的webapi的一个注意点. [HttpPost] public Dict ...
- ffmpeg命令参数详解
ffmpeg命令参数详解 http://linux.51yip.com/search/ffmpeg ffmpeg图片加滤镜效果 参考:https://cloud.tencent.com/develop ...
- Spring Boot 的自动配置探究、自制一个starter pom
//TODO @Conditional @Condition
- 在.Net Core中使用Swagger制作接口文档
在实际开发过程中后台开发人员与前端(移动端)接口的交流会很频繁.所以需要一个简单的接口文档让双方可以快速定位到问题所在. Swagger可以当接口调试工具也可以作为简单的接口文档使用. 在传统的asp ...
- only size-1 arrays can be converted to Python scalars
python版本:3.6.5 opencv版本:3.2.0 使用的jupyter notebook 源码如下: import cv2 import numpy as np import matplot ...
- VS 对话框控件的Tab顺序问题
我们先来直观的看看各个控件的Tab顺序吧.打开“Resource View”视图,然后在资源中找到对话框IDD_ADDITION_DIALOG,双击ID后中间客户区域出现其模板视图.在主菜单中选择“F ...
- Java Web项目搭建过程记录(struts2)
开发工具:eclipse 搭建环境:jdk1.7 tomcat 8.0 基础的java开发环境搭建过程不再赘述,下面从打开eclipse 之后的操作开始 第一步: 创建项目,File -> ...
- Java JDBC 数据源
数据源有2种: 普通数据源 即数据库驱动自带的数据源 连接池 包括数据库驱动自带的连接池,以及DBCP.C3P0等常用的第三方连接池. 数据库驱动自带的数据源 //从propertie ...