“使用 RestTemplate 结合 Ribbon 实现服务调用”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (建立内容为“https://www.bilibili.com/video/BV1AN411Z7mx?p=14”的新页面) |
Jihongchang(讨论 | 贡献) |
||
第1行: | 第1行: | ||
https://www.bilibili.com/video/BV1AN411Z7mx?p=14 | https://www.bilibili.com/video/BV1AN411Z7mx?p=14 | ||
+ | |||
+ | === MyConfig.java === | ||
+ | <syntaxhighlight lang="java"> | ||
+ | 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; | ||
+ | |||
+ | @Configuration | ||
+ | public class MyConfig { | ||
+ | |||
+ | // 如果希望使用 Ribbon 的负载均衡能力 | ||
+ | @Bean | ||
+ | @LoadBalanced | ||
+ | public RestTemplate restTemplate() { | ||
+ | return new RestTemplate(); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === ClientServiceImpl.java === | ||
+ | <syntaxhighlight lang="java"> | ||
+ | import io.github.jihch.service.ClientService; | ||
+ | import org.springframework.beans.factory.annotation.Autowired; | ||
+ | import org.springframework.cloud.client.ServiceInstance; | ||
+ | import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; | ||
+ | import org.springframework.stereotype.Service; | ||
+ | import org.springframework.web.client.RestTemplate; | ||
+ | |||
+ | @Service | ||
+ | public class ClientServiceImpl implements ClientService { | ||
+ | |||
+ | // Ribbon 的负载均衡工具。使用这个对象的时候可以使用 Ribbon 的功能 | ||
+ | @Autowired | ||
+ | private LoadBalancerClient loadBalancerClient; | ||
+ | |||
+ | @Autowired | ||
+ | private RestTemplate restTemplate; | ||
+ | |||
+ | |||
+ | @Override | ||
+ | public String client() { | ||
+ | String result = restTemplate.getForObject("http://myproject/demo", String.class); | ||
+ | return result; | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | </syntaxhighlight>stop all Application Service 实例,修改 DemoController.java<syntaxhighlight lang="java"> | ||
+ | import io.github.jihch.pojo.People; | ||
+ | import org.springframework.web.bind.annotation.PathVariable; | ||
+ | import org.springframework.web.bind.annotation.RequestBody; | ||
+ | import org.springframework.web.bind.annotation.RequestMapping; | ||
+ | import org.springframework.web.bind.annotation.RestController; | ||
+ | |||
+ | import java.util.ArrayList; | ||
+ | import java.util.List; | ||
+ | import java.util.Map; | ||
+ | |||
+ | @RestController | ||
+ | public class DemoController { | ||
+ | |||
+ | @RequestMapping("/demo") | ||
+ | public String demo() { | ||
+ | return "demo1"; | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight>启动,再修改 DemoController.java<syntaxhighlight lang="java"> | ||
+ | import io.github.jihch.pojo.People; | ||
+ | import org.springframework.web.bind.annotation.PathVariable; | ||
+ | import org.springframework.web.bind.annotation.RequestBody; | ||
+ | import org.springframework.web.bind.annotation.RequestMapping; | ||
+ | import org.springframework.web.bind.annotation.RestController; | ||
+ | |||
+ | import java.util.ArrayList; | ||
+ | import java.util.List; | ||
+ | import java.util.Map; | ||
+ | |||
+ | @RestController | ||
+ | public class DemoController { | ||
+ | |||
+ | @RequestMapping("/demo") | ||
+ | public String demo() { | ||
+ | return "demo2"; | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight>因为这次使用了应用 Ribbon 的 RestTemplate 对 Application Service 提供的接口进行调用,浏览器重复多次访问 Application Client 的 “/demo”URL,demo1 和 demo2 将交替出现, |
2023年3月27日 (一) 05:54的最新版本
https://www.bilibili.com/video/BV1AN411Z7mx?p=14
MyConfig.java
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;
@Configuration
public class MyConfig {
// 如果希望使用 Ribbon 的负载均衡能力
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
ClientServiceImpl.java
import io.github.jihch.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ClientServiceImpl implements ClientService {
// Ribbon 的负载均衡工具。使用这个对象的时候可以使用 Ribbon 的功能
@Autowired
private LoadBalancerClient loadBalancerClient;
@Autowired
private RestTemplate restTemplate;
@Override
public String client() {
String result = restTemplate.getForObject("http://myproject/demo", String.class);
return result;
}
}
stop all Application Service 实例,修改 DemoController.java
import io.github.jihch.pojo.People;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController
public class DemoController {
@RequestMapping("/demo")
public String demo() {
return "demo1";
}
}
启动,再修改 DemoController.java
import io.github.jihch.pojo.People;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController
public class DemoController {
@RequestMapping("/demo")
public String demo() {
return "demo2";
}
}
因为这次使用了应用 Ribbon 的 RestTemplate 对 Application Service 提供的接口进行调用,浏览器重复多次访问 Application Client 的 “/demo”URL,demo1 和 demo2 将交替出现,