.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 服务只验证 ...
随机推荐
- 【题解】Palindrome pairs [Codeforces159D]
[题解]Palindrome pairs [Codeforces159D] 传送门:\(Palindrome\) \(pairs\) \([CF159D]\) [题目描述] 给定一个长度为 \(N\) ...
- Mysql系列(二)—— Mysql支持的数据类型
Mysql版本众多,每个版本支持的数据类型繁多且不一样,本篇文章中主要基于MySQL Community Server 5.7.22介绍常用的数据类型,包括其特点以及区别. 一.数据类型 正确的定义表 ...
- http://blog.csdn.net/baidu_31657889/article/details/52315902
Java技术——你真的了解String类的intern()方法吗 转载 2016年08月25日 16:30:14 标签: java intern / intern / java 技术 6542 0.引 ...
- 华为 S5700 交换机 批量修改端口方法
常常在配置交换机端口的时候需要将多个端口设置为相同的配置,当时各端口逐一去配置不仅慢,而且容易出错,这个时候就需要对端口进行批量设置,不仅快捷,而且避免了反复输出容易出错的情况.不同系列.不同版本交换 ...
- python json库
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写. 1.json库的使用 使用 JSON 函数需要导入 json 库:import jso ...
- 物联网学习笔记三:物联网网关协议比较:MQTT 和 Modbus
物联网学习笔记三:物联网网关协议比较:MQTT 和 Modbus 物联网 (IoT) 不只是新技术,还是与旧技术的集成,其关键在于通信.可用的通信方法各不相同,但是,各种不同的协议在将海量“事物”连接 ...
- 交互式脚本expect场景示例
expect语法示例 #spawn 新建一个进程,这个进程的交互由expect控制 #expect 等待接受进程返回的字符串,直到超时时间,根据规则决定下一步操作 #send 发送字符串给expect ...
- React-router5.x 路由的使用及配置
在 React router 中通常使用的组件有三种: 路由组件(作为根组件): BrowserRouter(history模式) 和 HashRouter(hash模式) 路径匹配组件: Route ...
- CC2530调试过程中遇到的问题们
应用场景描述: 多个发送端在不同的信道上发送信息(11~26)信道,接收端轮询所有信道(11~26),若有信号,则接收,若无信号则继续轮询.形成多个点对点的收发系统. 一.问题1 Ø 问题现象描述: ...
- java8中日期字符串的月日时分秒自动补零
需求:如字符串2019-7-1 9:6:5转成2019-07-01 09:06:05 java8实现如下: public static String getStartDate(String start ...