yls
2020/5/5
<!--服务注册与发现中心 start-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--服务注册与发现中心 end-->
@EnableDiscoveryClient
spring:
application:
name: itoken-service-redis
server:
port: 8502
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
@RestController
public class DemoController {
@PostMapping(value = "/put")
public String put(String value){
return value+"ok";
}
}
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--熔断器 start 由于feign自带,所有不需要添加
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
熔断器 end-->
<!--服务消费者 feign start-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--服务消费者 feign end-->
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class ItokenWebadminApplication {
public static void main(String[] args) {
SpringApplication.run(ItokenWebadminApplication.class, args);
}
}
spring:
application:
name: itoken-web-admin
#开启熔断器
feign:
hystrix:
enabled: true
server:
port: 8601
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
//itoken-service-redis是服务提供者的服务名称
//fallback = RedisServiceFallBack.class是熔断操作类
@FeignClient(value = "itoken-service-redis",fallback = RedisServiceFallBack.class)
public interface DemoService {
@PostMapping(value = "/put")
public String put(@RequestParam(value = "value") String value);
}
@Component
public class RedisServiceFallBack implements DemoService {
@Override
public String put(String value) {
return FallBack.badGateWay();//统一管理熔断信息
}
}
/**
* 统一管理熔断信息
*/
public class FallBack {
public static String badGateWay(){
try {
return JsonUtil.objectToString(ResultUtil.error(502,"内部错误"));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
}
@RestController
public class UserController {
@Autowired
private DemoService demoService;
@RequestMapping(value = {"/index"})
public String index(){
String json = demoService.put("hello ");
System.out.println(json);
return json;
}
}
hello ok
,表示服务消费成功。原文:https://www.cnblogs.com/yloved/p/12833037.html