feign三:覆写feign的默认配置及feign的日志
默认配置复写
本项目地址:http://192.168.1.103:7601
本例是通过feign调用 eureka项目中的/eureka/apps/,获取相关项目的说明信息
http://localhost:8761/eureka/apps
1.首先需要在入口app处添加feign注解:@EnableFeignClients
@EnableEurekaClient @SpringBootApplication @EnableFeignClients public class FeignApp { public static void main(String[] args) { SpringApplication.run(FeignApp.class, args); } }
2.创建feign复写的配置:FooConfiguration2
@Configuration public class FooConfiguration2 { /** * 配置Url用户和密码,当eureka启用用户名和密码时 * @return */ /*@Bean public BasicAuthRequestInterceptor basicAuthRequestInterceptor() { return new BasicAuthRequestInterceptor("root", "root123"); }*/ //默认配置 @Bean public Contract getContract() { return new feign.Contract.Default(); } }
3.创建调用的client: UserFeignClient2
注意:@FeignClient 配置说明
当@FeignClient有name和url还有configuration时,取值为url的地址,name只是为一个名称而已(无意义)
当@FeignClient只有name和configuration时,name的取值为eureka中的application项目的名称即虚拟地址
/** * 当@FeignClient有name和url还有configuration时,取值为url的地址,name只是为一个名称而已(无意义) * 当@FeignClient只有name和configuration时,name的取值为eureka中的application项目的名称即虚拟地址 * @author Administrator * */ @FeignClient(name="boot-user", url="http://localhost:8761", configuration=FooConfiguration2.class) public interface UserFeignClient2 { @RequestLine("GET /eureka/apps/{serviceName}") public String findServiceInfoFromEurekaByServiceName(@Param("serviceName") String serviceName); //@RequestLine("GET /simple/{id}") //public User findById(@Param("id") Long id); }
5.controller调用client
@RestController public class UserController { @Autowired private UserFeignClient userFeignClient; @Autowired private UserFeignClient2 userFeignClient2; @GetMapping("/simple/{id}") public User findById(@PathVariable Long id) { return this.userFeignClient.findById(id); } //@GetMapping("/sip/{id}") //public User findByIdSip(@PathVariable Long id) //{ // return this.userFeignClient2.findById(id); //} @GetMapping("/eureka/apps/{serviceName}") public String findEurekaInfo(@PathVariable String serviceName) { return this.userFeignClient2.findServiceInfoFromEurekaByServiceName(serviceName); } }
调用:
http://192.168.1.103:7601/eureka/apps/SPRING-BOOT-USER
Feign日志添加:
1.在配置文件applicaton.propreties文件中添加配置
#loging.level.+Feign客户端路径 logging.level.com.muyang.bootmovie.feign.UserFeignClient2=debug
注意上面的内容:loggin的配置:
loging.level.+Feign客户端路径
com.muyang.bootmovie.feign.UserFeignClient2 ,client是我controller需要调用的
2.在复写的configureration加入logger的bean注解
//feign日志配置 @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; }
3.在实际访问:http://192.168.1.103:7601/eureka/apps/SPRING-BOOT-USER时
在访问http://192.168.1.103:7601/eureka/apps/SPRING-BOOT-USER时得出的xml/string信息会以日志的形式输出