提交 675fe133 authored 作者: 王健's avatar 王健

eager-load

上级 c81ef57f
package com.xcloud.galaxy.movieservice.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
......@@ -14,6 +15,7 @@ public class WebConfig {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
......
......@@ -34,11 +34,7 @@ public class MovieController {
@GetMapping(value = "/index")
public User index() {
List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
List<String> urls = instances.stream().map(instance -> instance.getUri().toString()).collect(Collectors.toList());
int anInt = ThreadLocalRandom.current().nextInt(urls.size());
String url = urls.get(anInt);
User user = restTemplate.getForObject(url + "/users/1", User.class);
User user = restTemplate.getForObject("http://user-service/users/1", User.class);
return user;
}
......
......@@ -6,4 +6,9 @@ spring:
discovery:
server-addr: 127.0.0.1:8848
application:
name: movie-service
\ No newline at end of file
name: movie-service
# 饥饿加载
ribbon:
eager-load:
enabled: true
clients: user-service
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论