springcloud服务调用其它服务接口实例及ribbon均衡负载
来源:互联网 发布:北京谷歌seo 编辑:程序博客网 时间:2024/06/08 07:48
创建服务注册中心(端口:8000)并启动
创建被调用的服务A(端口8010,服务名:service-hi)并编写controller/action将相关方法暴露出来 如下:
package com.ldcn.cloudsd.controllers;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class BizController { @Value("${server.port}") String port; @RequestMapping("/hi") public String home() { return "hi,i am from port:" +port; } }
服务A的一个访问controller/action地址为:localhost:8010/hi 返回一个字符串 准备被调用
创建被调用服务B(端口8011,服务名:service-hi)并编写controller/action同上
服务B的一个访问controller/action地址为:localhost:8011/hi 返回一个字符串 准备被调用
启动服务A和服务B,此时springcloud注册中心可以看service-hi服务8010和8011的2个实例
创建另一个服务ribbon(端口:8013)注册到注册中心,准备调用服务A或B的hi返回的字符串:
服务ribbon编写一个接口 如下:
package com.ldcn.cloudsd.callservice;import org.springframework.cloud.netflix.feign.FeignClient;import org.springframework.stereotype.Component;import org.springframework.web.bind.annotation.RequestMapping;@Component@FeignClient(value="SERVICE-HI")public interface CallClientService {@RequestMapping(value="/hi")public String getClientString();}
@FeignClient(value="SERVICE-HI") value为调用的服务名 即application.properties/yml配置里spring.application.name=xxx的配置
接口方法上面@RequestMapping(value="/hi")为service-hi服务的controller/action访问地址
服务ribbon编写controller/action调用刚刚编写的接口 :
package com.ldcn.cloudsd.controllers;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.ldcn.cloudsd.callservice.CallClientService;@RestControllerpublic class BizController {@AutowiredCallClientService callClientService; @RequestMapping("/call") public String home() { return callClientService.getClientString(); }}
启动服务ribbon,并访问服务ribbon的地址localhost:8013/call并不断刷新,浏览器依次出现如下结果:
调用8010:
调用8011:
注意:
服务ribbon启动类需要添加如下注解
@EnableFeignClients
同时需引入以下jar包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId></dependency>
阅读全文
0 0
- springcloud服务调用其它服务接口实例及ribbon均衡负载
- SpringCloud-----1、Eureka实现服务治理(Ribbon客户端负载均衡调用服务)
- SpringCloud微服务系列(4): 服务发现与消费及客户端负载均衡Ribbon
- SpringCloud之Ribbon+Rest调用负载均衡
- spring cloud之服务调用及使用ribbon实现负载均衡(三)
- springCloud学习01之eureka服务发现-提供者-消费者ribbon/feign-负载均衡
- SpringCloud 笔记 (二)---- 简单搭建一个服务消费者,实现简单的ribbon负载均衡
- SpringCloud(三)Ribbon负载均衡
- SpringCloud---Ribbon客户端负载均衡
- SpringCloud的Ribbon负载均衡
- springcloud 中使用eureka/ribbon/Hystrix负载均衡及断路器
- SpringCloud-----2、Eureka实现服务治理(Feign客户端声明式负载均衡调用服务)
- SpringCloud(四)Ribbon自定义负载均衡
- SpringCloud(五)Ribbon自定义负载均衡插件
- SpringCloud(四)Ribbon自定义负载均衡
- springcloud使用ribbon实现客户端负载均衡
- Eureka+ribbon 实现服务注册与发现和负载均衡
- springcloud入门系列(2)-Feign、Ribbon实现Rest接口请求和负载均衡
- 学习他人消灭星星代码的收获
- 1TensorFlow实现自编码器-1.4 TensorFlow实现降噪自动编码器--运行会话,训练模型
- Qt类型转换
- stl map键都是唯一的
- Springboot 上传文件至阿里云OSS
- springcloud服务调用其它服务接口实例及ribbon均衡负载
- Tomcat的账号与密码在哪?
- Linux进程的管理与调度
- React Native Redux TodoList Demo
- svn设置提交忽略某些文件或文件夹
- Firewall常用
- 面试手写代码的题目
- 获取字符串中数字,并计算指定时间的后几个月是几月几号
- 数据库事务的四大特性以及事务的隔离级别