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

eager-load

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