# Turn the breaker
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 application.yml
application.properties and bootstrap.yml a second election on the line
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 service call hystrix-b in b () method fails to perform when
* @Date 2019/5/20 23:31
* @Version 1.0
*/
@Override
public String b() {
return "Hello!aUseB fail";
}
}
@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);
}
} Wuxi Women's Hospital http://www.bhnnk120.com/
Add @EnableEurekaClient comment indicates that this project can provide service to the registry
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