SpringCloud之Ribbon
SpringCloud通过Ribbon实现负载均衡。
一、添加Maven依赖
<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.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency>
二、application.yml配置
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8765 spring: application: name: blog-ribbon-client
三、主类编写
package com.springcloud.blog; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @EnableHystrix @EnableCircuitBreaker public class BlogRibbonClientApplication { public static void main(String[] args) { SpringApplication.run(BlogRibbonClientApplication.class, args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
四、编写测试相关
1.编写测试业务接口类
package com.springcloud.blog.example; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class TestService { @Autowired RestTemplate restTemplate; public String getPhone(String phone) { return restTemplate.getForObject("http://BLOG-API/juhe/getPhoneInfo?phone=" + phone, String.class); } }
注意:
BLOG-API是服务实例的名字,是必须要在Eureka Server注册的,否则是无效的且请求会是404。
关于restTemplate分别实现RESTFUL相关,如POST、GET、DELETE、PUT等。
通过调用restTemplate.postForEntity(url,respResult)、restTemplate.getForEntity(url,respResult)、restTemplate.delete(url,respResult)、restTemplate.put(url,respResult)等,就能立马实现。
也可以使用全能的restTemplate.getForObject(url,respResult)。
其中的url一定要是eureka服务注册中心中存在的实例,同时实例后面相关的请求接口URL一定是要存在的,否则没有意义。
2.编写测试路由类
package com.springcloud.blog.example; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/test") public class TestController { @Autowired TestService testService; @GetMapping(value = "/getPhone") public String getPhone(String phone) { return testService.getPhone(phone); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
2019-06-08 VsCode插件与Node.js交互通信
2019-06-08 request:fail url not in domain list
2019-06-08 安卓进阶开发资料之分享
2019-06-08 算法图解之散列表
2018-06-08 shiro实战系列(五)之Authentication(身份验证)