SpringCloud是目前微服务改造的首选,主要是社区支持活跃,配套组件齐全。本章笔者将结合SpringCloudNetfix的几个核心组件,逐步与老手一起完成全套应用框架的搭建,让有需要的老手直接在项目中使用。在SpringCloudNetfix中,核心组件包括:注册中心(Eureka)、负载均衡(Ribbon)、服务调用(Feign)、熔断与降级(Hystrix)、网关(Gateway)、配置中心(Config)、链路跟踪(Sleuth)和其他主要部件。我们都知道SpringCloud是一个基于SpringBoot的微服务框架。也就是说,在开发SpringCloud微服务的时候,我们必须要用到SpringBoot,所以这里对SpringCloud的集成也遵循了SpringBoot的一定集成规范,比如对于依赖的引用如下:spring-cloud-starter-xxx,其中xxx是值得的我们使用的组件。比如我们需要集成Ribbon,它的依赖名称是spring-cloud-starter-netflix-ribbon。首先笔者将应用的整体情况介绍如下:1、应用的整体架构图:2、使用的SpringCloud版本为Hoxton.SR9,老手可以根据自己的需要选择版本。3、微服务的代码层次。Parent是所有模块的父依赖??,主要管理公共依赖;common是所有模块都需要用到的一些通用类;springcloud-demo-eureka-service是eureka的注册中心和配置中心;global-gateway是网关;order-demo和product-demo是具体的服务。4.JDK版本笔者使用的是JDK1.8。5.需要提前下载zipkin服务。可以在网上搜索下载地址。下载后执行:java-jarzipkin.jar启动,然后访问:http://localhost:9411/zipkin/看是否可以正常访问。正常情况下,访问显示如下:6.登录github,创建服务。比如笔者这里创建了一个springcloud-demo-config服务来存放配置文件。7、启动顺序:注册中心和配置中心->服务提供者->消费者->网关。接下来看各个模块的代码:1.parentparent的主要作用是管理公共依赖。核心是一个pom.xml文件。需要注意的是,它的parent是spring-boot-starter-parent:org.springframework.bootspring-boot-starter-parent2.3。6.RELEASEmy.springcloud.demoparent0.0.1-SNAPSHOTpom8Hoxton.SR9product-demospringcloud-demo-eureka-serviceorder-demospringcloud-demo-eureka-service2comm关于global-gatewayorg.springframework.cloudspring-cloud-dependencies${spring-cloud.version}pomimportmy.springcloud.demo复制代码groupId>common0.0.1-SNAPSHOTorg.springframework.启动spring-boot-maven-plugin2.eureka+config这个模块主要是2.1的注册中心和配置中心。依赖项:org.springframework.bootspring-boot-starter-weborg.springframework.cloudspring-cloud-starter-netflix-eureka-serverorg.springframework.cloudspring-cloud-config-server它的两个主要依赖是spring-cloud-config-server和spring-cloud-starter-netflix-eureka-server。2.2.配置文件:其配置文件application.properties如下:server.port=8761spring.application.name=springcloud-demo-eureka-service#最好选择一个容易区分的名字eureka.instance.instance-id=springclouddemo1.com#false不注册自己,当然也可以设置为true,自己注册eureka.client.register-with-eureka=false#config服务使用git存储数据,这里配置git仓库spring.cloud的地址.config.server.git.uri=https://github.com/xxxx/springcloud-demo-config.gitspring.cloud.config.server.git.username=xxxxxx@126.comspring.cloud.config.server.git.password=xxxxxxxx#指定仓库分支spring.cloud.config.server.git.default-label=mastereureka.instance.prefer-ip-address=true2.3.启动类注解在启动类中添加三个注解:@EnableEurekaServer@SpringBootApplication@EnableConfigServer。3.网关作为应用的入口。它的主要功能是请求转发、统一校验、路由、限流等,这里我们通过网关转发给后台应用。网关作为接入入口,也应该是呼叫链路的发起方。3.1.依赖:org.springframework.cloudspring-cloud-starter-netflix-eureka-client/dependency>org.springframework.cloudspring-cloud-starter-gatewayorg.springframework.cloudspring-cloud-starter-zipkinorg.springframework.cloudspring-cloud-starter-config3.2。在配置文件bootstrap.yml中指定配置服务:spring:application:name:global-gatewaycloud:config:uri:http://localhost:8761label:master3.3。远程仓库配置文件这里我们使用配置中心的远程配置,所以在git仓库的springcloud-dem在o-config.git应用下,添加网关的配置文件:global-gateway-${active}.properties,指定注册中心和zipkin的地址,同时配置网关的自动发现,内容如下:spring.application.name=global-gatewayserver.port=9006eureka.client.fetch-registry=trueureka.client.register-with-eureka=trueureka.client.service-url.defaultZone=http://localhost:8761/eureka/#配置网关自动发现spring.cloud.gateway.discovery.locator.enabled=truespring.zipkin.base-url=http://localhost:9411/spring.sleuth.sampler.probability=13.4.启动类注解,然后在启动类中添加如下注解:@SpringBootApplication@??EnableDiscoveryClient通过网关访问时,指定服务名称和服务URL,如:http://localhost:9006/ORDER-DEMO/order/getOrder?id=14。商品服务作为具体的服务提供者,需要在注册中心注册该服务;同时需要从配置中心读取配置文件,即作为配置服务的客户端;上报调用链路数据;4.1.依赖项:org.springframework.cloudspring-cloud-starter-netflix-eureka-clientorg.springframework.bootspring-boot-starter-web</artifactId>org.springframework.cloudspring-cloud-starter-netflix-hystrix org.springframework.cloud spring-cloud-starter-zipkinorg.springframework.cloudspring-cloud-starter-config4.2。在配置文件bootstrap.在yml中指定配置服务:spring:application:name:product-democloud:config:uri:http://localhost:8761label:master4.3。远程仓库配置文件这里我们使用配置中心的远程配置,所以在git仓库的springcloud-demo-config.git应用下,添加商品服务的配置文件:product-demo-${active}.propertiesspring.application.name=product-demoeureka.instance.instance-id=productdemoeureka.instance。主机ame=localhostserver.port=8080eureka.client.service-url.defaultZone=http://localhost:8761/eureka/management.endpoint.info.enabled=truemanagement.endpoints.web.exposure.include=*management.endpoint.health.enabled=truemanagement.endpoint.health.show-details=alwaysmanagement.endpoint.shutdown.enabled=trueinfo.app.name=productDemoinfo.company.name=testinfo.test.tt=这是产品spring.zipkin.base-url=http://localhost:9411/spring.sleuth.sampler.probability=14.4。启动类注解如果需要开启熔断器,需要在启动类中添加注解:SpringBootApplication和EnableCircuitBreaker5。订单服务类似于商品服务,但它需要消费商品服务,作为服务消费者,需要向注册中心注册服务,同时需要从注册中心同步服务信息;同时需要从配置中心读取配置文件,即作为配置服务的客户端;上报呼叫链路数据;5.1.依赖:org.springframework.bootspring-boot-starter-weborg.springframework.boot弹簧-引导启动器执行器org.springframework.cloudspring-cloud-starter-netflix-eureka-client<依赖>org.springframework.cloudspring-cloud-starter-netflix-ribbonorg.springframework.cloudspring-cloud-starter-openfeignorg.springframework.cloudspring-cloud-starter-netflix-hystrix org.springframework.cloud spring-cloud-starter-zipkinorg.springframework.cloudspring-cloud-starter-config5.2。在配置文件bootstrap.yml中指定配置服务:spring:application:name:order-democloud:config:uri:http://localhost:8761label:master5.3。远程仓库配置文件这里我们使用配置中心的远程配置,所以在git仓库的springcloud-demo-config.git应用下,添加商品服务的配置文件:product-demo-${active}.propertiesspring。application.name=order-demoserver.port=8081eureka.instance.hostname=localhosteureka.instance.instance-id=orderdemomanagement.endpoint.info.enabled=trueeureka.client.service-url.defaultZone=http://localhost:8761/尤里卡/hystrix.command.default.execution.isolation.thread.timeoutInMilsiseconds=5000#zipkinspring.zipkin.base-url=http://localhost:9411/spring.sleuth.sampler.probability=15.4。启动类注解如果需要开启熔断,需要在启动类中添加注解:@SpringBootApplication@EnableHystrix如果微服务调用使用Feign,还需要添加@EnableFeignClients注解,添加接口,添加FeignClient接口的注解。比如作者会在这里调用PRODUCT-DEMO服务:@FeignClient("PRODUCT-DEMO")publicinterfaceProductFeignService{@RequestMapping("/product/getProduct")publicProductgetProduct(@RequestParam(value="id")Integerid);}当然,如果要使用Ribbon调用,需要在配置类中添加如下代码:@Bean@LoadBalancedpublicRestTemplatecreateRestTemplate(){returnnewRestTemplate();使用时,直接通过如下注解注入即可使用:@AutowiredprivateRestTemplaterestTemplate;然后再写其他的代码,完成服务之间的调用。这里我要fuse发fuse的地方有两个:一个是服务商。当服务异常时,服务自身可以触发熔断,直接返回统一错误;另一个是消费者。在这种情况下,Fusing有另一个术语,也称为降级。一般是服务方不能正常提供服务时消费者的反应,比如访问超时,或者主动断开服务。发起请求的代码全部完成后,以此启动,然后通过网关访问,然后登录http://localhost:9411/,可以查看整体的调用依赖和调用链接信息,以及整体施工完成。