简易Spring Cloud工程搭建
- 格式:docx
- 大小:2.22 MB
- 文档页数:74
springcloud组件技术分享(推荐)⽬录1. 项⽬初始化配置2. Eureka3. Ribbon4. Feign学习5. hystrix学习6. springboot的健康监控actuator7. feign配合Hystrix使⽤8. Zuul9. springCloud的ConfigSpringcloud技术分享Spring Cloud 是⼀套完整的微服务解决⽅案,基于 Spring Boot 框架,准确的说,它不是⼀个框架,⽽是⼀个⼤的容器,它将市⾯上较好的微服务框架集成进来,从⽽简化了开发者的代码量。
Spring Cloud 是什么?Spring Cloud 是⼀系列框架的有序集合,它利⽤ Spring Boot 的开发便利性简化了分布式系统的开发,⽐如服务发现、服务⽹关、服务路由、链路追踪等。
Spring Cloud 并不重复造轮⼦,⽽是将市⾯上开发得⽐较好的模块集成进去,进⾏封装,从⽽减少了各模块的开发成本。
换句话说:Spring Cloud 提供了构建分布式系统所需的“全家桶”。
Spring Cloud 现状⽬前,国内使⽤ Spring Cloud 技术的公司并不多见,不是因为 Spring Cloud 不好,主要原因有以下⼏点:Spring Cloud 中⽂⽂档较少,出现问题⽹上没有太多的解决⽅案。
国内创业型公司技术⽼⼤⼤多是阿⾥系员⼯,⽽阿⾥系多采⽤ Dubbo 来构建微服务架构。
⼤型公司基本都有⾃⼰的分布式解决⽅案,⽽中⼩型公司的架构很多⽤不上微服务,所以没有采⽤ Spring Cloud 的必要性。
但是,微服务架构是⼀个趋势,⽽ Spring Cloud 是微服务解决⽅案的佼佼者,这也是作者写本系列课程的意义所在。
Spring Cloud 优缺点其主要优点有:集⼤成者,Spring Cloud 包含了微服务架构的⽅⽅⾯⾯。
约定优于配置,基于注解,没有配置⽂件。
轻量级组件,Spring Cloud 整合的组件⼤多⽐较轻量级,且都是各⾃领域的佼佼者。
【微框架】之⼀:从零开始,轻松搞定SpringCloud微服务系列--开⼭篇(spring。
Spring顶级框架有众多,那么接下的篇幅,我将重点讲解SpringCloud微框架的实现Spring 顶级项⽬,包含众多,我们重点学习⼀下,SpringCloud项⽬以及SpringBoot项⽬————————————————————main————————————————————⼀、SpringCloud项⽬简介 Spring Cloud: 微服务⼯具包,为开发者提供了在分布式系统的配置管理、服务发现、断路器、智能路由、微代理、控制总线等开发⼯具包。
Spring Boot: 旨在简化创建产品级的 Spring 应⽤和服务,简化了配置⽂件,使⽤嵌⼊式web服务器,含有诸多开箱即⽤微服务功能 可以和spring cloud联合部署。
⼆、SpringCloud⼦项⽬介绍 Spring Cloud Config:配置管理开发⼯具包,可以让你把配置放到远程服务器,⽬前⽀持本地存储、Git以及Subversion。
Spring Cloud Bus:事件、消息总线,⽤于在集群(例如,配置变化事件)中传播状态变化,可与Spring Cloud Config联合实现热部署。
Spring Cloud Netflix:针对多种Netflix组件提供的开发⼯具包,其中包括Eureka、Hystrix、Zuul、Archaius等。
Netflix Eureka:云端负载均衡,⼀个基于 REST 的服务,⽤于定位服务,以实现云端的负载均衡和中间层服务器的故障转移。
Netflix Hystrix:容错管理⼯具,旨在通过控制服务和第三⽅库的节点,从⽽对延迟和故障提供更强⼤的容错能⼒。
Netflix Zuul:边缘服务⼯具,是提供动态路由,监控,弹性,安全等的边缘服务。
Netflix Archaius:配置管理API,包含⼀系列配置管理API,提供动态类型化属性、线程安全配置操作、轮询框架、回调机制等功能。
springCloud+dubbo+nacos(服务注册与发现)⼊门案例1、环境准备2、项⽬创建 使⽤IDEA创建⼀个Maven项⽬,在创建三个模块,分别为spring-cloud-nacos-api、spring-cloud-nacos-provider、spring-cloud-nacos-consumer,其中spring-cloud-nacos-api是抽取出来的公共接⼝,使⽤mvn install 命令发布到本地仓库后,在另外两个项⽬中添加依赖。
spring-cloud-nacos-provider、spring-cloud-nacos-consumer为springBoot项⽬。
⽬录结构如下: 在spring-cloud-nacos-sample⽗⼯程中添加⼀下的依赖如下:<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>spring-cloud-nacos-sample</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><modules><module>spring-cloud-nacos-api</module><module>spring-cloud-nacos-provider</module><module>spring-cloud-nacos-consumer</module></modules><properties></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.3.9.RELEASE</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.5.RELEASE</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR9</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>2.3.9.RELEASE</version></dependency></dependencies></project>3、api代码编写 在api中定义⼀个接⼝:使⽤mvn install 发布到仓库public interface IHelloService {String sayHello(String name);}4、编写服务提供者spring-cloud-nacos-provider ⾸先,在pom.xml中添加相关依赖如下:(未指定version的已经在⽗⼯程中统⼀管理)<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><parent><artifactId>spring-cloud-nacos-sample</artifactId><groupId>org.example</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>spring-cloud-nacos-provider</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-dubbo</artifactId></dependency><dependency><groupId>org.example</groupId><artifactId>spring-cloud-nacos-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency></dependencies></project> 然后,编写主启动类和新建⼀个类实现api中的接⼝:@SpringBootApplication@DubboComponentScan//会扫描@DubboService发布的远程服务public class ProviderApplication {public static void main(String[] args) {SpringApplication.run(ProviderApplication.class,args);}}1 @DubboService2public class HelloServiceImpl implements IHelloService {3public String sayHello(String name) {4return "hello,"+name;5 }6 } 注意:@DubboService作⽤是将该接⼝发布为dubbo服务,在⽼的版本中叫@Service 配置application.yml:spring:application:name: spring-cloud-nacos-samplecloud:nacos:discovery: #nacos注册中⼼地址server-addr: 127.0.0.1:8848dubbo:protocol: #使⽤dubbo协议name: dubbo#端⼝号建议都设置⼀下port: 9000 #dubbo主机绑定host: localhostregistry: #Dubbo服务注册中⼼的配置地址,值spring-cloud://localhost表⽰挂载到SpringCloud注册中⼼,不配置会提⽰没有配置中⼼的错误 address: spring-cloud://localhost5、编写服务消费者spring-cloud-nacos-consumer 创建主启动类:@SpringBootApplication@DubboComponentScanpublic class ConsumerApplication {public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class,args);}}添加maven依赖: <?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><parent><artifactId>spring-cloud-nacos-sample</artifactId><groupId>org.example</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>spring-cloud-nacos-consumer</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-dubbo</artifactId></dependency><dependency><groupId>org.example</groupId><artifactId>spring-cloud-nacos-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency></dependencies></project> 创建⼀个HelloController测试类,定义⼀个接⼝:@RestControllerpublic class HelloController {@DubboReferenceprivate IHelloService helloService;@GetMapping("/say")public String sayHello(){return helloService.sayHello("mic");}} 配置application.yml:spring:application:name: spring-cloud-nacos-consumercloud:nacos:discovery:server-addr: 127.0.0.1:8848dubbo:cloud:#订阅已经注册的服务,(实际为spring-cloud-nacos-provider服务中yml⽂件配置的的值)subscribed-services: spring-cloud-nacos-sampleserver:port: 9000 最后,在浏览器端输⼊ localhost:9000/say 看到响应 hello,mic 说明操作成功!浏览器访问localhost:8848/nacos后,在服务管理——>服务列表查看响应的服务实例。
idea搭建springCloud----配置阿⾥maven(⼀)公司最近搭建微服务,现在我将项⽬的⼀点⼀滴记录下来,希望能帮助到需要的⼈⽬前暂时第⼀版为:springboot+mybatis+thymeleaf+shiro+⽇志今天介绍 idea 配置阿⾥maven1.在路径:C:\Users\Administrator\.m2 下创建:repository_al ⽂件夹和 settings_al.xml ⽂件2.settings_al.xml 内容:<?xml version="1.0" encoding="UTF-8"?><!--Licensed to the Apache Software Foundation (ASF) under oneor more contributor license agreements. See the NOTICE filedistributed with this work for additional informationregarding copyright ownership. The ASF licenses this fileto you under the Apache License, Version 2.0 (the"License"); you may not use this file except in compliancewith the License. You may obtain a copy of the License at/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing,software distributed under the License is distributed on an"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANYKIND, either express or implied. See the License for thespecific language governing permissions and limitationsunder the License.--><!--| This is the configuration file for Maven. It can be specified at two levels:|| 1. User Level. This settings.xml file provides configuration for a single user,| and is normally provided in ${user.home}/.m2/settings.xml.|| NOTE: This location can be overridden with the CLI option:|| -s /path/to/user/settings.xml|| 2. Global Level. This settings.xml file provides configuration for all Maven| users on a machine (assuming they're all using the same Maven| installation). It's normally provided in| ${maven.conf}/settings.xml.|| NOTE: This location can be overridden with the CLI option:|| -gs /path/to/global/settings.xml|| The sections in this sample file are intended to give you a running start at| getting the most out of your Maven installation. Where appropriate, the default| values (values used when the setting is not specified) are provided.||--><settings xmlns="/SETTINGS/1.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/SETTINGS/1.0.0 /xsd/settings-1.0.0.xsd"><!-- localRepository| The path to the local repository maven will use to store artifacts.|| Default: ${user.home}/.m2/repository--><localRepository>C:\Users\Administrator\.m2\repository_al</localRepository><!-- interactiveMode| This will determine whether maven prompts you when it needs input. If set to false,| maven will use a sensible default value, perhaps based on some other setting, for| the parameter in question.|| Default: true<interactiveMode>true</interactiveMode>--><!-- offline| Determines whether maven should attempt to connect to the network when executing a build.| This will have an effect on artifact downloads, artifact deployment, and others.|| Default: false<offline>false</offline>--><!-- pluginGroups| This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.| when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers| "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.|--><pluginGroups><!-- pluginGroup| Specifies a further group identifier to use for plugin lookup.<pluginGroup>com.your.plugins</pluginGroup>--></pluginGroups><!-- proxies| This is a list of proxies which can be used on this machine to connect to the network.| Unless otherwise specified (by system property or command-line switch), the first proxy| specification in this list marked as active will be used.|--><proxies><!-- proxy| Specification for one proxy, to be used in connecting to the network.|<proxy><id>optional</id><active>true</active><protocol>http</protocol><username>proxyuser</username><password>proxypass</password><host></host><port>80</port><nonProxyHosts>|</nonProxyHosts></proxy>--></proxies><!-- servers| This is a list of authentication profiles, keyed by the server-id used within the system.| Authentication profiles can be used whenever maven must make a connection to a remote server.|--><servers><!-- server| Specifies the authentication information to use when connecting to a particular server, identified by| a unique name within the system (referred to by the 'id' attribute below).|| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are | used together.|<server><id>deploymentRepo</id><username>repouser</username><password>repopwd</password></server>--><!-- Another sample, using keys to authenticate.<server><id>siteServer</id><privateKey>/path/to/private/key</privateKey><passphrase>optional; leave empty if not used.</passphrase></server>--></servers><!-- mirrors| This is a list of mirrors to be used in downloading artifacts from remote repositories.|| It works like this: a POM may declare a repository to use in resolving certain artifacts.| However, this repository may have problems with heavy traffic at times, so people have mirrored| it to several places.|| That repository definition will have a unique id, so we can create a mirror reference for that| repository, to be used as an alternate download site. The mirror site will be the preferred| server for that repository.|--><mirrors><!-- mirror| Specifies a repository mirror site to use instead of a given repository. The repository that| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.|<mirror><id>mirrorId</id><mirrorOf>repositoryId</mirrorOf><name>Human Readable Name for this Mirror.</name><url>/repo/path</url></mirror>--><mirror><id>alimaven</id><mirrorOf>central</mirrorOf><name>Maven repository provided by aliyun</name><url>/nexus/content/groups/public/</url></mirror><id>uk</id><mirrorOf>central</mirrorOf><name>Maven repository in UK</name><url>/maven2/</url></mirror><mirror><id>osc</id><mirrorOf>central</mirrorOf><name>Maven repository provided by oschina</name><url>/content/groups/public/</url></mirror><mirror><id>osc_thirdparty</id><mirrorOf>thirdparty</mirrorOf><name>Maven repository provided by oschina</name><url>/content/repositories/thirdparty/</url></mirror></mirrors><!-- profiles| This is a list of profiles which can be activated in a variety of ways, and which can modify| the build process. Profiles provided in the settings.xml are intended to provide local machine-| specific paths and repository locations which allow the build to work in the local environment.|| For example, if you have an integration testing plugin - like cactus - that needs to know where| your Tomcat instance is installed, you can provide a variable here such that the variable is| dereferenced during the build process to configure the cactus plugin.|| As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles| section of this document (settings.xml) - will be discussed later. Another way essentially| relies on the detection of a system property, either matching a particular value for the property,| or merely testing its existence. Profiles can also be activated by JDK version prefix, where a| value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.| Finally, the list of active profiles can be specified directly from the command line.|| NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact| repositories, plugin repositories, and free-form properties to be used as configuration| variables for plugins in the POM.||--><profiles><!-- profile| Specifies a set of introductions to the build process, to be activated using one or more of the| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/> | or the command line, profiles have to have an ID that is unique.|| An encouraged best practice for profile identification is to use a consistent naming convention| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.| This will make it more intuitive to understand what the set of introduced profiles is attempting| to accomplish, particularly when you only have a list of profile id's for debug.|| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.<profile><id>jdk-1.4</id><activation><jdk>1.4</jdk></activation><repositories><repository><id>jdk14</id><name>Repository for JDK 1.4 builds</name><url>/maven/jdk14</url><layout>default</layout><snapshotPolicy>always</snapshotPolicy></repository></repositories></profile>--><profile><id>jdk18</id><activation><jdk>1.8</jdk><activeByDefault>true</activeByDefault></activation><properties><piler.source>1.8</piler.source><piler.target>1.8</piler.target><pilerVersion>1.8</pilerVersion></properties></profile><!--| Here is another profile, activated by the system property 'target-env' with a value of 'dev',| which provides a specific path to the Tomcat instance. To use this, your plugin configuration| might hypothetically look like:|| ...| <groupId>org.myco.myplugins</groupId>| <artifactId>myplugin</artifactId>|| <configuration>| <tomcatLocation>${tomcatPath}</tomcatLocation>| </configuration>| </plugin>| ...|| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to | anything, you could just leave off the <value/> inside the activation-property.|<profile><id>env-dev</id><activation><property><name>target-env</name><value>dev</value></property></activation><properties><tomcatPath>/path/to/tomcat/instance</tomcatPath></properties></profile>--></profiles><!-- activeProfiles| List of profiles that are active for all builds.|<activeProfiles><activeProfile>alwaysActiveProfile</activeProfile><activeProfile>anotherAlwaysActiveProfile</activeProfile></activeProfiles>--></settings>3.在idea 配置maven4.注意:sttings_al.xml中: <localRepository>C:\Users\Administrator\.m2\repository_al</localRepository> 的路径对应的是:repository_al; 我使⽤的是idea⾃带的maven 如果导⼊maven项⽬后,右侧报红:终极⽅法是将新建⼀个maven仓库,重新下jar包,如果下完jar包还显⽰红,那你就将idea关掉重新打开项⽬,OK ==================趟过的坑,只有⾃⼰知道有多坑======================。
从零开始搭建springboot+springcloud+mybatis本地项⽬全过程(图解)记录⼀下从零开始搭建⼀个springboot+springcloud+mybatis本地项⽬的demo的过程。
纯代码⼩⽩⼀枚,若有不⾜或错误之处,欢迎⼴⼤朋友指出!开发环境准备:IDE:IntelliJ Idea 2019.3数据库:mysql 8.0.2.2SpringBoot版本:2.2.0.RELEASESpringCloud版本:Hoxton.RELEASE⼀、创建⼀个新的Idea项⽬打开Idea,单击New->File->Project,选择Maven,直接下⼀步输⼊我们的项⽬名,我这⾥起名为pam,单击Finish完成创建⼆、右键单击project下的⽗节点->New->Module,创建⼀个注册中⼼服务端的Module,输⼊⼀下名称,我这⾥以pam-server为例,直接选finish;同理再创建⼀个客户端的Module,输⼊⼀下名称,我这⾥以pam-service为例,直接选finish,结构如图所⽰:三、单击File->Settings->Maven,配置⼀下本地的maven,单击OK四、在最外层pom⽂件引⼊⼀下springboot、springcloud及mybatis的依赖。
这⾥引⼊的springboot版本为2.2.0-RELEASE,与之相对应的springcloud版本为Hoxton.RELEASE。
注意⼆者版本需要相适配,否则会启动报错。
这⾥额外引⼊了⼏个常⽤依赖,直接在maven单击⼀下刷新,仓库即可在线将所需依赖下载进去。
<dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.2.0.RELEASE</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><!--注册中⼼相关依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.4.RELEASE</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-api</artifactId><version>2.10.0</version></dependency><!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.16</version></dependency><!--mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.0.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-tomcat</artifactId><scope>provided</scope></dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId><version>1.3.1.RELEASE</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-contract-stub-runner</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-contract-verifier</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-stream-test-support</artifactId><scope>test</scope></dependency><dependency><groupId>mons</groupId><artifactId>commons-lang3</artifactId><version>3.5</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency></dependencies>五、创建如图所⽰的⽬录结构:①编写注册中⼼,注册中⼼⽬录结构:编写WebApplication启动类,添加注解@EnableEurekaServer和@SpringBootApplication,我这⾥想做⼀个纯注册中⼼,不与数据库挂钩,所以增加了⼀个exclude = DataSourceAutoConfiguration.class,启动项⽬时不⾃动与数据库连接,若想让注册中⼼也连接数据库,这个exclude也可以不要。
SpringCloud微服务架构的实现方式随着互联网的快速发展和业务规模的增长,传统的单体应用架构已经无法满足企业的需求。
为了提高系统的可伸缩性、容错性和可维护性,微服务架构应运而生。
SpringCloud作为一种主流的微服务框架,为开发人员提供了一套完整的解决方案,帮助企业快速而稳定地构建和部署微服务应用。
一、微服务架构简介微服务架构是一种将单一的应用程序拆分成一组小型、松耦合的服务的体系结构模式。
每个服务只负责特定的业务功能,通过独立的进程运行,并通过轻量级的通信机制(如RESTful API)进行互相通信。
微服务架构具有高内聚、低耦合、易于扩展和维护的优势,能够更好地适应快速变化的需求和复杂的业务场景。
二、SpringCloud微服务架构的组成SpringCloud是基于SpringBoot的微服务架构框架,它提供了一系列的子项目,以满足开发人员在微服务架构中所需的各种功能。
1. 服务注册与发现:SpringCloud提供了服务注册与发现的解决方案,其中最核心的组件是Netflix的Eureka和Consul。
通过服务注册,微服务可以自动向注册中心注册自己的信息,其他服务可以通过查询注册中心获取服务的实例信息,实现服务之间的动态发现和调用。
2. 服务调用:在微服务架构中,服务之间的调用是非常频繁的。
SpringCloud提供了多种服务调用的方式,包括基于HTTP的RestTemplate和基于Netflix的Feign。
开发人员可以灵活地选择适合自己的调用方式,并通过负载均衡、容错和熔断等机制提高服务的可用性和稳定性。
3. 服务熔断与容错:为了防止某个微服务的故障引起整个系统的连锁反应,SpringCloud引入了Netflix的Hystrix组件,实现了服务的熔断和容错机制。
通过设置超时时间、限流和降级策略,Hystrix可以在服务不可用或响应时间过长时,快速失败并返回降级的结果。
这样可以有效保护整个系统的稳定性和可用性。
SpringCloud系列之Apollo配置中⼼(三)本篇⽂章为系列⽂章,未读前⼏集的同学请猛戳这⾥:本篇⽂章讲解A p o l l o多环境部署⽅案,教⼤家搭建除了D E V的其他环境。
多环境部署⽅案点击链接观看:(获取更多请关注公众号「哈喽沃德先⽣」)为了让⼤家有更真实的感受,多环境部署⽅案我们在L i n u x环境下搭建,不再使⽤Q u i c k S t a r t脚本。
当项⽬要上线部署到⽣产环境时,项⽬的配置⽐如数据库、缓存、队列等服务器的地址都会发⽣改变,这时候就需要通过A p o l l o为⽣产环境添加配置。
⽬前A p o l l o预先定义的环境为:「D E V」:D e v e l o p m e n t e n v i r o n m e n t开发环境,⽤于开发者调试使⽤;「F A T」:F e a t u r e A c c e p t a n c e T e s t e n v i r o n m e n t功能验收测试环境,⽤于软件测试者测试使⽤;「U A T」:U s e r A c c e p t a n c e T e s t e n v i r o n m e n t⽤户验收测试环境(仿真环境),⽤于⽣产环境下的软件测试者测试使⽤「P R O」:P r o d u c t i o n e n v i r o n m e n t⽣产环境,最终上线环境。
这⾥我们要明确⼀些信息:P o r t a l部署在⽣产环境的机房,通过它来直接管理F A T、U A T、P R O等环境的配置即可;C o n f i g S e r v i c e、A d m i n S e r v i c e和A p o l l o C o n f i gD B在每个环境都单独部署;应⽤需要配置指定的环境,默认为D E V。
总结下来就是:⼀套P o r t a l可以管理多个环境,但是每个环境都需要独⽴部署⼀套C o n f i g S e r v i c e、A d m i n S e r v i c e和A p o l l o C o n f i g D B。
微服务⼊门⼆:SpringCloud(版本HoxtonSR6)⼀、什么是SpringCloud1、官⽅定义1)官⽅定义:springcloud为开发⼈员提供了在分布式系统中快速构建⼀些通⽤模式的⼯具(例如配置管理、服务发现、断路器、智能路由、微代理、控制总线)。
分布式系统的协调导致了锅炉板模式,使⽤springcloud开发⼈员可以快速地建⽴实现这些模式的服务和应⽤程序。
2)springcloud是⼀个含概多个⼦项⽬的开发⼯具集,集合了众多的开源框架,他利⽤了spring boot开发的便利性实现了很多功能,如服务注册,服务注册发现,负载均衡等,springcloud在整合过程中主要是针对Netflix开源组件的封装,springcloud的出现真正的简化了分布式架构的开发。
netflix是美国的⼀个在线视频⽹站,微服务业的翘楚,他是公认的⼤规模⽣产级微服务的杰出实践者,netflix的开源组件已经在他⼤规模分布式微服务环境中经过多年的⽣产实战验证,因此springcloud中很多组件都是基于netflix组件的封装。
2、核⼼架构及其组件1)核⼼组件说明eureka/consul/nacos(alibaba):服务注册中⼼组件rabbion 、openfeign:服务负载均衡和服务调⽤组件hystrix 、hystrix dashboard:服务断路器和服务监控组件zuul/gateway:服务⽹关组件config:统⼀配置中⼼组件bus:消息总线组件3、环境搭建1)版本命名springcloud是⼀个由众多独⽴⼦项⽬组成的⼤型综合项⽬,原则每个⼦项⽬有不同的发布节奏,都维护⾃⼰发布版本号。
为了更好的管理springcloud的版本,通过⼀个资源清单BOM(bill of materials),为了避免与⼦项⽬的发布好混淆,所以没有采⽤版本号的⽅式,⽽是通过命名的⽅式。
这些名字是按字母顺序排列的。
使用Jenkins实现SpringCloud自动化部署Jenkins是一个开源的持续集成和持续交付工具,可以帮助开发团队实现自动化构建、测试和部署。
SpringCloud是一种用于构建分布式系统的框架,它提供了一系列的组件和模块,可以用于快速构建和部署微服务应用程序。
使用Jenkins实现SpringCloud自动化部署可以提高开发团队的效率和生产力,减少手动操作和错误,同时也可以提高应用程序的可靠性和可维护性。
下面将介绍如何使用Jenkins实现SpringCloud自动化部署。
接下来,需要在Jenkins中创建一个新的任务。
打开Jenkins管理界面,点击“新建任务”,输入任务名称并选择“自由风格的软件项目”。
然后点击“确定”按钮。
在任务配置页面,可以配置任务的各种属性和参数。
首先,需要配置源代码管理,选择使用Git或其他版本控制系统管理源代码。
填写代码仓库的URL、分支和认证信息,并配置轮询触发器以实现自动触发构建。
然后,配置构建步骤,选择构建触发器和构建方式。
在构建步骤中,可以配置构建脚本或命令,用于构建和打包SpringCloud应用程序。
可以使用Maven或Gradle等构建工具,通过执行相应的构建命令来构建和打包应用程序。
例如,可以使用以下命令构建和打包一个SpringBoot应用程序:```mvn clean package```构建完成后,可以将构建产物上传到Jenkins服务器上的一个存储库中,以便后续部署使用。
可以使用插件或自定义脚本实现这一步骤。
接下来,需要配置部署步骤,将构建产物部署到目标环境中。
可以使用Docker、Kubernetes或其他部署工具来实现自动化部署。
可以使用相关插件或自定义脚本来实现这一步骤。
在部署步骤中,可以配置部署脚本或命令,用于将构建产物部署到目标环境中。
可以使用Docker命令来创建和启动容器,也可以使用Kubernetes命令来创建和管理Kubernetes资源。
SpringCloud微服务架构及实现基础概念铺垫●单点系统架构一般来说存在两个很大的问题:(1)非高可用:既然是单点,master一旦发生故障,服务就会受到影响(2)性能瓶颈:既然是单点,不具备良好的扩展性,服务性能总有一个上限,这个单点的性能上限往往就是整个系统的性能上限.●传统项目架构传统项目分为三层架构,将业务逻辑层、数据库访问层、控制层放入在一个项目中。
优点:适合于个人或者小团队开发,不适合大团队开发。
●分布式项目架构根据业务需求进行拆分成N个子系统,多个子系统相互协作才能完成业务流程子系统之间通讯使用RPC远程通讯技术。
优点:1.把模块拆分,使用接口通信,降低模块之间的耦合度。
2.把项目拆分成若干个子项目,不同的团队负责不同的子项目。
3.增加功能时只需要再增加一个子项目,调用其它系统的接口就可以。
4.可以灵活的进行分布式部署。
有优点就有缺点,缺点如下:1.系统之间交互需要使用远程通信,接口开发增加工作量。
2.各个模块有一些通用的业务逻辑无法共用。
为了解决上面分布式架构的缺点,我们引入了SOA架构,SOA:Service Oriented Architecture面向服务的架构。
也就是把工程拆分成服务层、表现层两个工程。
服务层中包含业务逻辑,只需要对外提供服务即可。
表现层只需要处理和页面的交互,业务逻辑都是调用服务层的服务来实现。
●什么是项目集群多台服务器部署相同应用构成一个集群作用:通过负载均衡设备共同对外提供服务●RPC远程调用RPC 的全称是 Remote Procedure Call 是一种进程间通信方式。
它允许程序调用另一个地址空间(通常是共享网络的另一台机器上)的过程或函数,而不用程序员显式编码这个远程调用的细节。
即无论是调用本地接口/服务的还是远程的接口/服务,本质上编写的调用代码基本相同。
比如两台服务器A,B,一个应用部署在A服务器上,想要调用B服务器上应用提供的函数或者方法,由于不在一个内存空间,不能直接调用,这时候需要通过就可以应用RPC框架的实现来解决。
简易Spring Cloud工程搭建-初篇序:看了半天的springcloud简介,越看越晕,这东西光看理论不上手练习,理解起来还真费劲。
所以整理了下网上的例子,本文的例子大多参考了csdn的<<史上最简单的SpringCloud教程>>。
吐槽一下此博文的内容确实很好,有些介绍的内容还太全,容易踩坑,故整理下实例方便学习。
个人建议把实例都做一遍,再回头看理论就豁然开朗。
代码下载地址:https:///kaifuu/cloud.git一、创建服务注册中心-Eureka Server1.新建工程es2.点击下一步输入包名3.选择Eureka Server4.选择工程路径,并选择完成。
7.启动工程,访问http://localhost:8000/8.pom.xml如下<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zhf</groupId><artifactId>es</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>es</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.4.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>Finchley.SR1</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><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><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>二、创建一个服务提供者-Eureka Client1.新建工程2.填写包路径3.选择Eureka Discovery4.选择保存路径7.在ES服务界面可以看到EC8.pom.xml如下<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zhf</groupId><artifactId>ec</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>ec</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.4.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>Finchley.SR1</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--<dependency>--><!--<groupId>org.springframework.boot</groupId>--><!--<artifactId>spring-boot-starter-web</artifactId>--><!--</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><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>三、消费服务之-负载均衡客户端-ribbon 准备:需要启动两个ec实例,端口8001和80021.创建工程2.创建包3.选择Web、Discovery和Ribbon4.指定工程目录9. 在浏览器上多次访问http://localhost:8100/get?name=111,因为用ribbon进行了负载均衡,<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zhf</groupId><artifactId>rb</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>rb</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.4.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>Finchley.SR1</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></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><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><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin></plugins></build></project>四、消费服务之-伪Http客户端- Feign1.新建工程2.设置包名3.选择依赖Web、Discovery、Feign4.指定保存目录9.Ribbon与Feign比较Feign 采用的是基于接口的注解11.pom.xml如下<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zhf</groupId><artifactId>feign</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>feign</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.4.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>Finchley.SR1</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><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><build><plugins><plugin>五、断路器- Hystrix准备:本节直接改造Ribbon与Feign工程,故要pom引入spring-cloud-starter-netflix-hystrix依赖新创建工程需要选择Hysrix改造Ribbon3.改造servicepackage com.zhf.service;import flix.hystrix.contrib.javanica.annotation.HystrixCommand;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;@Servicepublic class HelloService {@AutowiredRestTemplate restTemplate;//@HystrixCommand注解:创建了熔断器的功能,并指定了fallbackMethod熔断方法,熔断方法直接返回字符串@HystrixCommand(fallbackMethod = "getError")public String getService(String name) {//ribbon中它会根据服务名来选择具体的服务实例return restTemplate.getForObject("http://ec/index?name=" + name, String.class);}public String getError(String name) {return "sorry," + name + ",i cannot get it!";}}4. 启动ribbon并停掉一个ec8002,在浏览器上多次访问http://localhost:8100/get?name=111改造Feign说明:D版所说需要在yml文件中设置feign.hystrix.enabled=true, 文档中所有工程用的是5.启动工程, 启动feign(停掉一个ec),在浏览器上多次访问6.pom.xml如下<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zhf</groupId><artifactId>feign</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>feign</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.4.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>Finchley.SR1</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><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><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>六、路由网关- zuul 路由转发和过滤器路由转发1.创建工程2.配置包名3.选择依赖4.指定工程目录7.启动ec8001/ec8002/ribbon/feign和zuul工程, 浏览器访问:http://localhost:8300/api-a/get?name=111和http://localhost:8300/api-b/get?name=111 交替显示:hello world i'm port:8001hello world i'm port:8002过滤器准备:基于上节内容1.实现过滤接口package com.zhf.filter;import flix.zuul.ZuulFilter;import flix.zuul.context.RequestContext;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import ponent;import javax.servlet.http.HttpServletRequest;@Componentpublic class ZuuFilter extends ZuulFilter {private static Logger log = LoggerFactory.getLogger(ZuuFilter.class);/*** pre:路由之前* routing:路由之时* post:路由之后* error:发送错误调用* @return*/@Overridepublic String filterType() {return "pre";}/*** 过滤的顺序* @return*/@Overridepublic int filterOrder() {return 0;}/*** 逻辑判断,是否要过滤,本文true,永远过滤。