真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

SpringCloud分布式微服務(wù)b2b2c電子商務(wù)docker-feign配置的示例分析

這篇文章主要介紹了SpringCloud分布式微服務(wù)b2b2c電子商務(wù)docker-feign配置的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

公司主營業(yè)務(wù):成都做網(wǎng)站、成都網(wǎng)站制作、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)推出鹽山免費(fèi)做網(wǎng)站回饋大家。

討論一下feign配置,通過編寫配置類,我們可以自定義feign的日志級(jí)別,日志掃描目錄,可以通過feign調(diào)用服務(wù)在eureka上的調(diào)用信息。

feign聲明接口之后,在代碼中通過@Resource或者@Autowired注入之后即可使用。

@FeignClient標(biāo)簽的常用屬性如下:

name:指定FeignClient的名稱,如果項(xiàng)目使用了Ribbon,name屬性會(huì)作為微服務(wù)的名稱

url: url一般用于調(diào)試,可以手動(dòng)指定@FeignClient調(diào)用的地址

decode404:當(dāng)發(fā)生http 404錯(cuò)誤時(shí),如果該字段位true,會(huì)調(diào)用decoder進(jìn)行解碼,否則拋出FeignException

configuration: Feign配置類,可以自定義Feign的Encoder、Decoder、LogLevel、Contract

fallback: 定義容錯(cuò)的處理類,當(dāng)調(diào)用遠(yuǎn)程接口失敗或超時(shí)時(shí),會(huì)調(diào)用對(duì)應(yīng)接口的容錯(cuò)邏輯,fallback指定的類必須實(shí)現(xiàn)@FeignClient標(biāo)記的接口

fallbackFactory: 工廠類,用于生成fallback類示例,通過這個(gè)屬性我們可以實(shí)現(xiàn)每個(gè)接口通用的容錯(cuò)邏輯,減少重復(fù)的代碼

path: 定義當(dāng)前FeignClient的統(tǒng)一前綴

一、創(chuàng)建模塊(microservice-consumer-movie-feign-customizing)

項(xiàng)目結(jié)構(gòu)如下:

SpringCloud分布式微服務(wù)b2b2c電子商務(wù)docker-feign配置的示例分析

二、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-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
# 解決第一次請(qǐng)求報(bào)超時(shí)異常的方案:
# hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000  #延長超時(shí)時(shí)間
# 或者:
# hystrix.command.default.execution.timeout.enabled: false
# 或者:
feign.hystrix.enabled: false                       # 索性禁用feign的hystrix支持
# 超時(shí)的issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/768
# 超時(shí)的解決方案: 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

四、實(shí)體類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日志顯示級(jí)別
  }
}

八、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添加了安全驗(yàn)證,則需要配置上面的用戶名、密碼
  }
}

九、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;
  /**
   * 調(diào)用生產(chǎn)者服務(wù)
   * @param id
   * @return
     */
  @GetMapping("/movie/{id}")
  public User findById(@PathVariable Long id) {
    return this.userFeignClient1.findById(id);
  }
  /**
   * 獲得服務(wù)在eureka信息
   * @param serviceName
   * @return
     */
  @GetMapping("/{serviceName}")
  public String findServiceInfoFromEurekaByServiceName(@PathVariable String serviceName) {
    return this.userfeignClient2.findServiceInfoFromEurekaByServiceName(serviceName);
  }
}

十、啟動(dòng)類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);
  }
}

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“SpringCloud分布式微服務(wù)b2b2c電子商務(wù)docker-feign配置的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!


本文題目:SpringCloud分布式微服務(wù)b2b2c電子商務(wù)docker-feign配置的示例分析
標(biāo)題鏈接:http://weahome.cn/article/jpdgcc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部