SpringCloud分布式微服务b2b2c电子商务分布式微服务-docker-feign-hyst

上一节我们讨论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"&gt;
<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:[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)了解springcloud架构可以加求求:三五三六二四七二五九

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);
  }
}

原文地址:https://blog.51cto.com/14622290/2463353

时间: 2024-07-31 13:29:14

SpringCloud分布式微服务b2b2c电子商务分布式微服务-docker-feign-hyst的相关文章

JAVA springboot微服务b2b2c电子商务系统-springboot集成swagger2,构建优雅的Restful API(十一)

swagger,中文"拽"的意思.它是一个功能强大的api框架,它的集成非常简单,不仅提供了在线文档的查阅,而且还提供了在线文档的测试.另外swagger很容易构建restful风格的api,简单优雅帅气,正如它的名字. 一.引入依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <vers

SpringCloud分布式微服务b2b2c电子商务(二)Eureka(服务注册和服务发现基础篇)

一:Eureka简介 Eureka是Spring Cloud Netflix的一个子模块,也是核心模块之一.用于云端服务发现,一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移.了解springcloud架构可以加求求:三五三六二四七二五九,服务注册与发现对于微服务系统来说非常重要.有了服务发现与注册,你就不需要整天改服务调用的配置文件了,你只需要使用服务的标识符,就可以访问到服务.他的功能类似于dubbo的注册中心(register).服务发现:服务发现是微服务基础架构的

JAVA springboot微服务b2b2c电子商务系统 (十五)Springboot整合RabbitMQ

这篇文章带你了解怎么整合RabbitMQ服务器,并且通过它怎么去发送和接收消息.我将构建一个springboot工程,通过RabbitTemplate去通过MessageListenerAdapter去订阅一个POJO类型的消息. 准备工作 15min IDEA maven 3.0 在开始构建项目之前,机器需要安装rabbitmq,你可以去官网下载,http://www.rabbitmq.com/download.html ,如果你是用的Mac,你可以这样下载: brew install rab

JAVA springboot微服务b2b2c电子商务系统-hystrix参数详解(八)

简介 上节我们讨论了hystrix+feign+ribbon,但是可能很多人都知道hystrix还有线程隔离,信号量隔离,等等各种参数配置,在这几就记录下hystrix的参数, 一.hystrix参数使用方法 通过注解@HystrixCommand的commandProperties去配置, 如下就是hystrix命令超时时间命令执行超时时间,为1000ms和执行是不启用超时 @RestController public class MovieController { @Autowired pr

Java 版SpringCloud分布式微服务b2b2c电子商务- Zuul基于Consul配置及详

一.构建工程 1.引入依赖 <!--SpringBoot2.0以上版本需引入该依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org

SpringCloud分布式微服务b2b2c电子商务(一)构建第一个SpringBoot工程

spring boot 它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个web工程.它采用的是建立生产就绪的应用程序观点,优先于配置的惯例. 可能你有很多理由不放弃SSM,SSH,但是当你一旦使用了springboot ,你会觉得一切变得简单了,配置变的简单了.编码变的简单了,部署变的简单了,感觉自己健步如飞,开发速度大大提高了.了解springcloud架构可以加求求:三五三六二四七二五九,就好比,当你用了IDEA,你会觉得再也回不到Ec

SpringCloud分布式微服务b2b2c电子商务(三)注册中心集群篇

集群环境搭建第一步:我们新建两个注册中心工程一个叫eureka_register_service_master.另外一个叫eureka_register_service_backup eureka_register_service_master的application.properties配置如下 server.port=7998 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false spring.

SpringCloud分布式微服务b2b2c电子商务docker-feign-hystrix-rib

在上一节中,我们讨论了feign+hystrix在项目开发中,除了考虑正常的调用之外,负载均衡和故障转移也是关注的重点,这也是feign + ribbon+hystrix的优势所在,本节我们就讨论一下在feign中使用ribbon,有两种方式 一.通过在配置文件application.yml配置,开启ribbon,并指定调用生产者相对上一节可以不做任何更改,可以看项目(microservice-consumer-movie-feign-with-hystrix-hystrix-factory)

SpringCloud分布式微服务b2b2c电子商务-在springboot中用redis实现消息队

准备阶段安装redis,可参考我的另一篇文章java 1.8maven 3.0idea环境依赖创建一个新的springboot工程,了解springcloud架构可以加求求:三五三六二四七二五九.在其pom文件,加入spring-boot-starter-data-redis依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-