java spring cloud版b2b2c社交电商spring cloud分布式微服务-docker-feign-hystrix(六)

简介

spring cloud b2b2c电子商务社交平台源码请加企鹅求求:叁五叁六贰四柒贰五九。上一节我们讨论feign的配置,这节我们讨论一下,feign+hystrix调用生产者时,进行容错处理。

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

image

二、pom.xml文件



    
        microservice-spring-cloud
        com.jacky
        1.0-SNAPSHOT
    
    4.0.0
 
 
    microservice-consumer-movie-feign-with-hystrix
    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
        
 
        
            org.springframework.cloud
            spring-cloud-starter-hystrix
        
    
    
        
            
                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
                        
                    
                
            
        
    

三、配置文件application.yml

spring:
  application:
    name: microservice-consumer-movie-feign-with-hystrix
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
    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;
 
@Component
public 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;
 
@RestController
public 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
@EnableCircuitBreaker
public class ConsumerMovieFeignApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConsumerMovieFeignApplication.class, args);
  }
}

spring cloud b2b2c电子商务社交平台源码请加企鹅求求:叁五叁六贰四柒贰五九

你可能感兴趣的:(java spring cloud版b2b2c社交电商spring cloud分布式微服务-docker-feign-hystrix(六))