概述
/** * 当然我们也可以单个禁用,只需要在自定义的配置类中加入。 * 因为:默认支持的是HystrixFeign.Builder.而通过配置之后,返回的是Feign.builder去掉了对Hystrix的支持 * @return */ @Bean @Scope("prototype") public Feign.Builder feignBuilder() { return Feign.builder(); }
创建工程
MovieController
com.itmuch.cloud.controller.MovieController
package com.itmuch.cloud.controller;
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;
import com.itmuch.cloud.entity.User;
import com.itmuch.cloud.feign.FeignClient2;
import com.itmuch.cloud.feign.UserFeignClient;
@RestController
public class MovieController {
@Autowired
private UserFeignClient userFeignClient;
@Autowired
private FeignClient2 feignClient2;
@GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
}
@GetMapping("/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable String serviceName) {
return this.feignClient2.findServiceInfoFromEurekaByServiceName(serviceName);
}
}
Configuration1
com.itmuch.config.Configuration1
package com.itmuch.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.Contract;
import feign.Logger;
@Configuration
public class Configuration1 {
@Bean
public Contract feignContract() {
return new feign.Contract.Default();
}
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
UserFeignClient
com.itmuch.cloud.feign.UserFeignClient
package com.itmuch.cloud.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import com.itmuch.cloud.entity.User;
import com.itmuch.config.Configuration1;
import feign.Param;
import feign.RequestLine;
@FeignClient(name = "microservice-provider-user", configuration = Configuration1.class, fallback = HystrixClientFallback.class)
public interface UserFeignClient {
@RequestLine("GET /simple/{id}")
public User findById(@Param("id") Long id);
}
HystrixClientFallback
com.itmuch.cloud.feign.HystrixClientFallback
package com.itmuch.cloud.feign;
import org.springframework.stereotype.Component;
import com.itmuch.cloud.entity.User;
@Component
public class HystrixClientFallback implements UserFeignClient {
@Override
public User findById(Long id) {
User user = new User();
user.setId(0L);
return user;
}
}
Configuration2
com.itmuch.config.Configuration2
package com.itmuch.config;
import feign.Contract;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import feign.Feign;
import feign.auth.BasicAuthRequestInterceptor;
@Configuration
public class Configuration2 {
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("user", "password123");
}
/**
* 当然我们也可以单个禁用,只需要在自定义的配置类中加入。
* 因为:默认支持的是HystrixFeign.Builder.而通过配置之后,返回的是Feign.builder去掉了对Hystrix的支持
* @return
*/
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder();
}
}
说明:
1)当然我们也可以单个禁用,只需要在自定义的配置类中加入。
2)因为:默认支持的是HystrixFeign.Builder.而通过配置之后,返回的是Feign.builder去掉了对Hystrix的支持
3)加上之后,回调函数就没啥用了,访问会直接报错;
4)不加的话,访问返回haha;
FeignClient2
com.itmuch.cloud.feign.FeignClient2
package com.itmuch.cloud.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.itmuch.config.Configuration2;
@FeignClient(name = "xxxx", url = "http://localhost:8761/", configuration = Configuration2.class, fallback = FeignClient2Fallback.class)
public interface FeignClient2 {
@RequestMapping(value = "/eureka/apps/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName);
}
FeignClient2Fallback
com.itmuch.cloud.feign.FeignClient2Fallback
package com.itmuch.cloud.feign;
import org.springframework.stereotype.Component;
@Component
public class FeignClient2Fallback implements FeignClient2 {
@Override
public String findServiceInfoFromEurekaByServiceName(String serviceName) {
return "haha";
}
}
测试
不加之前:
http://localhost:7901/microservice-provider-user
加了之后:
http://localhost:7901/microservice-provider-user
==============================
QQ群:143522604
群里有相关资源
欢迎和大家一起学习、交流、提升!
==============================