Spring Cloud LoadBalancer的使用
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>vnacos</artifactId> <groupId>com.nacos</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>order-loadbalancer</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--nacos-服务注册发现--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <exclusions> <!--将ribbon排除--> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </exclusion> </exclusions> </dependency> <!--添加loadbalanncer依赖, 添加spring-cloud的依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> </dependencies> </project>
启动类:
package com.order; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; /** * @author vans on 2022/2/16 18:32 */ /** * 此配 scanBasePackages={"com.netflix.client.config.IClientConfig"} 解决: * Consider defining a bean of type 'com.netflix.client.config.IClientConfig' in your configuration. */ @SpringBootApplication(scanBasePackages={"com.netflix.client.config.IClientConfig","com.order"}) public class VnOrderApplication { public static void main(String[] args) { SpringApplication.run(VnOrderApplication.class,args); } /** * * @param restTemplateBuilder * @return * * @LoadBalanced 定义负载均衡器 * */ @Bean @LoadBalanced public RestTemplate getRestTemplate(RestTemplateBuilder restTemplateBuilder){ RestTemplate restTemplate = restTemplateBuilder.build(); return restTemplate; } }
Controller类:
package com.order.controller; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; /** * @author vans on 2022/2/16 18:13 */ @RestController @RequestMapping("/vn/order") @Slf4j public class OrderController { @Autowired private RestTemplate restTemplate; @ApiOperation(value = "获取所有用户列表", notes = "不需要任何参数") @ApiImplicitParam(name = "运单号", value = "mailNo", required = false, dataType = "String") @RequestMapping(value="/{mailNo}",method = RequestMethod.GET) public String getOrder(@PathVariable("mailNo") String mailNo) { String message = restTemplate.getForObject("http://stock-service:90/vn/stock", String.class); return message; } }