Spring Cloud Eureka 

服务架构图

1.Hello-Service服务端配置

在pom文件中添加Eureka客户端依赖,并配置Eureka注册中心的服务地址.也可以不配置,那就会使用默认的localhost的8761端口.

pom.xml

<!-- eureka 客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

application.yml

spring:
application:
name: hello-service
server:
port: 8080 eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/

启动两个service

  • mvn package 打包
  • java -jar jar包 --server.port=端口 启动并指定端口

使用bat脚本可以快速启动服务,

start java -jar hello-service-0.0.1-SNAPSHOT.jar --server.port=8081
start java -jar hello-service-0.0.1-SNAPSHOT.jar --server.port=8082

启动 8081 8082 两个端口提供服务

 

2.Ribbon客户端配置

pom.xml

<!-- ribbon 客户端 负载均衡 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

application.yml

spring:
application:
name: ribbon-client server:
port: 8085 hello:
serviceUrl: http://localhost:8081/hello/

http://localhost:8081/hello 是hello-service的服务请求地址,用于非负载均衡的情况下,Bean的配置不需要加@LoadBalanced注解;

在Ribbon中可以直接调用HELLO-SERVICE服务注册的名字使用服务,已达到负载均衡的目的.

Application.java

@SpringBootApplication
public class RibbonClientApplication { @LoadBalanced
@Bean
RestTemplate restTemplate(){
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(RibbonClientApplication.class, args);
} }

@LoadBalanced 客户端负载均衡模板

Controller.java

@RestController
@RequestMapping("con")
public class ConsumerController { @Autowired
private RestTemplate template; @Value("${hello.serviceUrl}")
private String helloServiceUrl; /**
* 非负载均衡请求,不需要@LoadBalance
* @return
*/
@GetMapping("hello01")
public String hello01(){
return template.getForObject(helloServiceUrl, String.class);
} /**
* 负载均衡请求 HELLO-SERVICE 服务在Eureka上注册的名字
* @return
*/
@GetMapping("hello02")
public String hello02(){
return template.getForObject("http://HELLO-SERVICE/hello", String.class);
} }

3.启动服务并验证

依次启动Eureka注册中心,2个Hello-Service和Ribbon-Client

 

访问: http://localhost:8085/con/hello02

Hello World!

验证负载均衡和高可用,可以直接访问服务,然后关闭其中一个服务再访问

第一次访问: http://localhost:8085/con/hello02

Hello World!

可以直接访问到服务.然后关闭其中一个服务再访问

 

访问: http://localhost:8085/con/hello02 可能会看到如下报错

{"timestamp":"2018-12-21T04:01:02.288+0000","status":500,"error":"Internal Server Error","message":"I/O error on GET request for \"http://HELLO-SERVICE/hello\": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect","path":"/con/hello02"}

继续访问,就会正常被请求到了

访问:  http://localhost:8090/con/hello02
Hello World!
访问: http://localhost:8090/con/hello02
Hello World!
访问: http://localhost:8090/con/hello02
Hello World!

默认服务采用轮询方式提供服务,每两次会有一次失败.多访问几次后,就只有运行的服务提供服务.

Spring Cloud 2-Ribbon 客户端负载均衡(二)的更多相关文章

  1. Spring Cloud - 切换Ribbon的负载均衡模式

    Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现.通过Spring Cloud的封装,可以让我们轻松地将面向服务的REST模 ...

  2. Spring Cloud Gateway Ribbon 自定义负载均衡

    在微服务开发中,使用Spring Cloud Gateway做为服务的网关,网关后面启动N个业务服务.但是有这样一个需求,同一个用户的操作,有时候需要保证顺序性,如果使用默认负载均衡策略,同一个用户的 ...

  3. spring cloud --- Ribbon 客户端负载均衡 + RestTemplate + Hystrix 熔断器 [服务保护] ---心得

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 当超大并发量并发访问一个服务接口时,服务器会崩溃 ,不仅导致这个接口无法 ...

  4. spring cloud --- Ribbon 客户端负载均衡 + RestTemplate ---心得【无熔断器】

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 了解了 eureka 服务注册与发现 的3大角色 ,会使用RestTem ...

  5. springcloud(十二):Ribbon客户端负载均衡介绍

    springcloud(十二):Ribbon客户端负载均衡介绍 Ribbon简介 使用分布式微服务脚骨的应用系统,在部署的时候通常会为部分或者全部微服务搭建集群环境,通过提供多个实例来提高系统的稳定型 ...

  6. SpringBoot(三) - Ribbon客户端负载均衡,Zuul网关,Config配置中心

    1.Ribbon客户端负载均衡 1.1 依赖 1.2 配置信息 # feign默认加载了ribbon负载均衡,默认负载均衡机制是:轮询 # 负载均衡机制是添加在消费端(客户端)的,如果改为随机,指定服 ...

  7. Spring Cloud Ribbon——客户端负载均衡

    一.负载均衡负载均衡(Load Balance): 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽.增加吞吐量.加强网络数据处理能力.提高网络的灵活性和可用性.其意思 ...

  8. spring cloud 系列第3篇 —— ribbon 客户端负载均衡 (F版本)

    源码仓库地址:https://github.com/heibaiying/spring-samples-for-all 一.ribbon 简介 ribbon是Netfix公司开源的负载均衡组件,采用服 ...

  9. 笔记:Spring Cloud Ribbon 客户端负载均衡

    Spring Cloud Ribbon 是一个基于 HTTP 和 TCP 的客户端负载均衡工具,基于 Netflix Ribbon 实现,通过Spring Cloud 的封装,可以让我们轻松的将面向服 ...

随机推荐

  1. python第九章:面向对象--小白博客

     面向对象介绍 一.面向对象和面向过程 面向过程:核心过程二字,过程即解决问题的步骤,就是先干什么后干什么 基于该思想写程序就好比在这是一条流水线,是一种机械式的思维方式 优点:复杂的过程流程化 缺点 ...

  2. 控制结构(5): 必经之地(using)

    // 上一篇:局部化(localization) // 下一篇:最近最少使用(LRU) 基于语言提供的基本控制结构,更好地组织和表达程序,需要良好的控制结构. 前情回顾 上一周,我们谈到了分支/卫语句 ...

  3. Messenger更改系统语言以后无法登陆,提示“初始设置被修改”

    在安装messenger机器上使用SQL management studio打开数据库,链接YCD的数据库,找到dbo.Dic_Defaults的表,编辑打开以后找到“CultureInfo”两项,删 ...

  4. powershell 函数, foreach中格式化

    function testArg { $n = 1; if($args.Count -eq 0) { "No arg!" } else { $args | foreach {&qu ...

  5. RfcConfig 类 主要解决Tomcat 报 The valid characters are defined in RFC 7230 and RFC 3986

    tomcat 8.0以后对请求URL做了严格的过滤 就是严格按照 RFC 3986规范进行访问解析,而 RFC 3986规范定义了Url中只允许包含英文字母(a-zA-Z).数字(0-9).-_.~4 ...

  6. Python——hashilib 模块(哈希模块)

    hashilib 模块 摘要算法 import hashlib # 提供摘要算法的模块 md5 = hashlib.md5() md5.update(b'alex3714') print(md5.he ...

  7. Codeforces Round #382 (div2)

    A:题目:http://codeforces.com/contest/735/problem/A 题意:出发点G,终点T,每次只能走k步,#不能走,问能否到达终点 思路:暴力 #include < ...

  8. opencv 增强现实(二):特征点匹配

    import cv2 as cv import numpy as np # def draw_keypoints(img, keypoints): # for kp in keypoints: # x ...

  9. nginx的location配置root、alias用法和区别

    root & alias区别root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上root的处理结果是:root路径 ...

  10. python中xrange和range(转)

    说到序列,我们第一想到的是一组有序元素组成的集合.同时,每个元素都有唯一的下标作为索引. 在Python中,有许多内界的序列.包括元组tuple,列表list,字符串str等.上面提到的序列类型(li ...