1、服务ribbon-ha
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.yzm</groupId>
<artifactId>springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>ribbon-ha</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ribbon-ha</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 熔断 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.yzm</groupId>
<artifactId>springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>ribbon-hi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ribbon-hi</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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后再次访问