SpringCloud之Ribbon+Rest调用负载均衡
来源:互联网 发布:2017淘宝男装店铺排行 编辑:程序博客网 时间:2024/06/05 17:19
Spring Cloud之Ribbon+Rest调用负载均衡
创建Maven工程greeting-rest
导入依赖
pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.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></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>Dalston.RC1</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><repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository></repositories>
application.yml
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/server: port: 8764spring: application: name: greeting-rest
Application
package com.matrix.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;// @EnableDiscoveryClient,向eureka注册自己作为一个服务的调用client// 之前的服务,EnableEurekaClient,代表的是向eureka注册自己,将自己作为一个服务@SpringBootApplication@EnableDiscoveryClientpublic class GreetingRestApplication { public static void main(String[] args) { SpringApplication.run(GreetingRestApplication.class, args); } // 在spring容器中注入一个bean,RestTemplate,作为rest服务接口调用的客户端 // @LoadBalanced标注,代表对服务多个实例调用时开启负载均衡 @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); }}
调用service-say-hello
package com.matrix.ribbon.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;// 写一个服务,注入RestTemplate服务调用客户端@Servicepublic class GreetingService { @Autowired private RestTemplate restTemplate; // 用SAY-HELLO-SERVICE这个服务名替代实际的ip地址 // ribbon负载在多个服务实例之间负载均衡,每次调用随机挑选一个实例,然后替换服务名 public String greeting(String name) { return restTemplate.getForObject("http://SERVICE-SAY-HELLO/sayHello?name=" + name, String.class); }}
controller
package com.matrix.ribbon.controller;import com.matrix.ribbon.service.GreetingService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class GreetingController { @Autowired private GreetingService greetingService; @RequestMapping(value = "/greeting") public String greeting(@RequestParam String name) { return greetingService.greeting(name); }}
多次访问http://localhost:8764/greeting?name=matrix,发现每次访问的端口都不一样,在多个服务实例之间负载均衡了
阅读全文
0 0
- SpringCloud之Ribbon+Rest调用负载均衡
- SpringCloud(三)Ribbon负载均衡
- SpringCloud---Ribbon客户端负载均衡
- SpringCloud的Ribbon负载均衡
- springcloud入门系列(2)-Feign、Ribbon实现Rest接口请求和负载均衡
- SpringCloud(四)Ribbon自定义负载均衡
- SpringCloud(五)Ribbon自定义负载均衡插件
- SpringCloud(四)Ribbon自定义负载均衡
- springcloud使用ribbon实现客户端负载均衡
- 3.消费者(REST+Ribbon实现负载均衡)
- SpringCloud-----1、Eureka实现服务治理(Ribbon客户端负载均衡调用服务)
- springcloud服务调用其它服务接口实例及ribbon均衡负载
- springCloud学习01之eureka服务发现-提供者-消费者ribbon/feign-负载均衡
- springcloud 中使用eureka/ribbon/Hystrix负载均衡及断路器
- 【SpringCloud】(六):Ribbon实现客户端负载均衡
- SpringCloud 笔记 (六)---- 客户端负载均衡Ribbon
- SpringCloud | Feign如何整合Ribbon进行负载均衡的?
- SpringCloud 之第二篇: 服务消费者(rest+ribbon)
- 微信支付生成二维码方法封装
- 获取时间日期函数整理(部分来源于网络)
- PAT 甲级 1132. Cut Integer (20)
- 初识大数据
- 使用boot2docker管理Docker
- SpringCloud之Ribbon+Rest调用负载均衡
- 对数组元素进行由大到小排列
- 事务方法里如何单独添加日志
- Python学习---13
- NOIP模拟(20171102)T3 回文子串
- Java对象流与序列化
- 数据结构
- C++ 11的继承构造函数
- 有了开发板和平台资源,AI语音技能开发没有想象那么难