import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
public interface MemberService {
@RequestMapping(value = "testMember",method = RequestMethod.GET)
public String testMember();
@RequestMapping(value = "test",method = RequestMethod.GET)
public String test();
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
public interface OrderService {
@RequestMapping(value = "testOrder",method = RequestMethod.GET)
public String testOrder();
}
注意:该类实现的接口是feign中的接口
@Component
public class MemberFallBack implements MemberFeign {
@Override
public String testMember() {
return "服务降级";
}
@Override
public String test(){
return "服务降级";
}
import com.bdqn.fallBack.MemberFallBack;
import com.bdqn.service.MemberService;
import org.springframework.cloud.openfeign.FeignClient;
@FeignClient(name = "member",fallback = MemberFallBack.class)
public interface MemberFeign extends MemberService {
}
@RestController
public class OrderService {
@Autowired
private MemberFeign memberFeign;
@RequestMapping("test")
public String test(){
return memberFeign.testMember();
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class OrderStar {
public static void main(String[] args) {
SpringApplication.run(OrderStar.class,args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
application.yml配置文件
###服务启动端口号
server:
port: 8000
###服务名称(服务注册到eureka名称)
spring:
application:
name: order
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
###因为该应用为注册中心,不会注册自己
register-with-eureka: true
###是否需要从eureka上获取注册信息
fetch-registry: true
###开启Hystrix断路器
feign:
hystrix:
enabled: true
##### hystrix禁止服务超时时间
#hystrix:
# command:
# default:
# execution:
# timeout:
# enabled: false
#
import com.bdqn.service.MemberService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MemberServiceIpml implements MemberService {
@RequestMapping(value = "testMember",method = RequestMethod.GET)
public String testMember() {
try {
Thread.sleep(1500);
}catch (Exception e){}
return "会员接口";
}
@Override
public String test() {
try {
Thread.sleep(1500);
}catch (Exception e){}
return "会员接口";
}
}
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class MemberStar {
public static void main(String[] args) {
SpringApplication.run(MemberStar.class,args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
###服务启动端口号
server:
port: 8001
###服务名称(服务注册到eureka名称)
spring:
application:
name: member
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
###因为该应用为注册中心,不会注册自己
register-with-eureka: true
###是否需要从eureka上获取注册信息
fetch-registry: true
###开启Hystrix断路器
feign:
hystrix:
enabled: true