# 从浅入深 学习 SpringCloud 微服务架构(六)Feign(3)

本文围绕SpringCloud微服务架构展开,总结了组件使用方式,包括注册中心和服务调用。重点探讨高并发问题,通过模拟环境和Jmeter工具测试发现问题,分析是因tomcat线程池管理和最大并发设置导致。提出线程池隔离处理请求积压,还介绍了服务容错的雪崩效应、服务隔离、熔断降级和服务限流等核心知识。

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

从浅入深 学习 SpringCloud 微服务架构(六)Feign(3)

一、组件的使用方式总结

1、注册中心

1) Eureka

搭建注册中心

引入依赖 spring-cloud-starter-netflix-eureka-server。
配置 EurekaServer。
通过 @EnableEurekaServer 激活 Eureka Server 端配置。

服务注册

服务提供者引入 spring-cloud-starter-netflix-eureka-client 依赖。
通过 eureka.client.serviceur1.defaultZone 配置注册中心地址。

2)consul

搭建注册中心

下载安装 consul
命令形式启动 consul consul agent -dev

服务注册

服务提供者引入 spring-cloud-starter-consul-discovery 依赖。
通过 spring.c1oud.consul.host 和 spring.cloud.consul.port 指定 Consul Server 的请求地址。

2 服务调用

1)Ribbon

通过 Ribbon 结合 RestTemplate 方式进行服务调用只需要在声明 RestTemplate 的方法上添加注解	@LoadBalanced 即可。
可以通过{服务名称}.ribbon.NFLoadBalancerRuleClassName 配置负载均衡策略。

2)Feign

服务消费者引入 spring-cloud-starter-openfeign 依赖。
通过 @FeignClient 声明一个调用远程微服务接口。
启动类上通过 @EnableFeignclients 激活 Feign。

二、高并发问题:模拟环境

1、在消费者子工程(子模块) order_service 中,application.yml 配置文件中,添加 tomcat 最大连接数量,用以模拟高并发环境问题。


##  spring_cloud_demo\order_service\src\main\resources\application.yml

server:
  port: 9002 #端口
#  port: ${port:9002}  # 启动端口设置为动态传参,如果未传参数,默认端口为 9002
  tomcat:
    max-threads: 10  # 设置 tomcat 最大连接数量,用以模拟高并发环境问题。

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
#    url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
    url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8
    username: 'root'
    password: '012311'
  application:
    name: service-order #服务名称
  jpa:
    database: MySQL
    show-sql: true
    open-in-view: true

eureka:  # 配置 Eureka
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
  instance:
    prefer-ip-address: true  # 使用 ip 地址注册
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

# 配置 feign 日志的输出:
# 日志配置:NONE:不输出日志,BASIC:适用于生产环境追踪问题,HEADERS:在BASIC基础上记录请求和响应头信息,FULL:记录所有。
feign:
  client:
    config:
      service-product:  # 需要调用的服务名称
        loggerLevel: FULL
logging:
  level:
    djh.it.order.feign.ProductFeignClient: debug


2、在 消费者子工程(子模块) order_service 中,OrderController.java 文件中,添加一个方法,用以模拟高并发环境问题。


/**
 *   spring_cloud_demo\order_service\src\main\java\djh\it\order\controller\OrderController.java
 *
 *  2024-4-24 订单的 controller 类 OrderController.java
 */
package djh.it.order.controller;

import djh.it.order.command.OrderCommand;
import djh.it.order.domain.Product;
import djh.it.order.feign.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderController {

    // 注入 restTemplate 对象
    @Autowired
    private RestTe
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

段子手-168

你的鼓励将是我你的创作最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值