SpringCloud分散式微服務b2b2c電子商務docker-feign配置(六)
這節我們討論一下feign配置,透過編寫配置類,我們可以自定義feign的日誌級別,日誌掃描目錄,可以透過feign呼叫服務在eureka上的呼叫資訊。
feign宣告介面之後,在程式碼中透過@Resource或者@Autowired注入之後即可使用。
@FeignClient標籤的常用屬性如下:
name:指定FeignClient的名稱,如果專案使用了Ribbon,name屬性會作為微服務的名稱,用於服務發現瞭解springcloud架構可以加求求:三五三六二四七二五九
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檔案
<?xml version="1.0" encoding="UTF-8"?> <project xmlns=" xmlns:xsi=" xsi:schemaLocation=" <parent> <artifactId>microservice-spring-cloud</artifactId> <groupId>com.jacky</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>microservice-consumer-movie-feign-customizing</artifactId> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <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: 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: # 超時的解決方案: # 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 = ") 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); } }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69952307/viewspace-2670418/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- java b2b2c電子商務 springcloud分散式微服務-Session共享JavaSpringGCCloud分散式微服務Session
- SpringCloud分散式微服務b2b2c電子商務分散式微服務-docker-feign-hystrix(七)SpringGCCloud分散式微服務Docker
- SpringCloud分散式微服務b2b2c電子商務-Spring Boot配置檔案詳解GCCloud分散式微服務Spring Boot
- SpringCloud分散式微服務b2b2c電子商務-Gateway的基本入門SpringGCCloud分散式微服務Gateway
- Springcloud分散式微服務b2b2c電子商務一整合Hystrix(二)SpringGCCloud分散式微服務
- JAVA b2b2c電子商務SpringCloud分散式微服務spring-security-基本JavaSpringGCCloud分散式微服務
- SpringCloud分散式微服務b2b2c電子商務(五)springboot整合 beatlsqlGCCloud分散式微服務Spring BootTLSSQL
- SpringCloud分散式微服務b2b2c電子商務-hystrix引數詳解(十)SpringGCCloud分散式微服務
- SpringCloud分散式微服務b2b2c電子商務(十三)Springboot整合RabbitMQGCCloud分散式微服務Spring BootMQ
- SpringCloud分散式微服務b2b2c電子商務(一)元件和概念介紹SpringGCCloud分散式微服務元件
- SpringCloud分散式微服務b2b2c電子商務-SpringBoot 整合JPA(四)GCCloud分散式微服務Spring Boot
- Java b2b2c電子商務 SpringCloud分散式微服務-config的簡單配置shh方式JavaSpringGCCloud分散式微服務
- SpringCloud分散式微服務b2b2c電子商務-定時任務(Scheduling Tasks)SpringGCCloud分散式微服務
- SpringCloud分散式微服務b2b2c電子商務(三)註冊中心叢集篇SpringGCCloud分散式微服務
- SpringCloud分散式微服務b2b2c電子商務docker-feign-hystrix-ribbon(八)SpringGCCloud分散式微服務Docker
- SpringCloud分散式微服務b2b2c電子商務-docker部署spring cloud專案(十一)SpringGCCloud分散式微服務Docker
- Springcloud分散式微服務b2b2c電子商務一Spring Cloud常見問題(一)SpringGCCloud分散式微服務
- SpringCloud分散式微服務b2b2c電子商務(二)Eureka(服務註冊和服務發現基礎篇)SpringGCCloud分散式微服務
- 微服務、分散式、雲架構構建電子商務平臺微服務分散式架構
- JAVA spring cloud boot b2b2c電子商務分散式微服務JavaSpringCloudboot分散式微服務
- (十四)JAVA springboot微服務b2b2c電子商務系統- Spring Cloud構建分散式電子商務平臺JavaSpring Boot微服務Cloud分散式
- SpringCloud分散式微服務b2b2c電子商務-Spring Cloud自定義引導屬性源SpringGCCloud分散式微服務
- 分散式微服務雲架構電子商務平臺分散式微服務架構
- 分散式微服務雲架構構建電子商務分散式微服務架構
- SpringCloud(六)分散式事務SpringGCCloud分散式
- SpringCloud分散式微服務b2b2c電子商務(十二)在springboot中用redis實現訊息佇列GCCloud分散式微服務Spring BootRedis佇列
- 關於SpringCloud微服務雲架構構建B2B2C電子商務平臺之-(SpringGCCloud微服務架構
- 構建分散式微服務雲架構電子商務平臺分散式微服務架構
- 分散式微服務雲架構構建電子商務平臺分散式微服務架構
- SpringCloud微服務系列- 分散式能力建設之配置中心SpringGCCloud微服務分散式
- JAVA springboot微服務b2b2c電子商務系統-(七)高可用的分散式配置中心(Spring Cloud Config)JavaSpring Boot微服務分散式Cloud
- SpringCloud微服務雲架構構建B2B2C電子商務平臺分析之-服務消費(Ribbon)SpringGCCloud微服務架構
- 微服務之分散式配置中心微服務分散式
- java B2B2C Springcloud電子商務平臺原始碼JavaSpringGCCloud原始碼
- 微服務雲架構電子商務平臺微服務架構
- SpringCloud微服務系列- 分散式能力建設之微服務閘道器SpringGCCloud微服務分散式
- springcloud微服務分散式雲架構-SpringCloud簡介SpringGCCloud微服務分散式架構
- java版 電子商務Springcloud分散式微服務多使用者商城系統-Springboot專案打包JavaGCCloud分散式微服務Spring Boot