微服務springCloud-docker-feign-hystrix(六)

來源:互聯網
上載者:User

標籤:exec   XML   artifact   user   efi   私人倉庫   end   tor   xsd   

 

簡介

上一節我們討論feign的配置,這節我們討論一下,feign+hystrix調用生產者時,進行容錯處理

一、建立模組(microservice-consumer-movie-feign-with-hystrix)

二、pom.xml檔案

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <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-with-hystrix</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>         <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-hystrix</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>

三、設定檔application.yml

spring:  application:    name: microservice-consumer-movie-feign-with-hystrixserver:  port: 7901eureka:  client:    healthcheck:      enabled: true    serviceUrl:      defaultZone: http://jacky:[email protected]:8761/eureka/,http://jacky:[email protected]:8762/eureka/,http://jacky:[email protected]:8763/eureka/  instance:    prefer-ip-address: true    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

四、實體類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;  } }

五、生產者發生錯誤時使用的類(HystrixClientFallback.java)

package com.jacky.cloud.feign; import org.springframework.stereotype.Component; import com.jacky.cloud.entity.User; @Componentpublic class HystrixClientFallback implements UserFeignClient {   @Override  public User findById(Long id) {    User user = new User();    user.setId(0L);    return user;   }}

六、feign用戶端UserFeignClient.java

package com.jacky.cloud.feign; import com.jacky.cloud.entity.User;import org.springframework.cloud.netflix.feign.FeignClient;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod; @FeignClient(name = "microservice-provider-user", fallback = HystrixClientFallback.class)public interface UserFeignClient {  @RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)  public User findById(@PathVariable("id") Long id);}

七、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.UserFeignClient; @RestControllerpublic class MovieController {   @Autowired  private UserFeignClient userFeignClient;   @GetMapping("/movie/{id}")  public User findById(@PathVariable Long id) {    return this.userFeignClient.findById(id);  }}

八、啟動類

package com.jacky.cloud; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication@EnableEurekaClient@EnableFeignClients@EnableCircuitBreakerpublic class ConsumerMovieFeignApplication {  public static void main(String[] args) {    SpringApplication.run(ConsumerMovieFeignApplication.class, args);  }}

完整項目的源碼來源 支援人員1791743380

微服務springCloud-docker-feign-hystrix(六)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.