原创

Spring Cloud 学习笔记3 Eureka 结合feign实现负载均衡

Spring Cloud学习笔记3


上篇文章Spring Cloud 学习笔记2 Eureka 结合ribbon实现负载均衡 。实现了利用ribbon进行负载均衡,这篇文章接着学习方大佬的内容,实现feign负载均衡。 feign负载均衡其实底层也是ribbon,具体见这里:Spring Cloud Feign设计原理.

本次代码都已经提交在github上,点击这里访问

本篇文章基于上篇文章的代码。需要修改的地方:

pom.xml->modules 中增加

<module>feign</module>

新建spring boot项目feign

feign中的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>cn.djc8</groupId>
        <artifactId>blog2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>cn.djc8</groupId>
    <artifactId>feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>feign</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>

            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    </project>

修改 src\main\resources\application.properties 名称为 src\main\resources\application.yml 修改 src\main\resources\application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/
server:
  port: 7011
spring:
  application:
    name: feign

修改 FeignApplication.java ,增加注解 @EnableFeignClients

1.jpg

增加 cn.djc8.feign.IClientWelcome 接口类,内容如下:

package cn.djc8.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

//客户端(消费者)
@FeignClient("eurekaclient")
public interface IClientWelcome {
    //具体的接口。 restful
    @RequestMapping(value="/welcome",method=RequestMethod.GET)
    String welcomeFromClientOne(@RequestParam(value="name") String name);

}

增加 cn.djc8.feign.WelcomeCtl ,内容如下:

package cn.djc8.feign;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WelcomeCtl {
    @Autowired
    IClientWelcome iclientWelcome;

    @GetMapping(value="/welcome")
    public String welcome(@RequestParam String name){
        return iclientWelcome.welcomeFromClientOne(name);
    }
}

启动client两次,端口分别为7004,7005,参考 如何开启多实例(单个idea开启多个client,这样注册中心就会看到多个了) 启动feign项目,端口为7011. 2.jpg

web浏览器中 访问链接 :http://127.0.0.1:7010/welcome

浏览器中交替显示,代表负载均衡成功

username PORT:7005
username PORT:7004
温馨提示:
本文最后更新于 2020年08月18日,已超过 688 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我
正文到此结束
该篇文章的评论功能已被站长关闭
本文目录