前言
Github:https://github.com/yihonglei/thinking-in-springcloud
Eureka注册中心:eureka-server
服务提供者(订单服务):eureka-provider-order
ribbon消费(用户服务):eureka-consumer-ribbon
一 Ribbon概要
Ribbon是一个基于HTTP和TCP的客户端负载均衡器,Ribbon与Eureka结合使用时,
Ribbon会从Eureka注册中心获取服务端列表,通过某种轮询方式,达到负载均衡访问服务。
二 启动eureka-server(注册中心)
执行eureka-server项目EurekaServerApplication类的main方法。
三 启动eureka-provider-order(服务提供者)
1、注释掉eureka-provider-order配置文件application.properties中的instance-id配置,
使用默认配置,否则
eureka.instance.instance-id=eureka-provider-order-8001
2、打包eureka-provider-order项目,通过命令行启动两个服务。
java -jar eureka-provider-order-1.0-SNAPSHOT.jar --server.port=8081
java -jar eureka-provider-order-1.0-SNAPSHOT.jar --server.port=8082
启动后查看eureka注册中心,会发现注册了eureka-provider-order的8081,8082端口服务。
四 启动eureka-consumer-ribbon(服务消费者)
1、eureka-consumer-ribbon项目结构
2、pom.xml依赖
引入Ribbon模块依赖。
<!-- Eureka Client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Ribbon负载均衡依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
3、application.properties
配置应用名称,服务端口,注册中心。
# 注册到eureka服务端的微服务名称
spring.application.name=eureka-consumer-ribbon
# 服务提供端口
server.port=8004
# 注册到eureka服务端的地址
eureka.client.service-url.defaultZone=http://localhost:9000/eureka/
# 显示指定微服务的名称,默认ip:应用名称:端口(192.168.1.7:eureka-consumer-ribbon:8004)
eureka.instance.instance-id=eureka-consumer-ribbon-8004
eureka.instance.prefer-ip-address=true
4、MainConfig
创建RestTemplate应用实例,通过@LoadBalanced注解开启客户端负载均衡。
package com.jpeony.ribbon.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* RestTemplate创建
*
* @author yihonglei
*/
@Configuration
public class MainConfig {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
5、UserController控制类
通过RestTemplate发起服务访问,而不是具体的地址。
package com.jpeony.ribbon.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* -- @RestController这个注解等价于spring mvc用法中的@Controller+@ResponseBody
*
* @author yihonglei
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/queryUserInfo", method = {RequestMethod.GET, RequestMethod.POST})
public String queryUserInfo() {
ResponseEntity<String> responseEntity =
restTemplate.getForEntity("http://EUREKA-PROVIDER-ORDER/order/queryOrderInfo", String.class);
return responseEntity.getBody();
}
}
6、ConsumerRibbonApplication应用启动
通过注解@EnableDiscoveryClient让应用注册到Eureka客户端,具有获取服务发现的能力。
package com.jpeony.ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* -- @SpringBootApplication 启动一个Spring Boot应用程序
* -- @EnableDiscoveryClient 服务发现与注册,当应用启动时,将应用注册到配置的注册中心
*
* @author yihonglei
*/
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerRibbonApplication.class, args);
}
}
7、运行RibbonConsumerApplication类启动服务
注册中心多了eureka-consumer-ribbon服务。
五 访问eureka-consumer-ribbon服务,通过ribbon负载,
消费eureka-provider-order服务
http://localhost:8004/user/queryUserInfo
Ribbon从服务端获取eureka-consumer-ribbon服务列表,包含各个实例的位置,
然后Ribbon按照列表信息进行轮询访问,实现基于客户端的负载均衡。
关于eureka-consumer-ribbon访问时控制台打印的关于客户端负载均衡日志。
2019-06-22 17:02:08.118 INFO 90932 --- [nio-8004-exec-1] c.n.l.DynamicServerListLoadBalancer:
DynamicServerListLoadBalancer for client EUREKA-PROVIDER-ORDER initialized:
-- 服务列表
DynamicServerListLoadBalancer:{NFLoadBalancer:name=EUREKA-PROVIDER-ORDER,
current list of Servers=[192.168.102.186:8081, 192.168.102.186:8082],
-- 负载均衡
Load balancer stats=Zone stats:
{
defaultzone=[
Zone:defaultzone; Instance count:2;
Active connections count: 0;
Circuit breaker tripped count: 0;
Active connections per server: 0.0;
]
},
-- 服务状态
Server stats: [
[
Server:192.168.102.186:8082;
Zone:defaultZone;
Total Requests:0;
Successive connection failure:0;
Total blackout seconds:0;
Last connection made:Thu Jan 01 08:00:00 CST 1970;
First connection made: Thu Jan 01 08:00:00 CST 1970;
Active Connections:0;
total failure count in last (1000) msecs:0;
average resp time:0.0;
90 percentile resp time:0.0;
95 percentile resp time:0.0;
min resp time:0.0;
max resp time:0.0;
stddev resp time:0.0
],
[
Server:192.168.102.186:8081;
Zone:defaultZone;
Total Requests:0;
Successive connection failure:0;
Total blackout seconds:0;
Last connection made:Thu Jan 01 08:00:00 CST 1970;
First connection made: Thu Jan 01 08:00:00 CST 1970;
Active Connections:0;
total failure count in last (1000) msecs:0;
average resp time:0.0;
90 percentile resp time:0.0;
95 percentile resp time:0.0;
min resp time:0.0;
max resp time:0.0;
stddev resp time:0.0
]
]}
ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@7abc34b6
2019-06-22 17:02:09.106 INFO 90932 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty:
Flipping property:
EUREKA-PROVIDER-ORDER.ribbon.ActiveConnectionsLimit to use NEXT property:
niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
从日志中,可以看到Ribbon访问时客户端打印的服务列表,服务状态等等日志信息。