SpringCloud the fuse Hystrix

  Foreword

  SpringCloud micro-service leaders, the best floor plan.

  In the micro-services architecture will call each other between the multi-layer service, if there was one failure of the service, the service may result in one or more layers of service

  Failure, resulting in total system failure. This phenomenon is called avalanche effect service.

  SpringCloud in Hystrix components can solve such problems, Hystrix responsible for monitoring the situation calls between services, has repeatedly failed

  Situation fuse protection. Protection method is to use Fallback, when calling the service fails, you can use the Fallback method

  The return value; Hystrix interval checks failed service again, if the failure to restore the service, will continue to use the service.

  Source

  GitHub address: https: //github.com/intomylife/SpringCloud

  surroundings

  JDK 1.8.0 +

  Maven 3.0 +

  SpringBoot 2.0.3

  SpringCloud Finchley.RELEASE

  development tools

  IntelliJ IDEA

  text

  commons project

  commons project - POM file

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4.0.0

  com.zwc

  springcloud-hystrix-commons

  1.0

  springcloud-hystrix-commons

  Public works

  jar

  UTF-8

  1.8

  Cairo-SR3

  Finchley.RELEASE

  io.spring.platform

  platform-bom

  ${platform-bom.version}

  pom

  import

  org.springframework.cloud

  spring-cloud-dependencies

  ${spring-cloud-dependencies.version}

  pom

  import

  org.springframework.boot

  spring-boot-maven-plugin

  Some common configuration dependent

  commons project - Project Structure

  service project

  ① There are four modules in this project: a registry and services A, B, C

  ② A provision of services and service calls B, B calls the service to provide services and C and C to provide services

  registry-server (registry)

  registry-server - POM file

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4.0.0

  com.zwc

  springcloud-hystrix-service

  1.0

  com.zwc

  springcloud-hystrix-registry-service

  1.0

  springcloud-hystrix-registry-service

  Registry

  jar

  com.zwc

  springcloud-hystrix-commons

  1.0

  org.springframework.cloud

  spring-cloud-starter-netflix-eureka-server

  org.springframework.boot

  spring-boot-maven-plugin

  Mainly added spring-cloud-starter-netflix-eureka-server-dependent

  registry-server - application.yml profile

  # Port

  server:

  port: 8761

  # Application Name

  spring:

  application:

  name: eurka-server

  eureka:

  instance:

  # Ip instead of using the instance name

  prefer-ip-address: true

  Examples of the hostname #

  hostname: ${spring.cloud.client.ip-address}

  Rule # instance ID

  instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}

  client:

  # Whether to register themselves to the registration center

  registerWithEureka: false

  # Whether to obtain registration information to the registry

  fetchRegistry: false

  ServiceUrl:

  # Registration Center Address

  defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

  This uses the default port of 8761, of course, can change, but the discovery of registered address of the calling service center side of the port to be consistent with it

  registry-server - start class

  package com.zwc;

  import org.springframework.boot.SpringApplication;

  import org.springframework.boot.autoconfigure.SpringBootApplication;

  import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

  @SpringBootApplication

  @EnableEurekaServer

  public class SpringcloudHystrixRegistryServiceApplication {

  public static void main(String[] args) {

  SpringApplication.run(SpringcloudHystrixRegistryServiceApplication.class, args);

  }

  }

  Add @EnableEurekaServer annotation startup class indicates that this project is a registered center

  registry-server - to start the project

  Visit http 1. Start Project Success: // localhost: 8761 / to see the eureka-server main page

  Service Engineering A (providers and consumers)

  Service Engineering A - POM file

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4.0.0

  com.zwc

  springcloud-hystrix-a-service

  1.0

  com.zwc

  springcloud-hystrix-a-service-core

  1.0

  springcloud-hystrix-a-service-core

  Service Engineering - A Core

  jar

  com.zwc

  springcloud-hystrix-commons

  1.0

  com.zwc

  springcloud-hystrix-a-service-api

  1.0

  org.springframework.cloud

  spring-cloud-starter-netflix-eureka-client

  org.springframework.cloud

  spring-cloud-starter-openfeign

  org.springframework.boot

  spring-boot-maven-plugin

  Add spring-cloud-starter-netflix-eureka-client-dependent

  Also you need to add Feign start dependent spring-cloud-starter-openfeign

  Service Engineering A - application.yml profile

  # Port

  server:

  port: 8090

  # Application Name

  spring:

  application:

  name: hystrix-a

  eureka:

  instance:

  # Ip instead of using the instance name

  prefer-ip-address: true

  Examples of the hostname #

  hostname: ${spring.cloud.client.ip-address}

  Rule # instance ID

  instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}

  client:

  ServiceUrl:

  # Registration Center Address

  defaultZone: http://${eureka.instance.hostname}:8761/eureka/

  Note here configure registry address port port above 8761 is configured registry project

  spring.application.name: application name, need to use is called the consumer, it can also be consumed at the same time consumption

  Here three engineering projects in addition to registration centers are similar to this configuration, the port is not the same place and the name of the application, not repeat them

  Service Engineering A - application.properties (Note)

  # Turn the breaker

  feign.hystrix.enabled=true

  To take the initiative to open the circuit breaker, will fuse when a service call fails

  Here there is a pit, the fuse in this configuration will not take effect written yml

  A and B projects have this configuration, the building project to provide services only in C, no consumer services. So do the engineering configuration C

  Service Engineering A - controller front controller (service)

  package com.zwc.a.controller;

  import org.springframework.beans.factory.annotation.Value;

  import org.springframework.web.bind.annotation.RequestMapping;

  import org.springframework.web.bind.annotation.RestController;

  /*

  * @ClassName ASayHelloController

  * @Desc TODO Say Hello

  * @Date 2019/5/20 23:24

  * @Version 1.0

  */

  @RestController

  public class ASayHelloController {

  /*

  * @ClassName ASayHelloController

  * @Desc TODO reads the configuration file port

  * @Date 2019/5/20 23:24

  * @Version 1.0

  */

  @Value("${server.port}")

  private String port;

  /*

  * @ClassName ASayHelloController

  * @Desc TODO Say Hello

  * @Date 2019/5/20 23:24

  * @Version 1.0

  */

  @RequestMapping("/a")

  public String a(){

  return "Hello!I'm a. port:" + port;

  }

  }

  Provide a service: Hello and output ports

  Service Engineering A - service call

  package com.zwc.a.api.feign;

  import com.zwc.a.api.impl.FeignApiFallBack;

  import org.springframework.cloud.openfeign.FeignClient;

  import org.springframework.web.bind.annotation.RequestMapping;

  /*

  * @ClassName FeignApi

  * @Desc TODO using Feign call b - Interface

  * @Date 2019/5/20 23:21

  * @Version 1.0

  */

  @FeignClient(value = "hystrix-b" , fallback = FeignApiFallBack.class)

  FeignApi public interface {

  /*

  * @ClassName FeignApi

  * @Desc TODO call b () by hystrix-b service name method

  * @Date 2019/5/20 23:21

  * @Version 1.0

  */

  @RequestMapping("/b")

  String b();

  }

  = "Hystrix-b" to specify which service calls by @FeignClient annotation value

  hystrix-b is a provider of spring.application.name: Application Name

  To specify the method invoked when blown by @FeignClient annotation fallback = FeignApiFallBack.class

  Such a method is FeignApiFallBack (FeignApi) implementation class, a corresponding implementation method is called when such a fuse

  b (): This method is a service provided in project B, defined here as the interface

  Note that the provider has to return the same value, the same method and the same parameter name

  Service Engineering A - Fallback (FeignApiFallBack)

  package com.zwc.a.api.impl;

  import com.zwc.a.api.feign.FeignApi;

  import org.springframework.stereotype.Component;

  /*

  * @ClassName FeignApi

  * @Desc TODO fallback

  * @Date 2019/5/20 23:21

  * @Version 1.0

  */

  @Component

  public class implements FeignApiFallBack FeignApi {

  /*

  * @ClassName FeignApiFallBack

  * @Desc TODO call hystrix-b Services b () method fails to perform when

  * @Date 2019/5/20 23:31

  * @Version 1.0

  */

  @Override

  public String b() {

  return "Hello!aUseB fail";

  }

  } Wuxi Women's Hospital http://mobile.xasgyy.net/

  @Component the use of such notes to Spring Management

  FeignApi method implements the interface, providing the corresponding fuse

  Service Engineering A - controller front-end controller (Consumer Services)

  package com.zwc.a.controller;

  import com.zwc.a.api.feign.FeignApi;

  import org.springframework.beans.factory.annotation.Autowired;

  import org.springframework.web.bind.annotation.RequestMapping;

  import org.springframework.web.bind.annotation.RestController;

  /*

  * @ClassName AUseBFeignController

  * @Desc TODO using Feign call b - Front Controller

  * @Date 2019/5/20 23:23

  * @Version 1.0

  */

  @RestController

  public class AUseBFeignController {

  @Autowired(required = false)

  private FeignApi feignApi;

  /*

  * @ClassName FeignController

  * @Desc TODO call b () by hystrix-b service name method

  * @Date 2019/5/20 23:13

  * @Version 1.0

  */

  @RequestMapping("/aUseB")

  public String aUseB(){

  return feignApi.b();

  }

  }

  Use @Autowired comment assembly Bean, Bean calls the service by this method in

  Such exposure to the external interface call is actually a service provider

  Service Engineering A - start class

  package com.zwc;

  import org.springframework.boot.SpringApplication;

  import org.springframework.boot.autoconfigure.SpringBootApplication;

  import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

  import org.springframework.cloud.openfeign.EnableFeignClients;

  @SpringBootApplication

  @EnableEurekaClient

  @EnableFeignClients

  public class SpringcloudHystrixAServiceCoreApplication {

  public static void main(String[] args) {

  SpringApplication.run(SpringcloudHystrixAServiceCoreApplication.class, args);

  }

  }

  Add @EnableEurekaClient comment indicates that this project can provide a service or service call

  Add @EnableFeignClients notes indicate on Feign function for remote calls

  Engineering Services A - to start the project

  1. After starting a successful project visit: http: // localhost: 8090 / a (call your service)

  2. outputting content: '! Hello I'm a port:. 8090'

  3. Refresh http: // localhost: 8761 / (registry) can see the services have been registered came

  4. Visit address: http: // localhost: 8090 / aUseB (call B engineering services)

  5. outputting content: '! Hello aUseB fail' (B project has not started at this time because, so the call fallback method)

  6. Start service project B, the project started again successful visit: http: // localhost: 8090 / aUseB (call B engineering services)

  7. Output content: '! Hello I'm b port:. 8091' (if it is not successful call, wait for a while and then try to refresh)

  8. At this time, as evidenced by the success of the fuse

  9. access to the address: http: // localhost: 8091 / b (call your service)

  10. outputting content: '! Hello I'm b port:. 8091'

  11. Refresh again http: // localhost: 8761 / (registry), found that B engineering services also registered came

  service project - Project Structure

  The multi-use project IntelliJ IDEA Open

  The project from GitHub downloaded to your local

  Open IntelliJ IDEA

  Click File -> Open

  Open your downloaded to the local project directory

  springcloud-hystrix -> springcloud-hystrix-service (select to open this project)

  After opening the service project

  Click again File -> Project Structrue

  Select Modules, click on the '+' sign

  Click Import Module

  Or open your downloaded to the local project directory

  springcloud-hystrix -> springcloud-hystrix-commons -> pom.xml

  Click OK

  Click Next, Finish

  Click Apply, OK

  We hope to be able to help you

  over


Guess you like

Origin blog.51cto.com/14335413/2416095