标签:微软雅黑 one print new plugin black private tin 文件
Spring Cloud Ribbon 是一个基于 HTTP 和 TCP 的客户端负载均衡工具,基于 Netflix Ribbon 实现,通过Spring Cloud 的封装,可以让我们轻松的将面向服务的REST 模板请求自动转换为客户端负载均衡的服务调用。客户端负载均衡在系统架构中是一个非常重要的,并且是不得不去实施的内容,因为负载均衡是对系统的高可用、网络压力的缓解和处理能力扩容的重要手段,客户端负载均衡需要通过心跳去维护服务端清单的健康性,这个需要服务注册中心配合完成,在Spring Cloud 实现的服务治理框架中,默认会创建针对各个服务治理框架的 Ribbon 自动化整合配置,比如 Eureka 中的 org.springframework.cloud.netflix.ribbon.eureka.RibbonEurekaAutoConfiguration 。
通过 Spring Cloud 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">
????????<modelVersion>4.0.0</modelVersion>
? ?
????????<groupId>org.drsoft.consumer</groupId>
????????<artifactId>consumer-helloservice</artifactId>
????????<version>0.0.1-SNAPSHOT</version>
????????<packaging>jar</packaging>
? ?
????????<name>consumer-helloservice</name>
????????<description>Demo project for Spring Boot</description>
? ?
????????<parent>
????????????????<groupId>org.springframework.boot</groupId>
????????????????<artifactId>spring-boot-starter-parent</artifactId>
????????????????<version>1.5.6.RELEASE</version>
????????????????<relativePath/>
????????</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>Dalston.SR2</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.boot</groupId>
????????????????????????<artifactId>spring-boot-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>
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerHelloserviceApplication {
? ?
????????public static void main(String[] args) {
????????????????SpringApplication.run(ConsumerHelloserviceApplication.class, args);
????????}
? ?
????????@LoadBalanced
????????@Bean
????????public RestTemplate createRestTemplate() {
????????????????return new RestTemplate();
????????}
}
@RestController
public class ConsumerController {
????????@Autowired
????????private RestTemplate restTemplate;
???????? ?
????????@RequestMapping (value = "/ribbonconsumer", method = RequestMethod.GET)
????????public String ribbonConsumer() {
? ?
????????????????ResponseEntity<String> responseEntity = null;
????????????????try {
????????????????????????responseEntity =
????????????????????????????????????????restTemplate.getForEntity("http://ORG.DRSOFT.WEBSERVICE.HELLOSERVICE/hello/get",
String.class);
? ?
????????????????????????if (responseEntity.getStatusCode() == HttpStatus.OK) {
????????????????????????????????return responseEntity.getBody();
????????????????????????}
? ?
????????????????????????return "response status " + responseEntity.getStatusCodeValue();
????????????????} catch (UnsupportedEncodingException e) {
????????????????????????e.printStackTrace();
????????????????????????return "error";
????????????????}
????????}
}
server.port=8090
spring.application.name=consumer-helloservice
? ?
eureka.client.service-url.defaultZone=http://eurekaserver1:1111/eureka/,http://eurekaserver2:1112/eureka
? ?
? ?
? ?
笔记:Spring Cloud Ribbon 客户端负载均衡
标签:微软雅黑 one print new plugin black private tin 文件
原文地址:http://www.cnblogs.com/li3807/p/7469017.html