环境:
jdk1.8;spring boot2.0.3;spring cloud(Finchley.RELEASE版本);Maven3.3
摘要说明:
服务消费者:即调用注册中心中的注册服务暴露出的接口;同一服务提供者会启动多个,故服务消费者需要起到负载均衡的作用
Spring Cloud Ribbon:Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具。它是一个基于HTTP和TCP的客户端负载均衡器。它可以通过在客户端中配置ribbonServerList来设置服务端列表去轮询访问以达到均衡负载的作用。
步骤:
1.创建ribbon(ribbon的服务消费者)子项目
通过
SPRING INITIALIZR
工具选择Cloud Routing:Ribbon模块构建ribbon子项目引入依赖(pom.xnl):
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pers.cc</groupId>
<artifactId>springCloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ribbon</artifactId>
<name>ribbon</name>
<description>ribbon消费者</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
2.配置ribbon(ribbon的服务消费者)子项目
使用@EnableDiscoveryClient和@LoadBalanced注解开启ribbon的服务消费者配置;
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;
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}
配置application.properties
#配置服务及端口
spring.application.name=ribbon-consumer
server.port=3001
#注册到注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
#从eureka服务器注册表中获取注册信息的时间间隔(s),默认为30秒
eureka.client.registry-fetch-interval-seconds=5
3.开发服务提供者接口
在服务提供者eurekaDiscovery中开发部分接口给消费者调用
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
DiscoveryClient discoveryClient;
@GetMapping("/dc")
public String dc() {
String services = "Services: " + discoveryClient.getServices() + new Date().getTime();
System.out.println(services);
return services;
}
@GetMapping("/add")
public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
System.out.println(a + b);
return a + b;
}
@GetMapping("/stop")
public String stop() throws InterruptedException {
System.out.println("stop");
Thread.sleep(5000L);
return "stop";
}
}
4.开发ribbon调用提供者接口
在服务提供者eurekaDiscovery中开发部分接口给消费者调用
import org.springframework.beans.factory.annotation.Autowired;
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
public class TestCoontroller {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
return restTemplate.getForEntity("http://EUREKA-CLIENT/dc", String.class).getBody();
}
}
5.调用ribbon
先后启动:
服务注册中心(eurekaServer):eureka-server(1001)
服务提供者(eurekaDiscovery):eureka-client(2001)
服务提供者(eurekaDiscovery):eureka-client(2002)
服务消费者(ribbon):ribbon-consumer(3001)
多次输入http://localhost:3001/test;查看eurekaDiscovery两个服务提供者都会出现调用达到负载均衡的作用;
6.源码地址
github地址:https://github.com/cc6688211/springCloud.git