一烟零、代碼示例
說明:此處使用的SpringBoot版本為2.1.13.RELEASE瘪松,SpringCloud版本為Greenwich.SR5
此處不再貼server與client代碼。
1锨阿、maven依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
2宵睦、application.yml
增加配置
feign:
hystrix:
enabled: true
server:
port: 9005
spring:
application:
name: feign-hystrix
eureka:
instance:
hostname: localhost
prefer-ip-address: true
instance-id: feign-hystrix-9005
client:
service-url:
defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
feign:
hystrix:
enabled: true
#info信息
info:
app:
name: feign-hystrix-9005
company:
name: www.xxx.com
build:
artifactId: ${project.artifactId}
version: ${project.version}
3、啟動類
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class FeignHystrix9005Application {
public static void main(String[] args) {
SpringApplication.run(FeignHystrix9005Application.class,args);
}
}
4墅诡、其他java類
定義接口FeignService 壳嚎,增加@FeignClient,指明調用的服務末早,以及fallback
@FeignClient(value = "CLIENT",fallback = FeignServiceFallback.class)
public interface FeignService {
@GetMapping("/hello/{name}")
public String hello(@PathVariable("name") String name);
}
增加FeignService 接口的FeignServiceFallback,該類需注入到容器
package org.example.service;
import org.springframework.stereotype.Component;
@Component
public class FeignServiceFallback implements FeignService{
@Override
public String hello(String name) {
return "helloError,"+name;
}
}
Controller類
package org.example.controller;
import org.example.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FeignHystrixController {
@Autowired
private FeignService feignService;
@GetMapping("/hello/{name}")
public String hello(@PathVariable String name){
return feignService.hello(name);
}
}
2烟馅、測試驗證
先后啟動server、client和本服務荐吉,訪問http://localhost:7001/
再訪問http://localhost:9005/hello/zs
然后焙糟,停止client,再次訪問
表明代碼生效。
github:
https://github.com/panli1988/cloud01
https://github.com/panli1988/cloud02
參考:
https://blog.csdn.net/forezp/article/details/70148833
http://www.itmuch.com/spring-cloud/spring-cloud-index/
還有尚硅谷周陽老師的視頻