首页 > 编程知识 正文

feign自定义负载均衡,nginx负载均衡原理

时间:2023-05-03 19:44:15 阅读:114856 作者:1848

1 OpenFeign配置文件OpenFeign是声明性的、模板化的HTTP客户端。 通过在Spring Cloud上使用OpenFeign,可以通过HTTP请求访问远程服务。 这就像在调用本地方法,开发人员完全看不到正在调用远程方法,更看不到正在访问HTTP请求。

项目结构

1 dependencygroupidorg.spring framework.boot/groupidartifactidspring-boot-starter-web/artifact id/将添加到pom.xml的ependencydependencygroupidorg.spring framework.cloud/groupidartifactidspring-cloud-starter-Netflix-eureka-clie eka ependencydependencygroupidorg.spring framework.cloud

2由APP应用程序. yml配置

主要配置了端口和注册中心地址。 server : port :8085 spring : application : name : spring cloud-openfeigneureka :客户端: register-with-EUR 360 default zone :3358 localhost :8081/eureka /

创建openfeign调用接口package com.example.springcloudribbon.service; importorg.spring framework.cloud.open feign.feign客户端; importorg.spring framework.stereotype.com ponent; importorg.spring帧web.bind.annotation.request mapping; importorg.spring framework.web.bind.annotation.request method; @feignclient(name='servicea ' ) componentpublicinterfacemyinfo ) @requestmapping ) value='/a ',method=request }

4创建controllerpackagecom.example.springcloudribbon.controller; import com.example.springcloudribbon.service.my info; importorg.spring帧web.bind.annotation.get mapping; importorg.spring帧web.bind.annotation.rest controller; import javax.annotation.resource; @ restcontrollerpublicclasstestcontroller { @ resourcemyinfomyinfo; @getmapping(/test ) ) public String getTest ) ) system.out.println ) ) test-openfeign ); String result=myInfo.getUser (; system.out.println(result ); 返回结果; }@getmapping(/test1 ) (public String getTest1 ) ) { return 'test1; }

5启动项目访问URL http://localhost :8085/test

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。