irpas技术客

spring cloud 2020.0.3 学习记录(二)openfeign实现服务间调用以及负载均衡_weixin_45581403

未知 3686

?

目录

前言

一、依赖

二、使用步骤

1.启动类

2.yml配置

3.controller方法实现(例)

被调用端

openfeign实现方法间调用?

?调用端controller

?


前言

此处注册中心使用consul,consul可以免去eureka server项目创建的步骤,同时依赖中包含org.springframework.cloud:spring-cloud-starter-loadbalancer,能与新版本的openfeign实现服务调用间的负载均衡,文中的仅实现了简单的方法调用作为学习记录。

?建议使用spring initializr创建服务文中springboot版本为2.4.6

?

一、依赖 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</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> </dependencies> ?以consul作为注册中心时,服务注册到注册中心需要spring-boot-starter-actuator依赖实现健康检测这里创建两个boot项目分别作为生产者和消费者实现服务间调用 二、使用步骤 1.启动类 @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class Openfeigndemo1Application { public static void main(String[] args) { SpringApplication.run(Openfeigndemo1Application.class, args); } }

openfeign需要@EnableFeignClients注解支持,仅在服务调用端加入即可

2.yml配置 server: port: 8083 #服务端口号 spring: application: name: clientB cloud: consul: host: localhost port: 8500 #连接consul注册中心的地址 http://localhost:8500,consul默认端口8500 discovery: service-name: ${spring.application.name} #服务名 3.controller方法实现(例)

实现一个简单的返回string值的方法

被调用端 @RestController public class textcontroller { Logger logger= LoggerFactory.getLogger(textcontroller.class); @Value("${server.port}") private int port; // @RequestMapping("/bArray") //参数中传递数组 // public String text04(@RequestParam("strs") String[] strs){ // return "bArray"+port; // } // @RequestMapping("/bList") //参数中传递ArrayList // public String text05(ArrayList<String> list){ // return "bList"+port; // } // @RequestMapping("/bObject") //参数中传递对象 // public String text03(@RequestBody user user){ // // return user.getName()+"|||"+port; // } @RequestMapping("/b") //参数中传递string值 public String text01(){ try { TimeUnit.MILLISECONDS.sleep(5 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } return "bParam,当前服务端口为:"+port; } // @RequestMapping("/bPath/{str}/{num}") //参数中传递string值(path) // String text02(@PathVariable("str") String str, @PathVariable("num") Integer num){ // return str+num+"bPath,当前服务端口为:"+port; // } } openfeign实现方法间调用?

调用端通过openfeign实现服务间调用做了相当程度的优化,需要一个接口文件使用注解在注册中心通过服务名调用方法

由于openfeign 3.0.3 支持loadbalancer,在有spring-cloud-loadbalancer依赖时,openfeign会默认使用loadbalancer实现负载均衡,负载均衡默认使用轮巡策略,即同一个服务集群下由上到下依次调用循环

测试

?仅添加了依赖,获取到的client的属性就默认为FeignBlockingLoadBalancerClient对象

接口使用@FeignClient(value = "注册名")注解作为注册中心的连接,同时将接口加入容器。接口下复制被调用的方法,@RequestMapping必须相同而方法名可以不同。

示例

@FeignClient(value = "clientB") public interface bFeign { @RequestMapping("/bArray") public String text04(@RequestParam("strs") String[] strs); @RequestMapping("/bList") public String text05(@RequestParam("list") List<Integer> list); @RequestMapping("bObject") String text03(@RequestBody user user); @RequestMapping("/b") String text01(); @RequestMapping("/bPath/{str}/{num}") String text02(@PathVariable("str") String str, @PathVariable("num") Integer num); } ?调用端controller

从容器中获取接口实现方法间调用

@RestController public class textcontroller { Logger log= LoggerFactory.getLogger(textcontroller.class); @Resource private bFeign bFeign; @RequestMapping("/a") public String text01(){ String resoult= bFeign.text01(); return resoult; } }

?


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #Spring #Cloud #202003