1.服务ribbon-ha
application.yml
server:
port: 8092
spring:
application:
name: ribbon-ha
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8088/eureka/
注入RestTemplate实现请求转发
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
对外提供接口
@RestController
public class HaController {
@Autowired
private HaService haService;
@Value("${server.port}")
private String port;
@GetMapping("hello")
public String hello(@RequestParam String name) {
return "ha," + name + " ! " + "访问端口号:" + port;
}
@GetMapping("ha")
public String ha(@RequestParam String name) {
return haService.callHi(name);
}
}
@Service
public class HaService {
@Autowired
private RestTemplate restTemplate;
public String callHi(String name) {
return restTemplate.getForObject("http://ribbon-hi/hello?name=" + name, String.class);
}
}
2.服务ribbon-hi
application.yml
server:
port: 8093
spring:
application:
name: ribbon-hi
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8088/eureka/
@RestController
public class HiController {
@Autowired
private HiService hiService;
@Value("${server.port}")
private String port;
@GetMapping("hello")
public String hello(@RequestParam String name) {
return "hi," + name + " ! " + "访问端口号:" + port;
}
@GetMapping("hi")
public String hi(@RequestParam String name) {
return hiService.callHa(name);
}
}
@Service
public class HiService {
@Autowired
private RestTemplate restTemplate;
public String callHa(String name) {
return restTemplate.getForObject("http://ribbon-ha/hello?name=" + name, String.class);
}
}
3.路由转发
依次启动 eureka、ribbon-ha、ribbon-hi 访问 http://localhost:8092/hello?name=yzm http://localhost:8093/hello?name=yzm http://localhost:8092/ha?name=yzm http://localhost:8093/hi?name=yzm 看得出8092的ha请求被转发到8093去了 看得出8093的hi请求被转发到8092去了
4.负载均衡
再启动一个ribbon-hi:18093服务 一直刷新访问 http://localhost:8092/ha?name=yzm 8092的请求以轮询的方式,分发给8093和18093
5.熔断降级
把8093、18093服务都关闭了 访问:http://localhost:8092/ha?name=yzm 直接显示错误信息不够友好又或者需要记录日志信息之类的,这个时候就可以进行熔断处理。
需要添加Hystrix依赖
<!-- 熔断 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
在启动类上加注解:@EnableHystrix 开启熔断降级功能
修改service
通过@HystrixCommand指定转发失败后,回调haError()方法
@Service
public class HaService {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "haError")
public String callHi(String name) {
return restTemplate.getForObject("http://ribbon-hi/hello?name=" + name, String.class);
}
public String haError(String name) {
return "ha," + name + " , ribbon-hi 服务挂了!";
}
}
@Service
public class HiService {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "hiError")
public String callHa(String name) {
return restTemplate.getForObject("http://ribbon-ha/hello?name=" + name, String.class);
}
public String hiError(String name) {
return "hi," + name + " , ribbon-ha 服务挂了!";
}
}
重新启动8092、8093 正常访问:http://localhost:8092/ha?name=yzm 关闭8093后再次访问
相关链接
首页 上一篇:服务注册 下一篇:Feign
|