Feign + eureka 实现负载均衡

Feign 自带ribbon

1: 添加依赖

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


        <!--&lt;!&ndash;eureka-client 2.x版本使用,和之前不同,后缀已经表明了一切&ndash;&gt;-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

2:  application

server:
  port: 8081
spring:
  application:
    name: cloud-order-service

eureka:
  instance:
    hostname: localhost
    port: 7001
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    #是否从eurekaServer抓取自己的注册信息,默认是true。
    #单节点无所谓,集群必须设置为true擦能配个ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

ribbon:
  #指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
  ReadTimeout: 5000
  #指的是建立连接后从服务器读取到可用资源所用的时间
  ConnectTimeout: 5000

logging:
  level:
    # feign日志以什么级别监控哪个接口
    com.atguigu.springcloud.service.FeignPaymentService: debug

2: main 注册允许feign

@SpringBootApplication
@EnableFeignClients
//@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = MySelfRule.class)
public class FeignOrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(FeignOrderMain80.class,args);
    }
}

3: FeignPaymentService  写接口,指向服务提供者的接口

      1):CLOUD-PAYMENT-SERVICE 是服务提供者注册的名字,可在eureka上查看

      2):payment/get/{id} , payment/openfeign/timeout   指向服务提供者里面congtroller的接口地址

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface FeignPaymentService {
    @GetMapping("payment/get/{id}")
    CommonResult<Payment> getPaymentById(@PathVariable("id")Long id);

    @GetMapping("payment/openfeign/timeout")
    String openFeignTimeout();
}

4:  消费的的OrderControlle

@RestController
@RequestMapping("order")
@CrossOrigin
@Slf4j
public class OrderController {
    @Autowired
    private FeignPaymentService feignPaymentService;

    //http://localhost:8083/order/consumer/payment/get/1
    @GetMapping("consumer/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){

        System.out.println("发起请求:");

        return feignPaymentService.getPaymentById(id);
    }

    //http://localhost/order/consumer/payment/openfeign/timeout
    @GetMapping("consumer/payment/openfeign/timeout")
    public String openFeignTimeout(){

        System.out.println("发起请求:");

        return feignPaymentService.openFeignTimeout();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Feign是一个用于服务间通信的轻量级Java HTTP客户端。它可以与Eureka和Ribbon组合使用以支持负载均衡。要配置Feign进行负载均衡,可以按照以下步骤操作: 1. 添加依赖:在项目的pom.xml文件中添加Feign的依赖,如下所示: ``` <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> ``` 2. 启用Feign:在Spring Boot应用程序的启动类上添加`@EnableFeignClients`注解,以启用Feign的功能。例如: ``` @SpringBootApplication @EnableFeignClients public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 3. 创建Feign客户端接口:在需要调用其他服务的地方创建一个Feign客户端接口,并使用`@FeignClient`注解指定要调用的服务的名称。例如: ``` @FeignClient(name = "your-service") public interface YourServiceClient { // 定义需要调用的服务接口方法 } ``` 4. 使用Feign客户端:在需要调用服务的地方注入Feign客户端,并使用与调用本地方法类似的方式进行调用。例如: ``` @RestController public class YourController { private final YourServiceClient yourServiceClient; public YourController(YourServiceClient yourServiceClient) { this.yourServiceClient = yourServiceClient; } @GetMapping("/your-endpoint") public String yourEndpoint() { return yourServiceClient.yourMethod(); } } ``` 以上就是配置Feign进行负载均衡的基本步骤。通过使用FeignEureka/Ribbon的组合,您可以实现服务间的负载均衡和动态服务发现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值