前面我们集成了ribbon实现了客户端的负载均衡,这里我们要使用feign实现
在前一章节,我们使用Ribbon作为客户端负载均衡完成了订单服务和用户服务的通信,其实我们可以发现,当我们通过RestTemplate调用其它服务时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率低下,并且显得好傻。而Feign的服务调用方式对于程序员来说更为友好,它基于Ribbon进行了封装,把一些负责的url和参数处理细节屏蔽起来,我们只需要简单编写Fiegn的客户端接口就可以像调用本地service去调用远程微服务。
为了避免和前面的案例混淆,我这里又写了一个子模块pay模块
导入相关依赖:
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-starter-web
cn.wly
springcloud-user-common
1.0-SNAPSHOT
主配置类:
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients //自动开启feign服务
public class PayServerApplication {
public static void main(String[] args) {
SpringApplication.run(PayServerApplication.class);
}
}
yml:
eureka:
client:
serviceUrl: #注册中心地址
defaultZone: http://peer1:1010/eureka/,http://peer2:1011/eureka/,http://peer3:1012/eureka/
instance:
prefer-ip-address: true #使用ip地址注册
instance-id: pay-server:1040 #实列id地址
spring:
application:
name: pay-server #服务名
server:
port: 1040 #端口
Feign的客户端接口是用来调用微服务的,编写一个用来调用user服务的客户端接口,如下:
/**
* url路径要与user服务的路径一致,
* 服务名要一致 ,
* 参数要一致 ,
* 返回值类型要一致。
*/
//注解了@FeignClient的接口将会被扫描到然后交给Spring管理。
@FeignClient(value = "user-server") //这里指定要访问的服务名
public interface UserFeignClient {
@GetMapping("/user/{id}")
public User getUserById(@PathVariable("id") Long id);
}
创建payController类
@RestController
public class PayController {
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("/pay/{id}")
public User getUserById(@PathVariable("id") Long id){
return userFeignClient.getUserById(id);
}
}
启动服务注册成功:
浏览器多次访问:peer1:1040/pay/1
要使用Feign,我们除了导入依赖之外,需要主配置类通过@EnableFeignClients(value="")注解开启Feign,也可以通过value属性指定了Feign的扫描包。同时我们需要为Feign编写客户端接口,接口上需要注解@FeignClient标签。 当程序启动,注解了@FeignClient的接口将会被扫描到然后交给Spring管理。
当请求发起,会使用jdk的动态代理方式代理接口,生成相应的RequestTemplate,Feign会为每个方法生成一个RequestTemplate同时封装好http信息,如:url,请求参数等等
最终RequestTemplate生成request请求,交给Http客户端(UrlConnection ,HttpClient,OkHttp)。然后Http客户端会交给LoadBalancerClient,使用Ribbon的负载均衡发起调用。
原文链接:https://blog.csdn.net/u014494148/article/details/105038349
负载均衡配置
Feign已经集成了Ribbon,所以它的负载均衡配置基于Ribbon配置即可,这里使用xml简单配置负载均衡策略如下:
user-server:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
Feign的超时配置
如果在服务调用时出现了 “feign.RetryableException : Read timed out…”错误日志,说明Ribbon处理超时 ,我们可以配置Ribbon的超时时间:
ribbon:
ConnectTimeout: 3000
ReadTimeout: 6000
如果服务调用现“com.netflix.hystrix.exception.HystrixRuntimeException:… timed - out and no fallback available” 错误日志,是因为Hystrix超时,默认Feign集成了Hystrix,但是高版本是关闭了Hystrix,我们可以配置Hystrix超时时间:
feign:
hystrix:
enabled: true #开启熔断支持
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 6000 #hystrix超时时间
Feign开启日志调试
官方文档:https://cloud.spring.io/spring-cloud-openfeign/reference/html/#feign-logging
.配置Feign日志打印内容
有的时候我们需要看到Feign的调用过程中的参数及相应,我们可以对Feign的日志进行配置,Feign支持如下几种日志模式来决定日志记录内容多少:
创建Feign配置类
@Configuration
public class FeignConfiguration {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL; //打印Feign的所有日志
}
}
.配置日志打印级别
配置UserFeignClient的日志打印级别,上面的配置打印Feign的那些内容,下面这个是配置日志框架打印日志的级别,不修改可能打印不出来日志,DEBUG打印日志调试信息。
logging:
level:
cn.itsource.springboot.feignclient.UserFeignClient: debug
原文链接:https://blog.csdn.net/u014494148/article/details/105038349