本文主要介绍SpringCloud分布式微服务b2b2c电商docker-feign配置的实例分析,具有一定的参考价值,感兴趣的朋友可以参考一下。希望大家看完这篇文章后收获多多。让边肖带你去看看。
讨论feign配置。通过编写配置类,我们可以定制feign的日志级别、日志扫描目录,以及通过feign调用eureka上服务的信息。
feign声明接口后,可以在@Resource或@Autowired将其注入代码后使用。
@FeignClient标签的常见属性如下:
名称:指定FeignClient的名称。如果项目使用功能区,名称属性将用作微服务的名称。
Url: url一般用于调试,可以手动指定@ FeignClient调用的地址。
Decode404:当发生http 404错误时,如果该字段设置为true,将调用解码器进行解码,否则将引发FeignException。
配置: Feign配置类,可以自定义Feign的编码器、解码器、LogLevel和Contract。
Fallback:定义了一个容错处理类。当调用远程接口失败或超时时,将调用相应接口的容错逻辑。由回退指定的类必须实现由@FeignClient标记的接口。
fallbackFactory:工厂类用于生成回退类示例。通过这个属性,可以实现各接口的通用容错逻辑,减少重复代码。
Path:定义了当前FeignClient的统一前缀。
一、创建模块(微服务-消费者-电影-feign-定制)
项目结构如下:
第二,pom.xml文件
?xmlversion='1.0 '编码='UTF-8 '?
project xmlns=' http://aven . Apache . org/POM/4 . 0 . 0 '
xmlns : xsi=' http://www . w3 . org/2001/XMLSchema-instance '
xsi : schema location=' http://aven . Apache . org/POM/4 . 0 . 0http://aven . Apache . org/xsd/maven-4 . 0 . 0 . xsd '
父母
artifactIdmicroservice-spring-cloud/artifactId
groupIdcom.jacky/groupId
1.0版-快照/版本
/家长
模型版本4 . 0 . 0/模型版本
人工智能微服务-消费者-电影-feign-定制/人工智能
包装罐/包装
性能
project . build . sourceencodingutf-8/project . build . sourceencoding
project . reporting . OutPutencodingtf-8/project . reporting . OutPutencod
ing>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<executions>
<!--设置在执行maven 的install时构建镜像-->
<execution>
<id>build-image</id>
<phase>install</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<!--安装了docker的主机,并且打开了api remote接口设置-->
<dockerHost>http://192.168.6.130:5678</dockerHost>
<pushImage>true</pushImage><!--设置上传镜像到私有仓库,需要docker设置指定私有仓库地址-->
<!--镜像名称-->
<imageName>${docker.repostory}/${docker.image.prefix}/${project.artifactId}:${project.version}</imageName>
<!--镜像的基础版本-->
<baseImage>java:openjdk-8-jdk-alpine</baseImage>
<!--镜像启动参数-->
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>
三、配置文件
spring: application: name: microservice-consumer-movie-feign-customizing server: port: 7901 eureka: 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; @Configuration public 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; @Configuration public 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; @RestController public 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 @EnableFeignClients public class ConsumerMovieFeignApplication { public static void main(String[] args) { SpringApplication.run(ConsumerMovieFeignApplication.class, args); } }
感谢你能够认真阅读完这篇文章,希望小编分享的“SpringCloud分布式微服务b2b2c电子商务docker-feign配置的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/104210.html