码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA springcloud ssm b2b2c多用户商城系统源码-docker-feign-hystrix(六)

时间:2019-01-18 10:54:01      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:div   sim   big   ica   Delve   ipa   技术分享   break   remote   

简介

上一节我们讨论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-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 springcloud ssm b2b2c多用户商城系统源码-docker-feign-hystrix(六)

标签:div   sim   big   ica   Delve   ipa   技术分享   break   remote   

原文地址:https://www.cnblogs.com/sc-sc/p/10286325.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!