博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java springcloud B2B2C o2o多用户商城 springcloud架构-docker-feign配置(五)
阅读量:6343 次
发布时间:2019-06-22

本文共 7974 字,大约阅读时间需要 26 分钟。

hot3.png

简介

上一节我们讨论了怎么用feign声明式调用cloud的生产者,这节我们讨论一下feign配置,通过编写配置类,我们可以自定义feign的日志级别,日志扫描目录,可以通过feign调用服务在eureka上的调用信息。Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码请加企鹅求求:一零三八七七四六二六

feign声明接口之后,在代码中通过@Resource或者@Autowired注入之后即可使用。

@FeignClient标签的常用属性如下:

  • name:指定FeignClient的名称,如果项目使用了Ribbon,name属性会作为微服务的名称,用于服务发现
  • url: url一般用于调试,可以手动指定@FeignClient调用的地址
  • decode404:当发生http 404错误时,如果该字段位true,会调用decoder进行解码,否则抛出FeignException
  • configuration: Feign配置类,可以自定义Feign的Encoder、Decoder、LogLevel、Contract
  • fallback: 定义容错的处理类,当调用远程接口失败或超时时,会调用对应接口的容错逻辑,fallback指定的类必须实现@FeignClient标记的接口
  • fallbackFactory: 工厂类,用于生成fallback类示例,通过这个属性我们可以实现每个接口通用的容错逻辑,减少重复的代码
  • path: 定义当前FeignClient的统一前缀

一、创建模块(microservice-consumer-movie-feign-customizing)

项目结构如下:

二、pom.xml

microservice-spring-cloud
com.jacky
1.0-SNAPSHOT
4.0.0
microservice-consumer-movie-feign-customizing
jar
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
com.spotify
docker-maven-plugin
build-image
install
build
http://192.168.6.130:5678
true
${docker.repostory}/${docker.image.prefix}/${project.artifactId}:${project.version}
java:openjdk-8-jdk-alpine
["java", "-jar", "/${project.build.finalName}.jar"]
/
${project.build.directory}
${project.build.finalName}.jar

三、配置文件

spring:  application:    name: microservice-consumer-movie-feign-customizingserver:  port: 7901eureka:  client:    healthcheck:      enabled: true    serviceUrl:      defaultZone: http://jacky:admin@peer1:8761/eureka/,http://jacky:admin@peer2:8762/eureka/,http://jacky:admin@peer3:8763/eureka/  instance:    prefer-ip-address: true#feign日志配置logging:  level:    com.jacky.cloud.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

四、实体类User.java

package com.jacky.cloud.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;  }}

五、feign客户端1(UserFeignClient1.java)

package com.jacky.cloud.feign;import org.springframework.cloud.netflix.feign.FeignClient;import com.jacky.cloud.entity.User;import com.jacky.config.Configuration1;import feign.Param;import feign.RequestLine;@FeignClient(name = "microservice-provider-user", configuration = Configuration1.class)public interface UserFeignClient1 {  @RequestLine("GET /simple/{id}")  public User findById(@Param("id") Long id);}

六、feign客户端2(UserFeignClient2.java)

package com.jacky.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.jacky.config.Configuration2;@FeignClient(name = "xxxx", url = "http://localhost:8761/", configuration = Configuration2.class)public interface UserFeignClient2 {  @RequestMapping(value = "/eureka/apps/{serviceName}")  public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName);}

七、feign配置类1(Configuration1.java)

package com.jacky.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import feign.Contract;import feign.Logger;@Configurationpublic class Configuration1 {  @Bean  public Contract feignContract() {    return new feign.Contract.Default();  }  @Bean  Logger.Level feignLoggerLevel() {    return Logger.Level.FULL;  //定义feign日志显示级别  }}

八、feign配置类2(Configuration2.java)

package com.jacky.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import feign.auth.BasicAuthRequestInterceptor;@Configurationpublic class Configuration2 {  @Bean  public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {    return new BasicAuthRequestInterceptor("jacky", "admin"); //Eureka添加了安全验证,则需要配置上面的用户名、密码  }}

九、MovieController.java

package com.jacky.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.jacky.cloud.entity.User;import com.jacky.cloud.feign.UserFeignClient2;import com.jacky.cloud.feign.UserFeignClient1;@RestControllerpublic class MovieController {  @Autowired  private UserFeignClient1 userFeignClient1;  @Autowired  private UserFeignClient2 userfeignClient2;  /**   * 调用生产者服务   * @param id   * @return     */  @GetMapping("/movie/{id}")  public User findById(@PathVariable Long id) {    return this.userFeignClient1.findById(id);  }  /**   * 获得服务在eureka信息   * @param serviceName   * @return     */  @GetMapping("/{serviceName}")  public String findServiceInfoFromEurekaByServiceName(@PathVariable String serviceName) {    return this.userfeignClient2.findServiceInfoFromEurekaByServiceName(serviceName);  }}

十、启动类ConsumerMovieFeignApplication.java

package com.jacky.cloud;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@EnableFeignClientspublic class ConsumerMovieFeignApplication {  public static void main(String[] args) {    SpringApplication.run(ConsumerMovieFeignApplication.class, args);  }}

Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码请加企鹅求求:一零三八七七四六二六 

转载于:https://my.oschina.net/u/4045192/blog/3004821

你可能感兴趣的文章
apache2.4.33伪静态配置入门教程(1)
查看>>
银行卡的三个磁道
查看>>
OpenSSL 提取 pfx 数字证书公钥与私钥
查看>>
Keepalived详解(四):通过vrrp_script实现对集群资源的监控【转】
查看>>
CollapsingToolbarLayoutDemo【可折叠式标题栏,顺便带有CardView卡片式布局】
查看>>
CentOS7.4安装配置mysql5.7 TAR免安装版
查看>>
解决IE二级链接无法打开故障
查看>>
Windows phone应用开发[16]-数据加密
查看>>
SQL Server 迁移数据到MySQL
查看>>
通用数据压缩算法简介
查看>>
The next Industry Standard in IT Monitoring, a python implementation Nagios like tool --- Shinken
查看>>
(笔记)找工作,该怎么进补
查看>>
div的显示和隐藏以及点击图标的更改
查看>>
(轉貼) Ubuntu將在ARM平台netbook上現身 (SOC) (News) (Linux) (Ubuntu)
查看>>
SQL注入测试工具:Pangolin(穿山甲)
查看>>
在html 的img属性里只显示图片的部分区域(矩形,给出开始点和结束点),其他部份不显示,也不要拉伸...
查看>>
程序员第二定律:量化管理在程序员身上永无可能
查看>>
ubuntu一些脚本的执行顺序
查看>>
类继承的结构
查看>>
Moving thoughts
查看>>