代码地址:github-spring-cloud地址
前言:在上一篇文章,讲了服务的注册和发现。在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http
restful的。Spring
cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。在这一篇文章首先讲解下ribbon+rest。
一、ribbon介绍
官方文档简介
Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.
Ribbon是客户端负载平衡器,它使您可以对HTTP和TCP客户端的行为进行大量控制。
Feign已经使用了Ribbon,因此如果您使用@FeignClient,则本节也适用。
ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。Feign默认集成了ribbon。
二、启动两个实例
这篇文章基于上一篇文章启动eureka-server 工程;启动service-hello工程,它的端口为8081;将service-hello的配置文件的端口改为8083,并启动:service-hello在eureka-server注册了2个实例,这就相当于一个小的集群。
三、建一个服务消费者(ribbon)
新建一个工程server-ribbon,引入pom文件
<?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>spring-cloud-learn</artifactId>
<groupId>com.sl.learn.cloud</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.sl.learn.cloud</groupId>
<artifactId>server-ribbon</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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-ribbon</artifactId>
</dependency>
</dependencies>
</project>
修改配置文件
server:
port: 8082
spring:
application:
name: server-ribbon
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
shutdown:
enabled: true
在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
package com.sl.cloud.learn;
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;
/**
* @author shuliangzhao
* @Title: ServerRibbonApplication
* @ProjectName spring-cloud-learn
* @Description: TODO
* @date 2019/11/24 15:59
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ServerRibbonApplication {
public static void main(String[] args) {
SpringApplication.run( ServerRibbonApplication.class, args );
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {`在这里插入代码片`
return new RestTemplate();
}
}
写一个测试类HelloService
@Service
public class HelloService {
@Autowired
private RestTemplate restTemplate;
public String helloService(String name) {
return restTemplate.getForObject("http://EUREKA-HELLO/hi?name="+name,String.class);
}
}
写一个controller调用类
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping(value = "/hi")
public String helloService(String name) {
return helloService.helloService(name);
}
}
在浏览器上多次访问http://localhost:8082/hi?name=zhansan,浏览器交替显示
hi zhansan ,my port is:8083
hi zhansan ,my port is:8081
四、 Ribbon调用总结
服务注册中心,eureka server,端口为8080
eureka-hello工程跑了两个实例,端口分别为8081,8983,分别向服务注册中心注册
sercvice-ribbon端口为8082,向服务注册中心注册
当sercvice-ribbon通过restTemplate调用eureka-hello的hi接口时,因为用ribbon进行了负载均衡,会轮流的调用eureka-hello:8081,8983 两个端口的hi接口;