OpenFeign 基础篇:什么是OpenFeign,什么是Feign、OpenFeign的示例代码

什么是OpenFeign

OpenFeign是Spring Cloud提供的一个声明式的伪Http客户端, 它使得调用远程服务就像调用本地服务一样
简单, 只需要创建一个接口并添加一个注解即可。
Nacos很好的兼容了OpenFeign, OpenFeign默认集成了 Ribbon, 所以在Nacos下使用OpenFegin默认就实现了负载均
衡的效果。

Feign与OpenFeign的区别

Feign是Spring Cloud组件中的一个轻量级RESTful的HTTP服务客户端。Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务

OpenFeign是Spring Cloud 在Feign的基础上支持了SpringMVC的注解,如@RequesMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。

Feign使用

依赖


	org.springframework.cloud
	spring-cloud-starter-openfeign

启动注解

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients//开启Fegin

代码

常量配置

public interface RequestMappingConst {
    String SAMPLES_INTERFACE = "/samples/interface";
    String SAMPLES_INTERFACE_TWO = "/samples/interface/two";
    String SAMPLES_INTERFACE_SEARCH_LOGS = "/samples/interface/search/logs";
}

feign接口

@FeignClient(value = ServiceConst.SAMPLES_NACOS_PROVIDER, fallbackFactory = SamplesFallbackFactory.class)
public interface SamplesInterface {

    @RequestMapping(RequestMappingConst.SAMPLES_INTERFACE)
    SamplesModel samplesInterface(@RequestParam("name") String name);

    @RequestMapping(RequestMappingConst.SAMPLES_INTERFACE_TWO)
    SamplesModel samplesInterfaceTwo(@RequestParam("name") String name);

    @RequestMapping(RequestMappingConst.SAMPLES_INTERFACE_SEARCH_LOGS)
    SamplesModel samplesInterfaceSearchLogs();
}

熔断配置

@Component
public class SamplesFallbackFactory implements FallbackFactory {
    @Override
    public Object create(Throwable cause) {
        return new SamplesInterface() {
            @Override
            public SamplesModel samplesInterface(String name) {
                return new SamplesModel("当前服务不可用");
            }

            @Override
            public SamplesModel samplesInterfaceTwo(String name) {
                return new SamplesModel("当前服务不可用");
            }

            @Override
            public SamplesModel samplesInterfaceSearchLogs() {
                return null;
            }
        };
    }
}

控制层

@RestController
@RequestMapping("/samples")
public class SamplesController {

    @Autowired
    private SamplesInterface samplesInterface;

    /**
     * 对外接口URL需要与当前Controller URL一致
     *
     * @param name
     * @return
     */
    @HystrixCommand(fallbackMethod = "samplesInterfaceTimeOutHandler", commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")})
    @RequestMapping("/interface")
    public SamplesModel samplesInterface(String name) {
        return samplesInterface.samplesInterface(name);
    }

    /**
     * @param name
     * @return
     * @HystrixCommand Debug 或sleep 测试熔断器
     */
    @HystrixCommand(fallbackMethod = "samplesInterfaceTimeOutHandler", commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")})
    @RequestMapping("/interface/two")
    public SamplesModel samplesInterfaceTwo(String name) {
        return samplesInterface.samplesInterfaceTwo(name);
    }

    private SamplesModel samplesInterfaceTimeOutHandler(String name) {
        return new SamplesModel("赵六");
    }
}

你可能感兴趣的:(分布式理论&中间件搭建,前沿框架,java,spring,cloud,spring,boot)