一、负载均衡
实际上,在添加@LoadBalanced
注解之后,会启用拦截器对我们发起的服务调用请求进行拦截(注意这里是针对我们发起的请求进行拦截),叫做LoadBalancerInterceptor
,它实现ClientHttpRequestInterceptor
接口:
@FunctionalInterface
public interface ClientHttpRequestInterceptor {ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException;
}
主要是对intercept
方法的实现:
public ClientHttpResponse intercept(final HttpRequest request, final byte[] body, final ClientHttpRequestExecution execution) throws IOException {URI originalUri = request.getURI();String serviceName = originalUri.getHost();Assert.state(serviceName != null, "Request URI does not contain a valid hostname: " + originalUri);return (ClientHttpResponse)this.loadBalancer.execute(serviceName, this.requestFactory.createRequest(request, body, execution));
}
服务端会在发起请求时执行这些拦截器。
那么这个拦截器做了什么事情呢,首先我们要明确,我们给过来的请求地址,并不是一个有效的主机名称,而是服务名称,那么怎么才能得到真正需要访问的主机名称呢,肯定是得找Eureka获取的。
我们来看看loadBalancer.execute()做了什么,它的具体实现为BlockingLoadBalancerClient:
//从上面给进来了服务的名称和具体的请求实体
public <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException {String hint = this.getHint(serviceId);LoadBalancerRequestAdapter<T, DefaultRequestContext> lbRequest = new LoadBalancerRequestAdapter(request, new DefaultRequestContext(request, hint));Set<LoadBalancerLifecycle> supportedLifecycleProcessors = this.getSupportedLifecycleProcessors(serviceId);supportedLifecycleProcessors.forEach((lifecycle) -> {lifecycle.onStart(lbRequest);});//可以看到在这里会调用choose方法自动获取对应的服务实例信息ServiceInstance serviceInstance = this.choose(serviceId, lbRequest);if (serviceInstance == null) {supportedLifecycleProcessors.forEach((lifecycle) -> {lifecycle.onComplete(new CompletionContext(Status.DISCARD, lbRequest, new EmptyResponse()));});//没有发现任何此服务的实例就抛异常(之前的测试中可能已经遇到了)throw new IllegalStateException("No instances available for " + serviceId);} else {//成功获取到对应服务的实例,这时就可以发起HTTP请求获取信息了return this.execute(serviceId, serviceInstance, lbRequest);}
}
所以,实际上在进行负载均衡的时候,会向Eureka发起请求,选择一个可用的对应服务,然后会返回此服务的主机地址等信息:
二、自定义负载均衡
LoadBalancer默认提供了两种负载均衡策略:
RandomLoadBalancer - 随机分配策略
(默认) RoundRobinLoadBalancer - 轮询分配策略
现在我们希望修改默认的负载均衡策略,可以进行指定,比如我们现在希望用户服务采用随机分配策略,我们需要先创建随机分配策略的配置类(不用加@Configuration):
package com.example.config;import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;public class LoadBalancerConfig {//将官方提供的 RandomLoadBalancer 注册为Bean@Beanpublic ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory){String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);return new RandomLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);}
}
接着我们需要为对应的服务指定负载均衡策略,直接使用注解即可:
@Configuration
@LoadBalancerClient(value = "userservice", //指定为 userservice 服务,只要是调用此服务都会使用我们指定的策略configuration = LoadBalancerConfig.class) //指定我们刚刚定义好的配置类
public class BeanConfiguration {@Bean@LoadBalancedpublic RestTemplate restTemplate() {return new RestTemplate();}
}
三、OpenFeign实现负载均衡
官方文档:https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/
Feign和RestTemplate一样,也是HTTP客户端请求工具,但是它的使用方式更加便捷。首先是依赖:
borrow-service(pom.xml)
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
接着在启动类添加@EnableFeignClients
注解:
@SpringBootApplication
@EnableFeignClients
public class BorrowApplication {public static void main(String[] args) {SpringApplication.run(BorrowApplication.class, args);}
}
现在我们需要调用其他微服务提供的接口,我们直接创建一个对应服务的接口类即可:
@FeignClient("userservice") //声明为userservice服务的HTTP请求客户端
public interface UserClient {
}
接着我们直接创建所需类型的方法,比如我们之前的:
RestTemplate template = new RestTemplate();
User user = template.getForObject("http://userservice/user/"+uid, User.class);
现在可以直接写成这样:
UserClient
package com.example.service.client;import com.example.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;//声明为userservice服务的HTTP请求客户端
@FeignClient("userservice")
public interface UserClient {//路径保证和其他微服务提供的一致即可@RequestMapping("/user/getUserById/{uid}")User getUserById(@PathVariable("uid") Integer uid);
}
BookClient
package com.example.service.client;import com.example.entity.Book;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;@FeignClient("bookservice")
public interface BookClient {@GetMapping("/book/getBookById/{bid}")Book getBookById(@PathVariable("bid") Integer bid);
}
接着我们直接注入使用(有Mybatis那味了):
BorrowServiceImpl
@Service
public class BorrowServiceImpl implements BorrowService {@Resourceprivate BorrowMapper borrowMapper;@Resourceprivate UserClient userClient;@Resourceprivate BookClient bookClient;@Overridepublic UserBorrowDto getUserBorrowDtoByUid(Integer uid) {List<Borrow> borrow = borrowMapper.getBorrowByUid(uid);//这里通过调用getForObject来请求其他服务,并将结果自动进行封装//获取User信息User user = userClient.getUserById(uid);//获取每一本书的详情信息List<Book> bookList = borrow.stream().map(b ->bookClient.getBookById(b.getBid())).collect(Collectors.toList());return new UserBorrowDto(user,bookList);}
}
访问,可以看到结果依然是正确的: