SpringCloud-服务的消费者(rest+ribbon)
在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的。Spring Cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign,先来说下ribbon+rest
Ribbon简介
Ribbon是一个负载均衡客户端,可以很好的控制http和tcp的一些行为。Feign默认集成了ribbon
实现负载均衡,我们可以通过服务器端和客户端做负载均衡。服务器端做负载均衡,可以使用Nginx。而客户端做负载均衡,就是客户端有一个组件,知道有哪些可用的微服务,实现一个负载均衡算法。
Ribbon工作流程主要分为两步:
第一:先选择Eureka Server,优先选择在同一个Zone且负载较少的Server
第二:再根据用户指定的策略,再从server取到的服务注册列表中选择一个地址。其中Ribbon提供了很多种策略,例如轮询round bin ,随机random,很具响应时间加权
Ribbon已经默认实现了这些配置bean
IClientConfig ribbonClientConfig: DefaultClientConfigImpl
IRule ribbonRule: ZoneAvoidanceRule
IPing ribbonPing: NoOpPing
ServerList ribbonServerList: ConfigurationBasedServerList
ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter
ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer
项目创建
新建一个Eureka Client项目
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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.SpringCloud</groupId>
<artifactId>service-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-ribbon</name>
<description>service-ribbon-description</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml如下
spring:
application:
name: service-ribbon
server:
port: 8764
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
项目启动类修改如下
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
通过@EurekaDiscoveryClient向服务中心注册;并且向程序的IOC注入一个bean:restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
新建测试service
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hiService(String name) {
return restTemplate.getForObject("http://eureka-client/hello?name=" + name, String.class);
}
}
新建测试controller
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping("/hi")
public String hi(@RequestParam String name) {
return helloService.hiService(name);
}
}
浏览器访问
http://localhost:8764/hi?name=test可看到以下效果