Spring Cloud Alibaba创建服务消费者Feign(负载均衡)

[!NOTE]

本篇内容需要在上一篇的基础上进行。

请找到上一篇的项目代码,本篇在上一篇的源代码上进行编写。

本系列教程目录:https://blog.csdn.net/laisc7301/article/details/135918617

接着上一篇继续。

首先按照图示新建模块:

在这里插入图片描述

整个项目的文件结构如下图所示:

在这里插入图片描述

修改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">
<parent>
    <groupId>org.laisc</groupId>
    <artifactId>my-cloud</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>my-consumer-feign</artifactId>
<packaging>jar</packaging>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <!-- Spring Boot Begin -->
    <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>
    <!-- Spring Boot End -->

    <!-- Spring Cloud Begin -->
    <!-- Nacos 注册与发现 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!-- feign  -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <!-- sentinel 熔断 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>
    <!-- Nacos 分布式配置中心 -->
    <!--        <dependency>-->
    <!--            <groupId>com.alibaba.cloud</groupId>-->
    <!--            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>-->
    <!--        </dependency>-->
    <!-- Spring Cloud End-->
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>org.laisc.consumer.feign.ConsumerFeignApplication</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

添加application.yml文件:

spring:
  application:
    name: my-consumer-feign
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

server:
  port: 9092

management:
  endpoints:
    web:
      exposure:
        include: "*"

其它文件的源代码:

ConsumerFeignApplication.java

package org.laisc.consumer.feign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerFeignApplication.class, args);
        System.out.println("ok!");
    }
}

FeignService.java

package org.laisc.consumer.feign.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "my-provider")
public interface FeignService {
    @GetMapping(value = "/test/{message}")
    String test(@PathVariable("message") String message);
}

ConsumerFeignController.java

package org.laisc.consumer.feign.conttroller;

import org.laisc.consumer.feign.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConsumerFeignController {

    @Autowired
    private FeignService FeignService;

    @GetMapping(value = "/test/hi")
    public String test() {
        return FeignService.test("Hi Feign");
    }
}

启动ProviderApplicationConsumerFeignApplication

打开 http://localhost:9092/test/hi ,你会在浏览器上看到:

当前服务收到消息: Hi Feign, 该服务由端口: 8081提供。

测试负载均衡

右击服务中的ProviderApplication,复制配置,按照图示配置好。
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

依次启动ProviderApplicationConsumerFeignApplication和新添加的服务ProviderApplication (1)

在浏览器上多次访问 http://localhost:9092/test/hi ,浏览器交替显示:

当前服务收到消息: Hi Feign, 该服务由端口: 8081提供。

当前服务收到消息: Hi Feign, 该服务由端口: 8082提供。

项目源代码下载:https://pan.baidu.com/s/1-Ncvfk480DjVg-H2ULCtLg?pwd=mbuw

上一篇:Spring Cloud Alibaba创建服务消费者:https://laisc7301.github.io/blog/2024/01/27/202401270002SpringCloudAlibaba%E5%88%9B%E5%BB%BA%E6%9C%8D%E5%8A%A1%E6%B6%88%E8%B4%B9%E8%80%85/

下一篇:Spring Cloud Alibaba添加熔断机制Sentinel:https://laisc7301.github.io/blog/2024/01/29/202401290000SpringCloudAlibaba%E6%B7%BB%E5%8A%A0%E7%86%94%E6%96%AD%E6%9C%BA%E5%88%B6Sentinel/

原文:https://laisc7301.github.io/blog/2024/01/28/202401280000SpringCloudAlibaba%E5%88%9B%E5%BB%BA%E6%9C%8D%E5%8A%A1%E6%B6%88%E8%B4%B9%E8%80%85Feign(%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1)/

  • 25
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值