创建网站基本流程,网站调用微信js视频,动漫设计是干什么的,wordpress修改源码Spring Cloud 文章目录 Spring Cloud任务三#xff1a;Spring Cloud与微服务架构1.Spring Cloud课程内容介绍2.单体应用架构2.1 互联网应用架构演进2.2 单体应用架构 3.垂直应用架构4.SOA应用架构5.微服务应用架构介绍6.微服务架构核心思想及优缺点7.微服务架构的核心概念8.Sp…Spring Cloud 文章目录 Spring Cloud任务三Spring Cloud与微服务架构1.Spring Cloud课程内容介绍2.单体应用架构2.1 互联网应用架构演进2.2 单体应用架构 3.垂直应用架构4.SOA应用架构5.微服务应用架构介绍6.微服务架构核心思想及优缺点7.微服务架构的核心概念8.Spring Cloud 是什么9.Spring Cloud解决什么问题10.Spring Cloud核心组件11.Spring Cloud体系结构12.Spring Cloud对比Dubbo13.自测 任务四 Spring Cloud核心组件及实战1.案例需求及流程分析2.商品表与工程架构说明3.项目父工程创建4.公共组件微服务搭建5.商品微服务搭建6.页面静态化微服务搭建7.案例代码问题分析8.第一代Spring Cloud核心组件9.注册中心实现原理10.主流的服务注册中心11.Eureka 基础架构及交互原理12.搭建Eureka Server13.将微服务注册到Eureka14.搭建Eureka Server高可用集群15.改造服务消费者调用服务提供者 任务三Spring Cloud与微服务架构
1.Spring Cloud课程内容介绍
第⼀部分微服务架构互联网应用架构演进微服务架构的体现思想及优缺点微服务架构的核心概念
第⼆部分 SpringCloud概述Sping Cloud 是什么Sping Cloud 解决什么问题Sping Cloud 架构
第三部分案例准备
第四部分第⼀代 Spring Cloud 核⼼组件 Spring Cloud NetflixEureka服务注册中心Ribbon负载均衡Hystrix熔断器Feign远程调用组件GateWay网关组件Config 分布式配置中心
第五部分第⼆代 Spring Cloud 核⼼组件Spring Cloud AlibabaNacos 服务注册和配置中心 Sentinel 分布式系统的流量防卫兵2.单体应用架构
2.1 互联网应用架构演进
随着互联网的发展用户群体逐渐扩大网站的流量成倍增长常规的单体架构已无法满足请求压力和业务的快速迭代架构的变化势在必行。下面我们以拉勾网的架构演进为例从最开始的单体架构分析一步步的到现在的微服务架构。
淘宝LAMP LinuxApacheMySQLPHP
2.2 单体应用架构
在诞生之初拉钩的用户量、数据规模都比较小项目所有的功能模块都放在了一个工程中编码编译打包并且部署在一个tomcat容器中的架构模式就是单体应用架构这样的架构模式即简单实用便于维护成本有低成为那个时代的主流架构方式。 3.垂直应用架构
为了避免上面提到的那些问题开始做模块的垂直划分做垂直划分的原则是基于拉钩现有的特性来做核心目标第一个为了业务之间相互不影响第二个是在研发团队的壮大后为了提高效率减少组件之间的依赖。 4.SOA应用架构
Service-Oriented Architecture服务-面向 体系结构 In the service-oriented architecture ( SOA) of Web services, there are three distinct actors: the Provider, the Requestor, and the Broker. 在Web服务的面向服务体系SOA中有三个截然不同的角色提供者、请求者和中介者 在做了垂直划分之后模块随之增多维护的成本也在变高一些通用的业务和模块重复的越来越多为了解决上面提到的接口协议不统一服务无法监控服务的负载均衡引入了阿里巴巴开源的Dubbo一款高性能、轻量级的开源java RPC框架可以和Spring框架无缝集成。它提供了三大核心能力面向接口的远程方法调用智能容错和负载均衡以及服务自动注册和发现。
SOA(Service-Oriented Architecture)即面向服务的架构根据实际业务把系统分成合适的独立部署的模块模块之间相互独立(通过Webservice/Dubbo等技术进行通信)
优点分布式、松耦合、扩展灵活、可重用
缺点服务抽取粒度大、服务调用方和提供方耦合度较高(接口耦合度 ) 5.微服务应用架构介绍 6.微服务架构核心思想及优缺点
微服务架构设计的核心思想就是微拆分的粒度较小这样的话单一职责、开发的耦合度就会降低、微小的功能可以独立部署扩展、灵活性强升级改造影响范围小
微服务架构的优点微服务架构和微服务 微服务很小便于特定业务功能的聚焦 微服务很小每个微服务都可以被一个小团队单独实施开发、测试、部署上线、运维团队合作一定程度解耦便于实施敏捷开发 微服务很小便于重用和模块之间的组装 微服务很独立那么不同的微服务可以使用不同的语言开发松耦合可以用python进行爬虫的开发用C#进行底层的开发 微服务架构下我们更容易引入新技术(微服务之间独立影响小使用新技术的影响也小)
微服务架构的缺点 微服务架构下分布式复杂难以管理当服务数量增加管理将越来越复杂 微服务架构下分布式链路跟踪难等
7.微服务架构的核心概念 服务注册与服务发现 例如职位搜索 ———— 简历服务
服务提供者简历服务 做了集群有3 台服务器这个时候服务消费者-职位搜索调用服务提供者-简历服务要调用哪一个呢我是采用轮询呢还是随机
服务消费者职位搜索
服务注册服务提供者将所有提供服务的信息(服务器IP和端口、服务访问协议等) 注册/登记到注册中心
服务发现服务消费者能够从注册中心获取到较为实时的服务列表然后根据一个策略选择一个服务访问
细节服务的消费者也要把自己注册到服务注册中心 注册完成之后如果你要调用别的服务这个时候可以从服务注册中心去拉取服务提供者的列表然后根据需求进行调用 负载均衡
负载均衡即将请求压力分配到多个服务器(应用服务器数据库服务器)以此来提高服务的性能、可靠性。多台服务器部署的是同一份代码这才叫集群嘛 熔断
熔断即断路保护。微服务架构中如果下游服务因访问压力过大而响应变慢或失败上游服务为了保护系统的可用性可以暂时切断对下游服务的调用。这种牺牲局部保 全整体的措施就叫熔断 服务B调用服务C这个时候服务C宕机了C已经死了自己再调也会跟着死服务B可以进行熔断不去调用服务C然后进行服务降级返回服务C的默认数据给到服务A
链路追踪
微服务架构越发流行一个项目往往拆分成很多个服务那么一次请求就需要设计很多个服务。不同的微服务可能是由不同的团队开发、可能使用不同的语言开发整个项目也可能部 署在了很多个服务器上甚至百台、千台横跨多个不同的数据中心。所谓链路追踪就是对一次请求设计的很多个服务链路进行日志记录、性能监控。其实就是分析你的轨迹然后进 行日志记录通过分析日志我们就会得出很多信息
那么这个日志记录有用吗?
非常有用你这个请求经过了谁延迟有多久我们就可以大致的推算出来我服务的瓶颈是在哪个微服务上哪个微服务需要进行优化比如说你这个请求中断了没有成功调 用那我们通过日志是不是就能够得出你死在了哪个位置。比如说你死在了微服务B那为什么死了所以说我们就可以做重点的监控OK这个就非常 nice 了。 API网关 IOT所有的物联网设备只要能够联网的设备豆角IOT 客户端Mobile手机 客户端Browser浏览器 客户端 客户端访问某些数据可以访问我们的微服务比如看一下拉钩最近发布了哪些活动我们每一个微服务都需要单独的调用而且你这么多微服务所对应的地址我是不是都要记住太难了(臣妾做不到 QAQ)所以得话我们需要统一的入口我不找你们我就找它什么叫统一的入口呢比如说马上十一了你要去故宫去玩你不能直接翻墙过去吧你应该从大门凭票进入有票请进没票滚蛋-鉴权有票是吧那行你要去太和殿那我给你指路你要去后宫那行在那边我给你指路。什么你偷过东西你也敢来赶紧滚蛋黑白名单。这就是我们网关的作用就等同于给了你一个统一的入口。 微服务架构下不同的微服务往往会有不同的访问地址客户端可能需要调用多个服务的接口才能完成一个业务需求如果让客户端直接与各个微服务通信可能出现 (1)客户端需要调用不同的URL地址增加了维护调用的难度 (url 得写死万一某个URL宕掉了维护起来比较麻烦) (2)在一定的场景下也存在跨域请求的问题前后端分离就会碰到跨域问题原本我们在后端采用Cors就能解决现在利用网关那么就放在网关这层做就好了 (3)每一个微服务都需要进行单独的身份认证 每调一个微服务都认证一次太麻烦了用户体验也太不友好了 | 服务A —— (认证一次)服务B——(认证一次)服务C
那么API网关就可以较好的统计处理上述问题API请求调用统一接入API网关层由网关转发请求。API网关更专注在安全、路由、流量等问题的处理上微服务团队专注于处理业务 逻辑即可它的功能比如
1统一接入路由
2安全防护统一鉴权负责网关访问身份认证验证与 访问认证中心通信实际认证业务逻辑交移访问认证中心处理
3黑白名单实现通过ID地址控制禁止访问网关功能控制访问
4协议适配实现通信协议校验、适配转换的功能
5流量管控限流-平滑这个请求削峰
6长短链接支持
7容错能力负载均衡
8.Spring Cloud 是什么
Spring Cloud是一系列框架的有序集合(Spring Cloud是一个规范)
Spring Cloud其实是一套规范是一套用于构建微服务架构的规范而不是一个拿来即用的框架所谓规范就是应该有哪些功能组件然后组件之间怎么配合共同完成什么事情。在这个规范之下第三方的Netflix公司开发了一些组件、Spring官方开发了一些框架/组件包括第三方的阿里巴巴开发了一套框架/组件集合 Spring Cloud Alibaba这些才是Spring Cloud规范的实现。 Netflix搞了一套简称SCN Spring Cloud 吸收了Netflix公司的产品基础之上自己也搞了几个组件 阿里巴巴在之前的基础上搞出了一堆微服务组件Spring Cloud AlibabaSCA
9.Spring Cloud解决什么问题
Spring Cloud 规范及实现意图要解决的问题其实就是微服务架构实施过程中存在的一些问题比如
微服务架构中的服务注册发现问题、网络问题比如熔断场景、统一认证安全授权问题、负载均衡问
题、链路追踪等问题。
Distributed/versioned configuration 分布式/版本化配置Service registration and discovery 服务注册和发现Routing 智能路由Service-to-service calls 服务调用Load balancing 负载均衡Circuit Breakers 熔断器Global locks 全局锁Leadership election and cluster state 选举与集群状态管理Distributed messaging 分布式消息传递平台
10.Spring Cloud核心组件 11.Spring Cloud体系结构 12.Spring Cloud对比Dubbo dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架基于RPC调用底层用的协议是TCP协议但是也可以选择HTTP默认是TCP对于目前使用率较高的Spring Cloud Netflix来说他是基于HTTP的Spring Cloud Feign基于HTTP协议进行调用所以效率上没有Dubbo高但问题在于Dubbo体系的组件不全不能够提供一站式解决方案比如服务注册与发现需要借助与Zookeeper等实现而Spring Cloud Netflix 则是真正的提供了一站式服务化解决方案且有Spring大家族背景。
前些年Dubbo使用率高于SpringCloud但目前SpringCloud在服务化/微服务解决方案中已经有了非常好的发展趋势。
OSI模型主要协议单位TCP/IP应用层telnet、ftp、HTTP、snmp等数据流应用层表示层css、gif、html、json、xml、数据流应用层会话层ftp、ssh、tls、http(s)、sql数据流应用层传输层tcp、udp传输层传输层网络层IP(IPV4、IPV6) ICMP数据包网际层数据链路层802.2,802.3ATM、HDLC帧网路接口层物理层v.35、EIA/TIA-232比特流网路接口层
13.自测
阶段九模块一任务三 Spring Cloud与微服务架构
共3道答对1题
35
总分100分 1. 多选题以下哪些选项是单体架构的不足之处多选 [多选题] *(35分) A可靠性差B复杂性高C扩展能力受限D服务监控不到位 D 选项 回答错误 正确答案为 ABC 答案解析 解析服务监控不到位是垂直应用架构及部分设计不合理的分布式架构存在的问题 2. 单选题以下关于微服务架构的选项描述错误的是[单选题] *(30分) A微服务架构可以说是SOA架构的一种拓展这种架构模式下它拆分粒度更小、服务更独立。B微服务架构强调的⼀个重点是业务需要彻底的组件化和服务化C各个微服务很独立那么不同的微服务可以使用不同的语言开发松耦合D微服务架构下各个微服务数据交互的效率要远远高于单体应用架构 B 选项 回答错误 正确答案为 D 答案解析 解析微服务下各个微服务通讯需要借助于http、tcp等通讯协议进行网络通讯涉及的网络延迟、处理时间相比于单体架构的直接new效率要低很多。 3. 单选题以下哪个选项是针对微服务架构概念中网关的描述[单选题] *(35分) AA网关负责对API请求调用统一接入由该统一服务转发请求。BB网关负责对一次请求涉及的很多个服务链路进行日志记录、性能监控CC微服务架构中如果下游服务因访问压力过大而响应变慢或失败上游服务为了保护系统整体可用性可以暂时切断对下游服务的调用。这种牺牲局部保全整体的措施就叫做网关。DD网关负责即将请求压力分配到多个服务器应用服务器、数据库服务器等以此来提高服务的性能、可靠性 回答正确 35分 答案解析 解析B描述的链路追踪C描述的熔断D描述的是负载均衡
任务四 Spring Cloud核心组件及实战
1.案例需求及流程分析 2.商品表与工程架构说明
创建商品信息表
CREATE TABLE products( id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(50), #商品名称 price DOUBLE, flag VARCHAR(2), #上架状态 goods_desc VARCHAR(100), #商品描述 images VARCHAR(400), #商品图片 goods_stock INT, #商品库存 goods_type VARCHAR(20) #商品类型
);案例工程的准备 3.项目父工程创建
父工程 pom 文件配置
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.aifeng/groupIdartifactIdaifeng-parent/artifactIdversion1.0-SNAPSHOT/version!--父工程打包方式--packagingpom/packaging!--spring boot 父启动器依赖-- parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version2.1.6.RELEASE/version !--2.4 以上版本没有 RELEASE--/parent dependencies !--web依赖-- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency !--日志依赖-- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-logging/artifactId /dependency !--测试依赖-- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-test/artifactId scopetest/scope /dependency !--lombok工具-- dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId version1.18.4/version scopeprovided/scope /dependency !-- Actuator可以帮助你监控和管理Spring Boot应用-- dependencygroupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-actuator/artifactId/dependency !--热部署-- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-devtools/artifactId optionaltrue/optional /dependency /dependencies build plugins !--编译插件-- plugin groupIdorg.apache.maven.plugins/groupId artifactIdmaven-compiler-plugin/artifactId configuration source21/source target21/target encodingutf-8/encoding /configuration /plugin !--打包插件-- plugin groupIdorg.springframework.boot/groupId artifactIdspring-boot-maven-plugin/artifactIdexecutions execution goals goalrepackage/goal /goals /execution /executions /plugin /plugins /build
/project4.公共组件微服务搭建 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdcom.aifeng/groupIdartifactIdaifeng-parent/artifactIdversion1.0-SNAPSHOT/version/parentartifactIdaifeng-service-common/artifactIdpropertiesmaven.compiler.source11/maven.compiler.sourcemaven.compiler.target11/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependencies!--引入mybatis-plus--dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.3.2/version/dependency!--pojo持久化使用--!-- https://mvnrepository.com/artifact/javax.el/javax.el-api --!--dependencygroupIdjavax.el/groupIdartifactIdjavax.el-api/artifactIdversion2.2.2/version/dependency--!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-jpa/artifactIdversion3.2.1/version/dependencydependencygroupIdjavax.persistence/groupIdartifactIdjavax.persistence-api/artifactIdversion2.2/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.13/versionscoperuntime/scope!--scoperuntime/scope--/dependency/dependencies
/project编译报错: ‘dependencies.dependency.version’ for mysql:mysql-connector-java:jar is missing. com.aifeng:aifeng-service-common:[unknown-version]
[INFO] Scanning for projects...
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[ERROR] dependencies.dependency.version for mysql:mysql-connector-java:jar is missing. com.aifeng:aifeng-service-common:[unknown-version], D:\workspace\springclouddemo2\aifeng-parent\aifeng-service-common\pom.xml, line 32, column 21
[ERROR] The build could not read 1 project - [Help 1]
[ERROR]
[ERROR] The project com.aifeng:aifeng-service-common:1.0-SNAPSHOT (D:\workspace\springclouddemo2\aifeng-parent\aifeng-service-common\pom.xml) has 1 error
[ERROR] dependencies.dependency.version for mysql:mysql-connector-java:jar is missing. com.aifeng:aifeng-service-common:[unknown-version], D:\workspace\springclouddemo2\aifeng-parent\aifeng-service-common\pom.xml, line 32, column 21
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingExceptionProcess finished with exit code 1
解决方法加上版本号 8.0.13 dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.13/versionscoperuntime/scope!--scoperuntime/scope--/dependencydependencygroupIdjavax.el/groupIdartifactIdjavax.el-api/artifactIdversion2.2.4/version
/dependency生成数据库实体类 使用lombok注解去掉生成的getter 和 setter 方法 报错JPS incremental annotation processing is disabled
Executing pre-compile tasks...
Cleaning output directories…
Running before tasks
Checking sources
Copying resources... [aifeng-service-product]
Copying resources... [aifeng-service-common]
Parsing java… [aifeng-service-common]
java: JPS incremental annotation processing is disabled. Compilation results on partial recompilation may be inaccurate. Use build process jps.track.ap.dependencies VM flag to enable/disable incremental annotation processing environment.
java: 由于在类路径中发现了一个或多个处理程序因此启用了批注处理。未来发行版的 javac 可能会禁用批注处理除非至少按名称指定了一个处理程序 (-processor)或指定了搜索路径 (--processor-path, --processor-module-path)或显式启用了批注处理 (-proc:only, -proc:full)。可使用 -Xlint:-options 隐藏此消息。可使用 -proc:none 禁用批注处理。
java: java.lang.NoSuchFieldError: Class com.sun.tools.javac.tree.JCTree$JCImport does not have member field com.sun.tools.javac.tree.JCTree qualid
java: java.lang.NoSuchFieldError: Class com.sun.tools.javac.tree.JCTree$JCImport does not have member field com.sun.tools.javac.tree.JCTree qualid
Checking dependencies… [aifeng-service-common]
Dependency analysis found 0 affected files
Errors occurred while compiling module aifeng-service-common
javac 21.0.1 was used to compile java sources
Finished, saving caches…
Executing post-compile tasks...
Finished, saving caches…
Synchronizing output directories...
2024/1/16 22:12 - Build completed with 2 errors and 1 warning in 2 sec, 697 msjava: JPS incremental annotation processing is disabled. Compilation results on partial recompilation may be inaccurate. Use build process jps.track.ap.dependencies VM flag to enable/disable incremental annotation processing environment.java: java.lang.NoSuchFieldError: Class com.sun.tools.javac.tree.JCTree$JCImport does not have member field com.sun.tools.javac.tree.JCTree qualidjava: java.lang.NoSuchFieldError: Class com.sun.tools.javac.tree.JCTree$JCImport does not have member field com.sun.tools.javac.tree.JCTree qualid
解决方法提升lombok 版本为1.18.30 报错: Failed to read candidate component class 有提示信息Failure to find org.springframework.boot:spring-boot-starter-parent:pom:3.2.1.RELEASE in http://mave Failure to find org.springframework.boot:spring-boot-starter-parent:pom:3.2.1.RELEASE in http://mave2024-01-16 22:20:54.607 ERROR 3772 --- [ restartedMain] o.s.boot.SpringApplication : Application run failedorg.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [D:\workspace\springclouddemo\aifeng-parent\aifeng-service-product\target\classes\com\aifeng\product\ProductApplication.class]; nested exception is org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isnt supported yet: file [D:\workspace\springclouddemo\aifeng-parent\aifeng-service-product\target\classes\com\aifeng\product\ProductApplication.class]; nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 65at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:454) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:316) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:275) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:132) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:287) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:242) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:199) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:315) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]at com.aifeng.product.ProductApplication.main(ProductApplication.java:13) ~[classes/:na]at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na]at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na]at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.1.6.RELEASE.jar:2.1.6.RELEASE]
Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isnt supported yet: file [D:\workspace\springclouddemo\aifeng-parent\aifeng-service-product\target\classes\com\aifeng\product\ProductApplication.class]; nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 65at org.springframework.core.type.classreading.SimpleMetadataReader.init(SimpleMetadataReader.java:57) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:103) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:123) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:430) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]... 23 common frames omitted
Caused by: java.lang.IllegalArgumentException: Unsupported class file major version 65at org.springframework.asm.ClassReader.init(ClassReader.java:184) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.asm.ClassReader.init(ClassReader.java:166) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.asm.ClassReader.init(ClassReader.java:152) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.asm.ClassReader.init(ClassReader.java:273) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.core.type.classreading.SimpleMetadataReader.init(SimpleMetadataReader.java:54) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE]... 26 common frames omittedProcess finished with exit code 0
**解决方法**去掉release 而且maven仓库官网也没有在版本之后 加release 仔细看了下 maven仓库的官网发现从 2.4 版本之后就没有加 release 了。 与其他springboot的pom文件进行了对比也是没有release的 repackage报红解决方法 plugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdversion3.2.1/versionexecutionsexecutiongoalsgoalrepackage/goal !--repackage 本地mvn仓库没有这个依赖, 查看使用的默认的, 然后改为自建的本地仓库(D:repository | D:\software\apache-maven- 3.3.3\conf\settings.xml)--/goals/execution/executions/plugin5.商品微服务搭建
商品微服务引入公共组件坐标 dependencygroupIdcom.aifeng/groupIdartifactIdaifeng-service-common/artifactIdversion1.0-SNAPSHOT/versionscopecompile/scope/dependency在yml文件中配置端口、应用名、数据库连接等信息
server:port: 9000 #微服务的集群环境中通常会为每一个微服务叠加
spring:application:name: aifeng-service-productdatasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://192.168.11.128:3306/springclouddata?useUnicodetruecharacterEncodingutf8serverTimezoneUTCusername: rootpassword: 123456Mapper接口开发
package com.aifeng.product.mapper;import com.aifeng.common.pojo.Products;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;/**** 现在使用的Mybatis-plus组件该组件是Mybatis的加强版* 能够与SpringBoot进行非常友好的整合对比Mybatis框架只有使用便捷的改变* 没有具体功能的改变* 具体使用让具体的Mapper接口继承BaseMapper即可* */
public interface ProductMapper extends BaseMapperProducts {
} serive层开发 ProductService 接口 package com.aifeng.product.service;import com.aifeng.common.pojo.Products;public interface ProductService {Products queryById(Integer id);
} ProductServiceImpl 实现类 package com.aifeng.product.service.imp;import com.aifeng.common.pojo.Products;
import com.aifeng.product.mapper.ProductMapper;
import com.aifeng.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;Service
public class ProductServiceImpl implements ProductService {Autowiredprivate ProductMapper productMapper;Overridepublic Products queryById(Integer id) {return productMapper.selectById(id);}
}controller层开发 ProductController 类 package com.aifeng.product.controller;import com.aifeng.common.pojo.Products;
import com.aifeng.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
RequestMapping(/product)
public class ProductController {Autowiredprivate ProductService productService;GetMapping(/query/{id})public Products queryById(PathVariable Integer id){return productService.queryById(id);}
}启动类 package com.aifeng.product;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
MapperScan(com.aifeng.product.mapper)// 扫描mapper
public class ProductApplication {public static void main(String[] args) {SpringApplication.run(ProductApplication.class,args);}
}启动项目的时候一直报红table注解不可用依赖坐标没问题但是就是包没有导入进来最后把版本重新降回 2.1.6.release依然还是爆红已经试了很多方法也很晚了就睡了想着下次把jdk版本换成11 再试一下卡难看过了大概有3天吧这个周末再重新打开项目table注解已经不爆红了然后查看扩展库里已经依赖包导入。嗯想着应该是依赖的包没有导入进来可能是因为缓存和网络的问题当时依赖的包还没有下载完成当时IDEA又没有提示导致我以为是依赖包下载完成嗯后续深入分析一下… controller层代码写好之后 启动项目还是包 版本错误果断 jdk版本降为 11 啊 jdk 21 用不来啊然后启动项目正常了。
postman测试报500错误查看mapper扫描的路径没有配置正确(少了个mapper)
SpringBootApplication
MapperScan(com.aifeng.product.mapper)// 扫描mapper
public class ProductApplication {public static void main(String[] args) {SpringApplication.run(ProductApplication.class,args);}
}项目重新启动又报错啊查看是数据库配置错误mysql下 没有 products 表表是建在 springclouddata 下的
2024-01-21 20:40:18.141 ERROR 12712 --- [nio-9000-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. Cause: java.sql.SQLSyntaxErrorException: Table mysql.products doesnt exist
### The error may exist in com/aifeng/product/mapper/ProductMapper.java (best guess)
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT id,name,price,flag,goods_desc,images,goods_stock,goods_type FROM products WHERE id?
### Cause: java.sql.SQLSyntaxErrorException: Table mysql.products doesnt exist
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Table mysql.products doesnt exist] with root cause果断切换数据库为 springclouddata
postman测试http://127.0.0.1:9000/product/query/1 {id: 1,name: null,price: 100.0,flag: Y,goodsDesc: 2亿像素让你拍照更清晰,images: url,goodsStock: 1000,goodsType: 手机
}6.页面静态化微服务搭建
报错: java.net.ConnectException: Connection refused: connect
原因微服务中我依赖的那个服务没有启动QAQ !!! 从昨晚开始到现在我也是对自己无语了最怕开始遇到坑结果还是坑了自己
2024-01-22 20:47:48.705 ERROR 25196 --- [nio-9100-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on GET request for http://127.0.0.1:9000/product/query/1: Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect] with root causejava.net.ConnectException: Connection refused: connectat java.base/java.net.PlainSocketImpl.connect0(Native Method) ~[na:na]at java.base/java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:101) ~[na:na]at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:412) ~[na:na]at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:255) ~[na:na]at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:237) ~[na:na]at java.base/java.net.Socket.connect(Socket.java:608) ~[na:na]at java.base/java.net.Socket.connect(Socket.java:557) ~[na:na]at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:182) ~[na:na]at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:508) ~[na:na]at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:603) ~[na:na]at java.base/sun.net.www.http.HttpClient.init(HttpClient.java:276) ~[na:na]at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:375) ~[na:na]at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:396) ~[na:na]at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1253) ~[na:na]at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1187) ~[na:na]at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1081) ~[na:na]at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1015) ~[na:na]at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:735) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:311) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]at com.aifeng.page.controller.PageController.getProduct(PageController.java:23) ~[classes/:na]at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.21.jar:9.0.21]at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:88) ~[spring-boot-actuator-2.1.6.RELEASE.jar:2.1.6.RELEASE]at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:114) ~[spring-boot-actuator-2.1.6.RELEASE.jar:2.1.6.RELEASE]at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:104) ~[spring-boot-actuator-2.1.6.RELEASE.jar:2.1.6.RELEASE]at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.21.jar:9.0.21]at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]postman 测试调用成功返回 7.案例代码问题分析
我们在页面静态化微服务中使用RestTemplate调用商品微服务的商品状态接口时Restful API 接
口。在微服务分布式集群环境下会存在什么问题呢怎么解决
存在的问题
1在服务消费者中我们把url地址硬编码到代码中不方便后期维护。
2服务提供者只有一个服务即便服务提供者形成集群服务消费者还需要自己实现负载均衡。
3在服务消费者中不清楚服务提供者的状态。
4服务消费者调用服务提供者时候如果出现故障能否及时发现不向用户抛出异常页面
5RestTemplate这种请求调用方式是否还有优化空间能不能类似于Dubbo那样玩
6这么多的微服务统一认证如何实现
7配置文件每次都修改好多个很麻烦
8…
上述分析出的问题其实就是微服务架构中必然面临的一些问题
1服务管理自动注册与发现、状态监管
2服务负载均衡
3熔断
4远程过程调用
5网关拦截、路由转发
6统一认证
7集中式配置管理配置信息实时自动更新
这些问题Spring Cloud 体系都有解决方案后续我们会逐个学习。
8.第一代Spring Cloud核心组件 9.注册中心实现原理 10.主流的服务注册中心 Zookeeper Dubbo Zookeeper Zookeeper它是一个分布式服务框架是Apache Hadoop 的一个子项目它主要是用来解决 分布式应用中经常遇到的一些数据管理问题如统一命名服务、状态同步服务、集群管理、分布 式应用配置项的管理等。 简单来说zookeeper本质 存储 监听通知。 Zookeeper 用来做服务注册中心主要是因为它具有节点变更通知功能只要客户端监听相 关服务节点服务节点的所有变更都能及时的通知到监听客户端这样作为调用方只要使用 Zookeeper 的客户端就能实现服务节点的订阅和变更通知功能了非常方便。另外Zookeeper 可用性也可以因为只要半数以上的选举节点存活整个集群就是可用的最少节点数为3。 Eureka 由Netflix开源并被Pivatal集成到SpringCloud体系中它是基于 RestfulAPI 风格开发的服务 注册与发现组件。 Consul Consul是由HashiCorp基于Go语言开发的支持多数据中心分布式高可用的服务发布和注册服 务软件 采用Raft算法保证服务的一致性且支持健康检查。 Nacos Nacos是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。简单来说 Nacos 就是 注册中心 配置中心的组合帮助我们解决微服务开发必会涉及到的服务注册 与发 现服务配置服务管理等问题。Nacos 是 Spring Cloud Alibaba 核心组件之一负责服务注册 与发现还有配置。 对比
11.Eureka 基础架构及交互原理 12.搭建Eureka Server
aifeng-parent 父工程引入Spring Cloud依赖
Spring Cloud是一个综合项目下面有很多子项目比如eureka子项目
dependencyManagement dependencies dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-dependencies/artifactId versionGreenwich.RELEASE/version typepom/type scopeimport/scope /dependency /dependencies
/dependencyManagementaifeng-cloud-eureka 工程pom.xml文件引入依赖
dependencies !--Eureka server依赖-- dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-netflix-eureka-server/artifactId /dependency
/dependencies父工程
!--引入Jaxb开始-- dependency groupIdcom.sun.xml.bind/groupId artifactIdjaxb-core/artifactId version2.2.11/version /dependency dependency groupIdjavax.xml.bind/groupId artifactIdjaxb-api/artifactId /dependency dependency groupIdcom.sun.xml.bind/groupId artifactIdjaxb-impl/artifactId version2.2.11/version /dependency dependency groupIdorg.glassfish.jaxb/groupId artifactIdjaxb-runtime/artifactId version2.2.10-b140310.1920/version /dependency dependency groupIdjavax.activation/groupId artifactIdactivation/artifactId version1.1.1/version /dependency
!--引入Jaxb结束--在yml文件中配置Eureka Server服务端口服务名等信息 server:port: 9200
spring:application:name: aifeng-cloud-eureka
eureka:client: # Eureka Server本身也是eureka的一个客户端因为在集群下需要与其他eureka server 进行数据的同步service-url: # 客户端与 EurekaServer交互的地址如果是集群也需要写其他Server的地址#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/defaultZone: http://localhost:9200/eurekaregister-with-eureka: false # 自己就是服务不需要注册自己 | 表示是否向Eureka中心注册自己的信息因为自己就是Eureka Server所以不进行注册默认微truefetch-registry: false # 自己就是服务不需要从 Eureka Server获取信息默认微true,置为false | 表示是否查询/拉取Eureka Server服务注册列表默认微trueinstance:hostname: localhost # 当前eureka实例的主机名编写启动类声明当前服务为Eureka注册中心 package com.aifeng.eureka;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;SpringBootApplication
EnableEurekaServer //标识当前项目就是一个Eureka Server
public class EurekaApplication {public static void main(String[] args) {SpringApplication.run(EurekaApplication.class,args);}
}浏览器访问 http://localhost:9200/ yml 配置 server:port: 9200
spring:application:name: aifeng-cloud-eureka
eureka:client: # Eureka Server本身也是eureka的一个客户端因为在集群下需要与其他eureka server 进行数据的同步service-url: # 客户端与 EurekaServer交互的地址如果是集群也需要写其他Server的地址#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/defaultZone: http://localhost:9200/eurekaregister-with-eureka: true # 自己就是服务不需要注册自己 | 表示是否向Eureka中心注册自己的信息因为自己就是Eureka Server所以不进行注册默认微truefetch-registry: true # 自己就是服务不需要从 Eureka Server获取信息默认微true,置为false | 表示是否查询/拉取Eureka Server服务注册列表默认微trueinstance:#hostname: localhost # 当前eureka实例的主机名#使用ip注册否则会使用主机名注册了此处考虑到对老版本的兼容新版本经过实验都是ipprefer-ip-address: true #自定义实例显示格式加上版本号便于多版本管理注意是ip-address早期版本是ipAddressinstance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:project.version浏览器测试
13.将微服务注册到Eureka 商品微服务和页面静态化微服务注册到Eureka pom文件中添加Eureka Client依赖 !--Eureka client--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependencyapplication.yml 配置Eureka服务端信息 eureka:client: # Eureka Server本身也是eureka的一个客户端因为在集群下需要与其他eureka server 进行数据的同步service-url: # 客户端与 EurekaServer交互的地址如果是集群也需要写其他Server的地址#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/defaultZone: http://localhost:9200/eurekainstance:#hostname: localhost # 当前eureka实例的主机名#使用ip注册否则会使用主机名注册了此处考虑到对老版本的兼容新版本经过实验都是ipprefer-ip-address: true #自定义实例显示格式加上版本号便于多版本管理注意是ip-address早期版本是ipAddressinstance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:project.version修改启动类启用客户端服务 使用EnableDiscoveryClient注解 //使用这个注解,更加通用不管什么的注册中心都可以使用比如阿里的 nacos 相比 Eureka来说 内容更加丰富
SpringBootApplication
//EnableEurekaClient //表示: 将当前项目作为 Eureka Client 注册到Eureka Server, 只能在Eureka环境中使用
EnableDiscoveryClient //表示也是将当前项目表示为注册中心的客户端向注册中心进行注册可以在所有的注册中心环境下使用
MapperScan(com.aifeng.product.mapper)// 扫描mapper
public class ProductApplication {public static void main(String[] args) {SpringApplication.run(ProductApplication.class,args);}
}开起page 和 product 服务重新刷新浏览器正常展示
14.搭建Eureka Server高可用集群 创建2台 Eureka Server 修改host文件 windows11: C:\Windows\System32\drivers\etc 127.0.0.1 AifengCloudEurekaServerB
127.0.0.1 AifengCloudEurekaServerA9200 端口的Eureka服务的 applicaion.yml 文件配置 server:port: 9200
spring:application:name: aifeng-cloud-eureka
eureka:client: # Eureka Server本身也是eureka的一个客户端因为在集群下需要与其他eureka server 进行数据的同步service-url: # 客户端与 EurekaServer交互的地址如果是集群也需要写其他Server的地址 #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/defaultZone: http://AifengCloudEurekaServerB:9201/eurekaregister-with-eureka: true # 自己就是服务不需要注册自己 | 表示是否向Eureka中心注册自己的信息因为自己就是Eureka Server所以不进行注册默认微truefetch-registry: true # 自己就是服务不需要从 Eureka Server获取信息默认微true,置为false | 表示是否查询/拉取Eureka Server服务注册列表默认微trueinstance:#hostname: localhost # 当前eureka实例的主机名#使用ip注册否则会使用主机名注册了此处考虑到对老版本的兼容新版本经过实验都是ipprefer-ip-address: true #自定义实例显示格式加上版本号便于多版本管理注意是ip-address早期版本是ipAddressinstance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:project.version9201 端口的Eureka服务的 application.yml 文件配置 server:port: 9201
spring:application:name: aifeng-cloud-eureka
eureka:client: # Eureka Server本身也是eureka的一个客户端因为在集群下需要与其他eureka server 进行数据的同步service-url: # 客户端与 EurekaServer交互的地址如果是集群也需要写其他Server的地址#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ |集群情况下defaultZone 设置为其他 Eureka Server的地址, 集群-多个地址逗号隔开即可defaultZone: http://AifengCloudEurekaServerA:9200/eurekaregister-with-eureka: true # 自己就是服务不需要注册自己 | 表示是否向Eureka中心注册自己的信息因为自己就是Eureka Server所以不进行注册默认微truefetch-registry: true # 自己就是服务不需要从 Eureka Server获取信息默认微true,置为false | 表示是否查询/拉取Eureka Server服务注册列表默认微trueinstance:#hostname: localhost # 当前eureka实例的主机名#使用ip注册否则会使用主机名注册了此处考虑到对老版本的兼容新版本经过实验都是ipprefer-ip-address: true #自定义实例显示格式加上版本号便于多版本管理注意是ip-address早期版本是ipAddressinstance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:project.version商品微服务的 application.yml 文件配置 server:port: 9000 #微服务的集群环境中通常会为每一个微服务叠加
spring:application:name: aifeng-service-productdatasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://192.168.11.128:3306/springclouddata?useUnicodetruecharacterEncodingutf8serverTimezoneUTCusername: rootpassword: 123456
eureka:client: # Eureka Server本身也是eureka的一个客户端因为在集群下需要与其他eureka server 进行数据的同步service-url: # 客户端与 EurekaServer交互的地址如果是集群也需要写其他Server的地址#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/defaultZone: http://AifengCloudEurekaServerA:9200/eureka,http://AifengCloudEurekaServerB:9201/eurekainstance:#hostname: localhost # 当前eureka实例的主机名#使用ip注册否则会使用主机名注册了此处考虑到对老版本的兼容新版本经过实验都是ipprefer-ip-address: true #自定义实例显示格式加上版本号便于多版本管理注意是ip-address早期版本是ipAddressinstance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:project.version页面静态化微服务 server:port: 9100 #微服务的集群环境中通常会为每一个微服务叠加
spring:application:name: aifeng-service-pagedatasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://192.168.11.128:3306/springclouddata?useUnicodetruecharacterEncodingutf8serverTimezoneUTCusername: rootpassword: 123456eureka:client: # Eureka Server本身也是eureka的一个客户端因为在集群下需要与其他eureka server 进行数据的同步service-url: # 客户端与 EurekaServer交互的地址如果是集群也需要写其他Server的地址#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/defaultZone: http://AifengCloudEurekaServerA:9200/eureka,http://AifengCloudEurekaServerB:9201/eurekainstance:#hostname: localhost # 当前eureka实例的主机名#使用ip注册否则会使用主机名注册了此处考虑到对老版本的兼容新版本经过实验都是ipprefer-ip-address: true #自定义实例显示格式加上版本号便于多版本管理注意是ip-address早期版本是ipAddressinstance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:project.version查看Eureka Server 的注册服务中心 http://aifengcloudeurekaservera:9200/
http://aifengcloudeurekaserverb:9201/ 15.改造服务消费者调用服务提供者 使用Eureka 方式调用静态页面微服务 import org.springframework.cloud.client.discovery.DiscoveryClient; //使用 SpringCloud 官方提供的客户端RestController
RequestMapping(/page)
public class PageController {Autowiredprivate RestTemplate restTemplate;Autowiredprivate DiscoveryClient discoveryClient; //使用 SpringCloud 官方提供的客户端GetMapping(/getProduct/{id})public Products getProduct(PathVariable Integer id){//通过aifeng-service-product在服务注册中心注册的服务列表因为一般情况下实例都是集群ListServiceInstance instances discoveryClient.getInstances(aifeng-service-product);//因为实在实验环境中获取商品服务列表中的第一个就可以了ServiceInstance instanceInfo instances.get(0);//获取商品微服务的主机地址String host instanceInfo.getHost();//获得商品微服务的端口号int port instanceInfo.getPort();//拼接URLString url http:// host :portproduct/query/id;//发送HTTP请求给商品微服务将ID传过去获取ID所对应的products对象//String url http://127.0.0.1:9000/product/query/;Products products restTemplate.getForObject(url, Products.class);return products;}}注意使用 Spring Cloud官提供的 Client 因为在启用客户端的时候使用的是 Spring cloud 官方提供的 EnableDiscoveryClient 注解 所以注入到容器中的 Client 实例对象是 Spring cloud 官方提供的所以在 Autowired 注入 的时候也要用 Spring Cloud 下的 DiscoveryClient package com.aifeng.page;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; //Spring cloud 官方提供的
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;SpringBootApplication
//EnableEurekaClient
EnableDiscoveryClient //使用这个注解,更加通用,比如阿里的 nacos 相比 Eureka来说 内容更加丰富
public class PageApplication {public static void main(String[] args) {SpringApplication.run(PageApplication.class,args);}//向容器中注入一个RestTemplate,封装了HttpClientBeanpublic RestTemplate restTemplate(){return new RestTemplate();}
}注入了 netflix 提供的DiscoveryClient com.netflix.discovery.DiscoveryClient 会报错提示 2024-01-25 22:36:18.453 ERROR 20756 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter : ***************************
APPLICATION FAILED TO START
***************************Description:Field discoveryClient in com.aifeng.page.controller.PageController required a bean of type com.netflix.discovery.DiscoveryClient that could not be found.The injection point has the following annotations:- org.springframework.beans.factory.annotation.Autowired(requiredtrue)Action:Consider defining a bean of type com.netflix.discovery.DiscoveryClient in your configuration.Process finished with exit code 0使用Spring Cloud官提供的 Client 调用接口正常返回
/EnableEurekaClient EnableDiscoveryClient //使用这个注解,更加通用,比如阿里的 nacos 相比 Eureka来说 内容更加丰富 public class PageApplication { public static void main(String[] args) {SpringApplication.run(PageApplication.class,args);}//向容器中注入一个RestTemplate,封装了HttpClientBeanpublic RestTemplate restTemplate(){return new RestTemplate();}} 注入了 netflix 提供的DiscoveryClient com.netflix.discovery.DiscoveryClient **会报错提示**java
2024-01-25 22:36:18.453 ERROR 20756 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter : ***************************
APPLICATION FAILED TO START
***************************Description:Field discoveryClient in com.aifeng.page.controller.PageController required a bean of type com.netflix.discovery.DiscoveryClient that could not be found.The injection point has the following annotations:- org.springframework.beans.factory.annotation.Autowired(requiredtrue)Action:Consider defining a bean of type com.netflix.discovery.DiscoveryClient in your configuration.Process finished with exit code 0使用Spring Cloud官提供的 Client 调用接口正常返回 文章转载自: http://www.morning.nysjb.cn.gov.cn.nysjb.cn http://www.morning.sjjq.cn.gov.cn.sjjq.cn http://www.morning.csdgt.cn.gov.cn.csdgt.cn http://www.morning.dbdmr.cn.gov.cn.dbdmr.cn http://www.morning.ckwrn.cn.gov.cn.ckwrn.cn http://www.morning.rwyd.cn.gov.cn.rwyd.cn http://www.morning.zmwd.cn.gov.cn.zmwd.cn http://www.morning.rxfjg.cn.gov.cn.rxfjg.cn http://www.morning.lxfyn.cn.gov.cn.lxfyn.cn http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.rfbt.cn.gov.cn.rfbt.cn http://www.morning.zfrs.cn.gov.cn.zfrs.cn http://www.morning.hkshy.cn.gov.cn.hkshy.cn http://www.morning.xrwbc.cn.gov.cn.xrwbc.cn http://www.morning.rpwht.cn.gov.cn.rpwht.cn http://www.morning.kcxtz.cn.gov.cn.kcxtz.cn http://www.morning.xdhcr.cn.gov.cn.xdhcr.cn http://www.morning.mxhcf.cn.gov.cn.mxhcf.cn http://www.morning.wmsgt.cn.gov.cn.wmsgt.cn http://www.morning.qcdtzk.cn.gov.cn.qcdtzk.cn http://www.morning.lzrpy.cn.gov.cn.lzrpy.cn http://www.morning.njnqn.cn.gov.cn.njnqn.cn http://www.morning.pqypt.cn.gov.cn.pqypt.cn http://www.morning.kyctc.cn.gov.cn.kyctc.cn http://www.morning.tbksk.cn.gov.cn.tbksk.cn http://www.morning.sffkm.cn.gov.cn.sffkm.cn http://www.morning.ctqlq.cn.gov.cn.ctqlq.cn http://www.morning.sypzg.cn.gov.cn.sypzg.cn http://www.morning.xrlwr.cn.gov.cn.xrlwr.cn http://www.morning.pycpt.cn.gov.cn.pycpt.cn http://www.morning.bkqdg.cn.gov.cn.bkqdg.cn http://www.morning.llmhq.cn.gov.cn.llmhq.cn http://www.morning.xplng.cn.gov.cn.xplng.cn http://www.morning.pqyms.cn.gov.cn.pqyms.cn http://www.morning.bykqg.cn.gov.cn.bykqg.cn http://www.morning.glnmm.cn.gov.cn.glnmm.cn http://www.morning.cffwm.cn.gov.cn.cffwm.cn http://www.morning.yymlk.cn.gov.cn.yymlk.cn http://www.morning.cklgf.cn.gov.cn.cklgf.cn http://www.morning.xprq.cn.gov.cn.xprq.cn http://www.morning.dwgcx.cn.gov.cn.dwgcx.cn http://www.morning.kabaifu.com.gov.cn.kabaifu.com http://www.morning.qbmjf.cn.gov.cn.qbmjf.cn http://www.morning.qqhfc.cn.gov.cn.qqhfc.cn http://www.morning.jbshh.cn.gov.cn.jbshh.cn http://www.morning.smpmn.cn.gov.cn.smpmn.cn http://www.morning.xckqs.cn.gov.cn.xckqs.cn http://www.morning.xnyfn.cn.gov.cn.xnyfn.cn http://www.morning.thpzn.cn.gov.cn.thpzn.cn http://www.morning.jwtwf.cn.gov.cn.jwtwf.cn http://www.morning.bqmdl.cn.gov.cn.bqmdl.cn http://www.morning.trjr.cn.gov.cn.trjr.cn http://www.morning.zqmdn.cn.gov.cn.zqmdn.cn http://www.morning.qlsyf.cn.gov.cn.qlsyf.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.spnky.cn.gov.cn.spnky.cn http://www.morning.lthpr.cn.gov.cn.lthpr.cn http://www.morning.jphxt.cn.gov.cn.jphxt.cn http://www.morning.bysey.com.gov.cn.bysey.com http://www.morning.ummpdl.cn.gov.cn.ummpdl.cn http://www.morning.tldfp.cn.gov.cn.tldfp.cn http://www.morning.ndngj.cn.gov.cn.ndngj.cn http://www.morning.mpxbl.cn.gov.cn.mpxbl.cn http://www.morning.wfcqr.cn.gov.cn.wfcqr.cn http://www.morning.mgtrc.cn.gov.cn.mgtrc.cn http://www.morning.qhmgq.cn.gov.cn.qhmgq.cn http://www.morning.xmttd.cn.gov.cn.xmttd.cn http://www.morning.wwwghs.com.gov.cn.wwwghs.com http://www.morning.cfcpb.cn.gov.cn.cfcpb.cn http://www.morning.tqbw.cn.gov.cn.tqbw.cn http://www.morning.zcyxq.cn.gov.cn.zcyxq.cn http://www.morning.pkfpl.cn.gov.cn.pkfpl.cn http://www.morning.ogzjf.cn.gov.cn.ogzjf.cn http://www.morning.rbjth.cn.gov.cn.rbjth.cn http://www.morning.snxbf.cn.gov.cn.snxbf.cn http://www.morning.fsbns.cn.gov.cn.fsbns.cn http://www.morning.qpmmg.cn.gov.cn.qpmmg.cn http://www.morning.mtbsd.cn.gov.cn.mtbsd.cn http://www.morning.qysnd.cn.gov.cn.qysnd.cn http://www.morning.rdfq.cn.gov.cn.rdfq.cn