首页 文章 精选 留言 我的

精选列表

搜索[SpringCloud],共1289篇文章
优秀的个人博客,低调大师

SpringCloud组件之Ribbon

Spring Cloud Ribbon 是一个基于 Http 和 TCP 的客服端负载均衡工具,它是基于 Netflix Ribbon 实现的。客户端负载均衡即是当浏览器向后台发出请求的时候,客户端会向 Eureka Server 读取注册到服务器的可用服务信息列表,根据设定的负载均衡策略(没有设置即用默认的),选择向哪台服务器发送请求 本文将介绍Ribbon的工作原理以及如何在项目中使用 一、Ribbon简介 1、Ribbon工作原理 ribbon实现的关键点是为ribbon定制的RestTemplate,ribbon利用了RestTemplate的拦截器机制,在拦截器中实现ribbon的负载均衡。负载均衡的基本实现就是利用applicationName从服务注册中心获取可用的服务地址列表,然后通过一定算法负载,决定使用哪一个服务地址来

优秀的个人博客,低调大师

SpringCloud组件之Feign

Feign是一个声明式的Web服务客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其进行注释。它具有可插拔注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud添加了对Spring MVC注释的支持,并在Spring Web中使用默认使用的HttpMessageConverters。Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端 本文将介绍Feign的原理和一些相关知识点以及如何在项目中使用 一、Feign的原理 1、启动时,程序会进行包扫描,扫描所有包下所有@FeignClient注解的类,并将这些类注入到spring的IOC容器中。当定义的Feign中的接口被调用时,通过JDK的动态代理来生成

优秀的个人博客,低调大师

SpringCloud组件之Hystrix

在分布式环境中,许多服务依赖项中的一些必然会失败。Hystrix是一个库,通过添加延迟容忍和容错逻辑,帮助你控制这些分布式服务之间的交互。Hystrix通过隔离服务之间的访问点、停止级联失败和提供回退选项来实现这一点,所有这些都可以提高系统的整体弹性 一、Hystrix宗旨和工作原理 Hystrix的宗旨: 防止任何单个依赖项耗尽所有容器(如Tomcat)用户线程 在任何可行的地方提供回退,以保护用户不受失败的影响 使用隔离技术(如隔离板、泳道和断路器模式)来限制任何一个依赖项的影响 通过近实时的度量、监视和警报来优化发现时间 通过配置的低延迟传播来优化恢复时间 避免在整个依赖客户端执行中出现故障,而不仅仅是在网络流量中 Hystrix工作原理 当需要完成某项任务时,通过 Hystrix 将任务包裹起来,交由 Hystrix 来完成任务,从而享受 H

优秀的个人博客,低调大师

SpringCloud组件之Zuul

Zuul是Netflix开源的微服务网关,可以和Eureka、Ribbon、Hystrix等组件配合使用,Spring Cloud对Zuul进行了整合与增强,Zuul默认使用的HTTP客户端是Apache HTTPClient,也可以使用RestClient或okhttp3.OkHttpClient。 Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如/demo/test转发到到demo服务。zuul默认和Ribbon结合实现了负载均衡的功能 本文介绍zuul的工作原理和如何搭建zuul服务以及介绍相关知识点 一、工作原理 zuul的核心是一系列的filters, 其作用类比Servlet框架的Filter,或者AOP。zuul把请求路由到用户处理逻辑的过程中,这些filter参与一些过滤处理,比如Authentic

优秀的个人博客,低调大师

SpringCloud学习之SpringCloudBus

一。spring-cloud-bus是什么? 回答这个问题之前,我们先回顾先前的分布式配置,当配置中心发生变化后,我们需要利用spring-boot-actuator里的refresh端点进行手动刷新: 根据上述示例情况:我们每次要获取最新配置时,要一个一个的通过refresh刷新服务节点,这种方式是不是非常low而且非常麻烦,那该怎么办呢? 大家还记得zookeeper中watch是做什么用的吗?当监控的节点数据发生变化了,那么是不是所有订阅到该节点的客户端都会触发一个订阅回调呢?这其实也类似于我们的消息总线。在微服务架构的系统中,我们通常会使用轻量级的消息代理来构建一个公有的消息主题让系统中所有微服务实例都连接上来,由于该主题中产生的消息会被所有实例监听和消费,所以我们称它为消息总线。 那么在分布式配置中,我们的所有服务都订阅消息总线的话,当配置改变时,配置中心通知消息总线,然后所有的服务节点接收到订阅消息后,在从配置中心获取最新的配置,是不是一个一劳永逸的过程?那么可以得到如下结构图: 二、实现分布式配置的消息总线 我先贴出:项目结构图 我们统一把配置放在config目录下 1、在对应的服务添加对应的依赖 首先我们先配置config-server。gradle配置文件: dependencies { // testCompile group: 'junit', name: 'junit', version: '4.12' compile('org.springframework.cloud:spring-cloud-config-server') compile('org.springframework.kafka:spring-kafka') compile('org.springframework.cloud:spring-cloud-starter-bus-kafka') compile('org.springframework.cloud:spring-cloud-starter-eureka-server') } View Code 注意我们使用kafka作为消息总线的中间件 然后我们依次在所需的服务中添加对应的依赖 dependencies { // testCompile group: 'junit', name: 'junit', version: '4.12' compile('org.springframework.cloud:spring-cloud-starter-config') compile('org.springframework.kafka:spring-kafka') compile('org.springframework.cloud:spring-cloud-starter-bus-kafka') compile('org.springframework.cloud:spring-cloud-starter-eureka-server') } View Code 2、配置对应的application.yml config-server的yml文件: spring: application: name: config-server cloud: config: server: git: uri: file://${user.home}/IdeaProjects/spring-cloud repos: local: pattern: '**/local' uri: file://${user.home}/IdeaProjects/spring-cloud searchPaths: config search-paths: config label: master kafka: bootstrap-servers: localhost:9092 server: port: 8888 endpoints: refresh: sensitive: false bus: sensitive: false View Code 这里面注意要把端点先开放出来,然后进行kafka的相关配置,其余服务的配置文件也进行这样的操作,使其能与消息总线通讯。 3、依次启动服务 当服务启动成功时,SpringBootActuator给我们提供一个/bus/refresh端点,同时我们可以在kafka主题里面找到相应的topics,其名字为springCloudBus: 4、访问Config-Server的/bus/refesh 我们先使用kafka的消费端来监听一下消息内容。运行: ./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic springCloudBus 紧接着我们在访问http://localhost:8888/bus/refresh 刷新config-server的配置 然后我们可以发现消费端,订阅到如下消息: 其中:type为消息的事件类型 timestamp 为消息的时间戳 orginService:消息的来源服务实例 destinationService:消息的目标服务实例,**代表了总线上的所有服务实例 在本例子中,我们可以看到每个服务的ackId都来自于 type为RefreshRemoteApplicationEvent的服务ID 5、运行服务 我们先通过gradle的build任务进行打包会得到如下文件:xxxx.jar与xxx.jar.orginal 那么进入到对应目录下 启动两个服务并注册到注册中心 命令如下: java -Dserver.port=8300 -Dspring.profiles.active=local -jar xxxx.jar java -Dserver.port=8200 -Dspring.profiles.active=local -jar xxxx.jar 注意一定不要在application.yml配置如上参数,否则通过(-Dxxx=xxx)系统变量设置的值将不会生效 此时我们更改config对应的配置并commit后,在运行步骤4,就可以拿到最新的结果了,再次附上相关代码: StudentConfig: package com.hzgj.lyrk.order.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; @ConfigurationProperties(prefix = "student") @Configuration public class StudentConfig { private String name; private String age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @Override public String toString() { return "StudentConfig{" + "name='" + name + '\'' + ", age='" + age + '\'' + '}'; } } View Code order-server-local: student: name: student_local age: 17 View Code

优秀的个人博客,低调大师

springcloud 学习-eureka搭建

组件名:Netflix Eureka 作用:支撑微服务的自注册、自发现,提供负载均衡能力 开发环境使用IDEA,jdk1.8 一、搭建eureka服务 1.新建maven项目,配置pom.xml文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Camden.SR7</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> 2.新建启动类 1 2 3 4 5 6 7 @SpringBootApplication @EnableEurekaServer public class Application{ public static void main(String[]args){ SpringApplication.run(Application. class ,args); } } 3.新建配置文件application.yml 1 2 3 4 5 6 7 8 9 10 11 server: port: 1000 eureka: instance: hostname:localhost client: register-with-eureka: false fetch-registry: false spring: application: name:eureka-server 4.启动(启动类) 5.访问 eureka:http://localhost:1000/ erueka服务器启动成功,目前还未有服务注册 二、搭建服务提供方 1.新建maven项目,配置pom.xml文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Camden.SR7</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> 2.创建Application启动类,提供/hello服务 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Configuration @ComponentScan @EnableEurekaClient @EnableAutoConfiguration @RestController public class Application{ @RequestMapping (value= "hello" ,method=RequestMethod.GET) public Stringhello(){ return "你好,世界" ; } public static void main(String[]args){ new SpringApplicationBuilder(Application. class ).web( true ) .run(args); } } 3、新建application.yml配置文件 1 2 3 4 5 6 7 8 9 eureka: client: serviceUrl: defaultZone:http: //localhost:1000/eureka/ spring: application: name:feign-client-test- 001 server: port: 2000 查看路径id展示,需要添加配置 1 2 3 4 5 6 eureka: client: serviceUrl: defaultZone:http: //admin:admin123@localhost:1000/eureka instance: prefer-ip-address: true 4、运行,查看之前Erueka服务端的页面,FEIGN-CLIENT-TEST-001在注册中心变为了大写这个注意下 5、访问:http://127.0.0.1:2000/hello 三、搭建服务消费方 使用@FeignClient注解 Feignis a declarative web service client. It makes writing web service clients easier. 如上是Spring Cloud文档中对于Feign的定义,结合之前的两篇博文,在这里我们就可以吧Feign简单的理解为用户(前端)可以直接接触到的REST接口提供者。在Feign中,我们可以方便的访问和使用意已经在Erueka服务器中注册过的服务了。 1、建立maven工程,配置pom.xml文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 1.5 . 2 .RELEASE</version> <relativePath/><!--lookupparentfromrepository--> </parent> <properties> <project.build.sourceEncoding>UTF- 8 </project.build.sourceEncoding> <project.reporting.outputEncoding>UTF- 8 </project.reporting.outputEncoding> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</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>Camden.SR6</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> 2、建立包及启动类FeignApplication 1 2 3 4 5 6 7 8 9 10 11 12 13 14 /** *Createdbygaofengon2017/7/14. */ @Configuration @ComponentScan @EnableAutoConfiguration @EnableEurekaClient @EnableFeignClients @SpringBootApplication public class FeignApplication{ public static void main(String[]args){ SpringApplication.run(FeignApplication. class ,args); } } 3、建立接口类,用来调用上文中FEIGN-CLIENT-TEST-001服务的方法hello(),FEIGN-CLIENT-TEST-001是全大写的 1 2 3 4 5 @FeignClient ( "FEIGN-CLIENT-TEST-001" ) public interface IHello{ @RequestMapping (value= "/hello" ,method=RequestMethod.GET) Stringhello(); } 其中@FeignClient中指定需要调用的微服务的名称(全大写),@RequestMapping中指定访问微服务响应接口的路径,如之前微服务的hello方法是通过"/hello"路径访问,那么这里需要配置一致 4、新建Controller类,为前端提供REST接口 1 2 3 4 5 6 7 8 9 @RestController public class HelloController{ @Autowired private IHelloiHello; @RequestMapping (value= "gethello" ,method=RequestMethod.GET) public StringgetHello(){ return iHello.hello(); } } 5、配置Feign的配置文件,指定Eureka服务器注册地址和访问端口application.yml 1 2 3 4 5 6 7 8 9 server: port: 8081 eureka: client: serviceUrl: defaultZone:http: //localhost:1000/eureka/ spring: application: name:feign-client-test- 002 6、运行,查看之前Erueka服务端的页面 7、访问:http://127.0.0.1:8081/gethello 这里访问的就是feign-client-test-001的hello服务。 本文转自gaofeng36599 51CTO博客,原文链接:http://blog.51cto.com/786678398/1947471

优秀的个人博客,低调大师

SpringCloud LoadBalancer灰度策略实现

如何使用 Spring Cloud 2020 中重磅推荐的负载均衡器 Spring Cloud LoadBalancer (下文简称 SCL),如何扩展负载均衡策略? 你将从本文中获取到答案 快速上手 SCL 如果项目中想使用 SCL,则仅需要添加如下 maven 依赖即可 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> SCL 是构建服务发现的基础上,由于目前 Spring Cloud Alibaba 并未兼容 SCL (具体兼容方案可以参考 pig),当然你可以选择使用Eureka 测试。 若将 RestTemplate 和 客户端负载均衡结合使用,在 bean 定义上增加 @LoadBalanced 注解即可. @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } 个性化负载均衡策略 目前版本 (spring cloud 2020) 内置轮询、随机的负载均衡策略,默认轮询策略。 当然可以通过 LoadBalancerClient 注解,指定服务级别的负载均衡策略 @LoadBalancerClient(value = "demo-provider", configuration = RandomLoadbalancerConfig.class) public class RandomLoadbalancerConfig { @Bean public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory) { String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME); return new RandomLoadBalancer( loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name); } } 自定义负载均衡策略 通过上文可知,目前 SCL 支持的负载均衡策略相较于 Ribbon 还是较少,需要开发者自行实现,好在 SCL 提供了便捷的 API 方便扩展使用。 这里演示自定义一个基于注册中心元数据的灰度负载均衡策略。 定义灰度负载均衡策略 @Slf4j public class GrayRoundRobinLoadBalancer extends RoundRobinLoadBalancer { private ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider; private String serviceId; @Override public Mono<Response<ServiceInstance>> choose(Request request) { ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider .getIfAvailable(NoopServiceInstanceListSupplier::new); return supplier.get(request).next().map(serviceInstances -> getInstanceResponse(serviceInstances, request)); } Response<ServiceInstance> getInstanceResponse(List<ServiceInstance> instances, Request request) { // 注册中心无可用实例 抛出异常 if (CollUtil.isEmpty(instances)) { log.warn("No instance available {}", serviceId); return new EmptyResponse(); } DefaultRequestContext requestContext = (DefaultRequestContext) request.getContext(); RequestData clientRequest = (RequestData) requestContext.getClientRequest(); HttpHeaders headers = clientRequest.getHeaders(); String reqVersion = headers.getFirst(CommonConstants.VERSION); if (StrUtil.isBlank(reqVersion)) { return super.choose(request).block(); } // 遍历可以实例元数据,若匹配则返回此实例 for (ServiceInstance instance : instances) { NacosServiceInstance nacosInstance = (NacosServiceInstance) instance; Map<String, String> metadata = nacosInstance.getMetadata(); String targetVersion = MapUtil.getStr(metadata, CommonConstants.VERSION); if (reqVersion.equalsIgnoreCase(targetVersion)) { log.debug("gray requst match success :{} {}", reqVersion, nacosInstance); return new DefaultResponse(nacosInstance); } } // 降级策略,使用轮询策略 return super.choose(request).block(); } } 针对客户端注入灰度负载均衡策略 @LoadBalancerClient(value = "demo-provider", configuration = GrayRoundLoadbalancerConfig.class) 服务实例定义版本号 请求携带版本号,测试使用 curl --location --request GET 'http://localhost:6060/req?key=b' \ --header 'VERSION: b' 优化负载均衡策略注入 如上文所述,所有的个性化负载策略都需要手动通过 LoadBalancerClient 注入非常的不方便。 我们可以参考 LoadBalancerClients 的批量注入逻辑构造自己的 BeanRegistrar public class GrayLoadBalancerClientConfigurationRegistrar implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { Field[] fields = ReflectUtil.getFields(ServiceNameConstants.class); // 遍历服务名称,注入支持灰度策略的负载均衡器 for (Field field : fields) { Object fieldValue = ReflectUtil.getFieldValue(ServiceNameConstants.class, field); registerClientConfiguration(registry, fieldValue, GrayLoadBalancerClientConfiguration.class); } } } >>> 源码 https://gitee.com/log4j/pig,欢迎署名转载 <<<

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册