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
增加 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.
web浏览器中 访问链接 :http://127.0.0.1:7010/welcome
浏览器中交替显示,代表负载均衡成功
username PORT:7005
username PORT:7004
本文来自:Spring Cloud 学习笔记3 Eureka 结合feign实现负载均衡-小码农,转载请保留本条链接,感谢!
温馨提示:
本文最后更新于 2020年08月18日,已超过 1,618 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
正文到此结束
- 本文标签: eureka feign
- 本文链接: https://djc8.cn/archives/spring-cloud-learning-note-3-eureka-and-feign-to-realize-load-balancing.html
- 版权声明: 本文由小码农原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
热门推荐
相关文章
该篇文章的评论功能已被站长关闭