SpringCloud分布式微服务b2b2c电子商务分布式微服务中docker-feign-hystrix的示例分析

技术SpringCloud分布式微服务b2b2c电子商务分布式微服务中docker-feign-hystrix的示例分析这篇文章主要介绍SpringCloud分布式微服务b2b2c电子商务分布式微服务中docker-fe

这篇文章主要介绍SpringCloud分布式微服务b2b2c电子商务分布式微服务中假装海斯特里克斯码头的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

一、创建模块(微服务-消费者-电影feign与海斯特里克斯)

二、pom.xml文件

?xmlversion='1.0 '编码='UTF-8 '?

项目xmlns=' http://aven。阿帕奇。org/POM/4。0 .0 '

xmlns : xsi=' http://www。w3。org/2001/XMLSchema-instance '

xsi :架构位置=' http://aven。阿帕奇。org/POM/4。0 .0http://aven。阿帕奇。org/xsd/maven-4。0 .0 .xsd '

父母

artifactIdmicroservice-spring-cloud/artifactId

groupIdcom.jacky/groupId

1.0版-快照/版本

/家长

模型版本4 .0 .0/模型版本

人工智能微服务-消费者-电影feign与海斯特里克斯/人工智能

包装罐/包装

性能

项目。建造。sourceencodingutf-8/项目。建造。源编码

项目。报道。outputen coding TF-8/项目。报道。输出编码

java.version1.8/java.version

/properties

属国

属国

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-web/artifactId

/依赖性

属国

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

以上是“SpringCloud分布式微服务b2b2c电子商务分布式微服务中docker-feign-hystrix的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/104209.html

(0)

相关推荐

  • 能歌善舞近义词,“长袖善舞”指人能歌善舞吗

    技术能歌善舞近义词,“长袖善舞”指人能歌善舞吗“长袖善舞”语出《韩非子。五蠹》:“治强不可贵于外能歌善舞近义词,内政之有也。 今不行法术于内,而事智于外,则不至于治强矣。鄙谚曰:‘长袖善舞,多钱善贾。’此言多资之易为工也

    生活 2021年10月27日
  • 如何配置Git

    技术如何配置Git这篇文章主要介绍了如何配置Git,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。配置Git1.配置git 用户名和邮箱git config

    攻略 2021年11月21日
  • AT3913-XOR Tree【状压dp】

    技术AT3913-XOR Tree【状压dp】 AT3913-XOR Tree【状压dp】正题
    题目链接:https://www.luogu.com.cn/problem/AT3913题目大意
    给出一棵

    礼包 2021年12月18日
  • 怎样查看conda的python环境(如何关联jupyter和conda)

    技术windos jupyter动态切换多个conda环境怎么实现本篇内容主要讲解“windos jupyter动态切换多个conda环境怎么实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让

    攻略 2021年12月21日
  • 有哪些写Python程序的建议

    技术有哪些写Python程序的建议这篇文章主要介绍“有哪些写Python程序的建议”,在日常操作中,相信很多人在有哪些写Python程序的建议问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”

    攻略 2021年11月2日
  • 怎样进行debian apt mysql无密码安装

    技术怎样进行debian apt mysql无密码安装怎样进行debian apt mysql无密码安装,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能

    攻略 2021年11月30日