三、Spring Cloud 服务容错保护

 

目录

  1. 创建注册中心:EurekaCenter
  2. 创建服务提供者:ServerOne
  3. 将服务提供者负载 
  4. 创建服务消费者:ServerTwo
  5. 容错保护测试

 

1 创建注册中心:EurekaCenter

pom.xml文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- eureka server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
DemoApplication.java
package com.example.demo;

import ...
@SpringBootApplication @EnableEurekaServer
public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }

application.yml

server:
  port: 1313
spring:
  application:
    name: ServerCenter

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      default-zone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动服务 访问 http://localhost:1313/

 

注册中心搭建完毕;

 

2 创建服务提供者:ServerOne

pom.xml文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!--添加 boot-starter-web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- eureka server -->
        <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-config</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

DemoApplication.java
package com.example.demo;

import ...

@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

 

application.yml

server:
  port: 8080
spring:
  application:
    name: server-one
eureka:
  client:
    service-url:
      defaultZone: http://localhost:1313/eureka/

 

创建业务DemoController.java

package com.example.demo;

import ...

@RestController
public class DemoController {

    @GetMapping("hello")
    public String hello(@RequestParam("name")String name){
        System.out.println("Hi,"+name+",   Welcome !");
        return  "Hi,"+name+",   Welcome !";
    }
}

 

启动服务,继续访问 http://localhost:1313/

服务提供者已经注册完毕。

 

3 将服务提供者负载

使用java -jar 的方式多启动几个服务提供者

为了测试,起了4个服务,端口分别是:8081、8082、8083、8084

 

4 创建服务消费者:ServerTwo

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!--添加 boot-starter-web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- eureka server -->
        <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-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 DemoApplication.java

package com.example.demo;

import ...

@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }


}

 

application.yml

server:
  port: 8090
spring:
  application:
    name: server-two
eureka:
  client:
    service-url:
      defaultZone: http://localhost:1313/eureka/

业务处理DemoService.java

package com.example.demo;

import ...

/**
 * @author: DevanYan
 * @create: 2019-05-22 11:00
 */
@Service
public class DemoService {

    @Autowired
    RestTemplate restTemplate;

    public String hello(String name){
        String url = "http://SERVER-ONE/hello?name="+ name + (int)(Math.random()*10) ;
        ResponseEntity<String> str = restTemplate.getForEntity(url,String.class);
        return str.getBody();
    }


}

 

业务处理DemoController.java

package com.example.demo;

import ...

/**
 * @author: DevanYan
 * @create: 2019-05-21 16:39
 */
@RestController
public class DemoController {

    @Autowired
    DemoService demoService;

    @GetMapping("hello")
    public String hello(@RequestParam("name")String name){

        String ret = demoService.hello(name);
        return ret;
    }
}

 

访问 http://localhost:1313/,看是否多了SERVER-TWO:8090服务

访问 http://localhost:8090/hello?name=devan  

输出

 

 

5 容错保护测试与治理

多次访问 http://localhost:8090/hello?name=devan,会看到SERVER-OME的四个服务被轮询调用

 

假如某个服务出现了宕机(我们关掉某个服务)

我们再多次访问 http://localhost:8090/hello?name=devan

此时会出现这样的页面

看一下SERVER-TWO的控制台

 

 很明显这样的提现是不够好的,为了控制此类事情的发生,cloud提供了容错处理

 

我么继续修改SERVER-TWO服务

 

 pom.xml 文件添加

<dependency>
  <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>

 DemoApplication.java 添加 @EnableCircuitBreaker 注解

package com.example.demo;

import ...

@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }


}

 

修改DemoService.java  添加 @HystrixCommand注解,并配置错误回调

package com.example.demo;

import ...

/**
 * @author: DevanYan
 * @create: 2019-05-22 11:00
 */
@Service
public class DemoService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "helloError")
    public String hello(String name){
        String url = "http://SERVER-ONE/hello?name="+ name + (int)(Math.random()*10) ;
        ResponseEntity<String> str = restTemplate.getForEntity(url,String.class);
        return str.getBody();
    }

    public String helloError(String name){
        return " error ! name is " + name;
    }


}

 

重新启动 SERVER-TWO服务

多次访问  http://localhost:8090/hello?name=devan

会出现错误结果

 

posted @ 2019-05-22 13:55  Devan.Yan  阅读(289)  评论(0编辑  收藏  举报