软件系统开发定制SpringCloud之Ribbon路由、负载和熔断

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

application.yml

server:  port: 8092spring:  application:    name: ribbon-haeureka:  client:    serviceUrl:      defaultZone: http://localhost:8088/eureka/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

软件系统开发定制注入实现请求转发

@Configurationpublic class RestTemplateConfig {    @Bean    @LoadBalanced    RestTemplate restTemplate() {        return new RestTemplate();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

软件系统开发定制对外提供接口

@RestControllerpublic 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);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
@Servicepublic class HaService {    @Autowired    private RestTemplate restTemplate;    public String callHi(String name) {        return restTemplate.getForObject("http://ribbon-hi/hello?name=" + name, String.class);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

application.yml

server:  port: 8093spring:  application:    name: ribbon-hieureka:  client:    serviceUrl:      defaultZone: http://localhost:8088/eureka/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
@RestControllerpublic 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);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
@Servicepublic class HiService {    @Autowired    private RestTemplate restTemplate;    public String callHa(String name) {        return restTemplate.getForObject("http://ribbon-ha/hello?name=" + name, String.class);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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>
  • 1
  • 2
  • 3
  • 4
  • 5

在启动类上加注解:@EnableHystrix 开启熔断降级功能

修改service

通过@HystrixCommand指定转发失败后,回调haError()方法

@Servicepublic 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 服务挂了!";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
@Servicepublic 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 服务挂了!";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

重新启动8092、8093
正常访问:http://localhost:8092/ha?name=yzm
关闭8093后再次访问

相关链接



网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发