重写Fegion的默认配置和Fegion的日志
官网:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign-overriding-defaults
项目结构如图:
ConfigurationAuth类
package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.auth.BasicAuthRequestInterceptor;
/**
* 此类不要放在启动类能扫描到的包下
* @author 重写Feign的默认配置
*
*/
@Configuration
public class ConfigurationAuth {
//配置注册到Eureka所需要的账户和密码
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("user", "123456");
}
}
ConfigurationContract类
package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.Contract;
import feign.Logger;
/**
* 此类不要放在启动类能扫描到的包下
* @author 重写Feign的默认配置
*
*/
@Configuration
public class ConfigurationContract {
//Contract默认是Spring MVC的契约,就可以用Spring MVC的注解
@Bean
public Contract feignContract() {
return new feign.Contract.Default();
}
//配置日志级别
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
SpringCloudOrderApplication类
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
/**
* 声明式服务
*
*/
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients//表示为Fegin客户端
public class SpringCloudOrderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudOrderApplication.class, args);
}
}
OrderController类
package com.example.demo.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.example.demo.entity.User;
import com.example.demo.feign.FeignClientServiceName;
import com.example.demo.feign.UserFeignClient;
//声明式服务
@RestController
public class OrderController {
@Autowired
private UserFeignClient userFeignClient;
@Autowired
private FeignClientServiceName feignClientServiceName;
@GetMapping("/order/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
}
@GetMapping("/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable String serviceName) {
return this.feignClientServiceName.findServiceInfoFromEurekaByServiceName(serviceName);
}
}
User类
package com.example.demo.entity;
import java.math.BigDecimal;
public class User {
private Long id;
private String username;
private String name;
private Short age;
private BigDecimal balance;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return this.age;
}
public void setAge(Short age) {
this.age = age;
}
public BigDecimal getBalance() {
return this.balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
FeignClientServiceName接口
package com.example.demo.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.config.ConfigurationAuth;
@FeignClient(name = "aaaa", url = "http://localhost:8761/", configuration = ConfigurationAuth.class)
public interface FeignClientServiceName {
@RequestMapping(value = "/eureka/apps/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName);
}
UserFeignClient接口
package com.example.demo.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import com.example.config.ConfigurationContract;
import com.example.demo.entity.User;
import feign.Param;
import feign.RequestLine;
//重写Feign的默认配置
@FeignClient(name = "spring-cloud-user", configuration = ConfigurationContract.class) // 调用哪个服务的名称
public interface UserFeignClient {
@RequestLine("GET /user/{id}") //注意:GET后面有一个空格
public User findById(@Param("id") Long id);
}
application.yml配置
spring:
application:
name: spring-cloud-order_overriding_feign_defaults #微服务的名称
server:
port: 7907 #微服务端口号
eureka:
client:
healthcheck:
enabled: true # 开启健康检查
serviceUrl:
defaultZone: http://user:123456@localhost:8761/eureka #服务eureka的URL地址
instance:
prefer-ip-address: true
logging:
level:
com.example.demo.feign.UserFeignClient: DEBUG
# 解决第一次请求报超时异常的方案:
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000
# 或者:
# hystrix.command.default.execution.timeout.enabled: false
# 或者:
# feign.hystrix.enabled: false ## 索性禁用feign的hystrix支持
# 超时的issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/768
# 超时的解决方案: http://stackoverflow.com/questions/27375557/hystrix-command-fails-with-timed-out-and-no-fallback-available
# hystrix配置: https://github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.thread.timeoutInMilliseconds
pom.xml配置
4.0.0
com.example.demo
spring-cloud-order_overriding_feign_defaults
0.0.1-SNAPSHOT
war
spring-cloud-order_overriding_feign_defaults
spring-cloud-order_overriding_feign_defaults
org.springframework.boot
spring-boot-starter-parent
1.4.1.RELEASE
UTF-8
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-feign
org.springframework.boot
spring-boot-devtools
org.springframework.cloud
spring-cloud-dependencies
Camden.SR2
pom
import
org.springframework.boot
spring-boot-maven-plugin