一、Ribbon简介
Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现。
在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。
二、搭建服务提供者service-hello
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mo-cloud</artifactId>
<groupId>com.mo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service-hello</artifactId>
<description>服务提供者</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot 安全监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- eureka服务提供者的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
server:
# springboot设置随机端口号
port: 0
eureka:
client:
service-url:
default-zone: http://127.0.0.1:8761/eureka/
instance:
# 重写服务的唯一Id,防止多实例对被注册成一个服务
instance-id: ${spring.application.name}:${random.int}
# 设置优先使用ip进行注册
prefer-ip-address: true
spring:
application:
name: service-hello
package com.mo.config;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
* spring监听器,监听容器中发布的事件
*
* @author x.pan
* @email px5215201314@163.com
* @date 2020/4/6 19:31
*/
@Component
public class ServerConfig implements ApplicationListener<WebServerInitializedEvent> {
private int port;
@Override
public void onApplicationEvent(WebServerInitializedEvent webServerInitializedEvent) {
/*
* 获取tomcat启动的真实端口号
*/
port = webServerInitializedEvent.getWebServer().getPort();
}
public int getPort() {
return this.port;
}
}
package com.mo;
import com.mo.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author x.pan
* @email px5215201314@163.com
* @date 2020/4/6 19:02
*/
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceHelloApplication {
@Autowired
private ServerConfig serverConfig;
public static void main(String[] args) {
SpringApplication.run(ServiceHelloApplication.class, args);
}
/**
* 对外提供的一个服务,
* 可以使用idea开启多实例
*
* @return
*/
@GetMapping("/hello")
public String hello() {
return "hello, " + serverConfig.getPort();
}
}
三、搭建服务消费者ribbon
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mo-cloud</artifactId>
<groupId>com.mo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ribbon</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<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>
</dependencies>
</project>
server:
port: 0
spring:
application:
name: ribbon
eureka:
client:
service-url:
default-zone: http://127.0.0.1:8761/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${random.int}
package com.mo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author x.pan
* @email px5215201314@163.com
* @date 2020/4/6 19:54
*/
@RestController
public class HelloService {
@Autowired
private RestTemplate restTemplate;
/**
* 调用SERVICE-HELLO提供的服务
* RestTemplate通过负载均衡会自动将服务名,解析为ip:port
*
* @return
*/
@GetMapping("/hello")
public String hello() {
return restTemplate.getForObject("http://SERVICE-HELLO/hello", String.class);
}
}
package com.mo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* Ribbon服务启动器
*
* @author x.pan
* @email px5215201314@163.com
* @date 2020/4/6 19:51
*/
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
/**
* 注入一个开启了负载均衡的bean:restTemplate
*
* @return
*/
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
效果:请求ribbon服务的hello接口,会交替显示service-hello两个服务执行结果