SpringCloud-Finchley系列05-服务消费者Eureka Client(openfeign)搭建

本文详细介绍使用SpringCloud构建微服务的全过程,涵盖Eureka服务注册与发现、OpenFeign服务调用、Hystrix断路器、HystrixDashboard监控、Zipkin链路追踪等关键技术。通过创建Maven项目,配置服务,实现负载均衡、熔断处理及服务调用,最终验证微服务架构的正确性和稳定性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 创建项目
  • 配置文件
  • 启动类
  • 服务
  • pom文件
  • 验证

一.创建项目
选取Eureka作为服务注册和发现组件
OpenFeign作为服务调用组件
Hystirx作为断路器组件
Hystrix Dashboard作为监控组件
Spring Cloud Sleuth(zipkin)作为链路追踪组件

创建maven项目flow-eureka-consumer02(openfeign, 8782)作为子模块

OpenFeign是一个声明式的Rest客户端(伪客户端), 采用基于接口的注解
默认集成了Ribbon, 实现负载均衡. 整合了Hystrix, 有熔断的能力

二.配置文件
application-eureka-consumer02-dev.yml, 内容如下:

# dev_config
spring:
  application:
    name: eureka-consumer-feign
  zipkin:
    base-url: http://localhost:9411

server:
  port: 8782

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000
      circuitBreaker:
        requestVolumeThreshold: 5
        errorThresholdPercentage: 10
        sleepWindowInMilliseconds: 10000

eureka:
  instance:
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      # 单个配置
#      defaultZone: http://localhost:8761/eureka/
      # 集群配置
      defaultZone: http://127.0.0.1:8761/eureka/, http://127.0.0.1:8762/eureka/

feign:
  hystrix:
    enabled: true

management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

info:
  company.name: org.flowframework
  app.name: eureka-consumer-feign
  app.desc: springcloud-eureka-client-consumer-feign
  author: robin

三.启动类
Application.java

@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

四.服务
1.ServiceError.java, 熔断处理

@Component
public class ServiceError implements HelloFeignService {

    @Override
    public String hello(String name) {
        return error(name);
    }

    // 熔断方法
    public String error(String name) {
        return "service error";
    }
}

2.HelloController.java, 调用consumer01服务

@RestController
@RequestMapping(value = "/hello", produces = "application/json")
public class HelloController {

    @Autowired
    HelloFeignService helloFeignService;
    @Autowired
    ConsumerFeignService consumerFeignService;

    @RequestMapping(value = "/feign/{name}", method = RequestMethod.GET)
    public String hello(@PathVariable("name") String name) {
        return helloFeignService.hello(name) + ", response port: 8782";
    }

    // 调用consumer01服务
    @RequestMapping(value = "/consumer/{name}", method = RequestMethod.GET)
    public String consumer(@PathVariable("name") String name) {
        return consumerFeignService.consumer(name) + ", response port: 8782";
    }
}

3.HelloFeignService.java

@FeignClient(value = "eureka-provider", fallback = ServiceError.class)
public interface HelloFeignService {

    @RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
    String hello(@PathVariable("name") String name);
}

4.ConsumerFeignService.java

@FeignClient(value = "eureka-consumer-ribbon")
public interface ConsumerFeignService {

    @RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
    String consumer(@PathVariable("name") String name);
}

五.pom文件

<?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">

    <modelVersion>4.0.0</modelVersion>

    <groupId>org.flowframework</groupId>
    <artifactId>flow-eureka-consumer02</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.flowframework</groupId>
        <artifactId>springcloud</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <!-- eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!-- openfeign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <!-- hystrix -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

        <!-- zipkin -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>true</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>flow-eureka-consumer02</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

六.验证
1.服务调用
http://localhost:8782/hello/feign/robin
多次请求, 会发现轮流访问不同端口的服务提供者的服务(负载均衡)
Hello robin, service: provider01, response port: 8782
Hello robin, service: provider02, response port: 8782

2.http://localhost:8761/
在这里插入图片描述
3.Hystrix Dashboard
http://localhost:8782/hystrix
在这里插入图片描述
地址–http://localhost:8782/actuator/hystrix.stream
Delay–2000ms
Title–eureka-consumer-feign
Monitor Stream
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值