spring boot2X整合Consul一使用RestTemplate实现服务调用
Consul可以用于实现分布式系统的服务发现与配置
服务调用有两种方式:
A.使用RestTemplate 进行服务调用
负载均衡——通过Ribbon注解RestTemplate
B.使用Feign 进行声明式服务调用
负载均衡——默认使用Ribbon实现 查看
先使用RestTemplate来实现
1.服务注册发现中心
启动Consul
consul agent -dev
2.服务端
在spring boot2X整合Consul 的基础上
添加服务provider,provider1
provider测试方法
package com.xyz.provider.controller; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class demoController {
@RequestMapping("/hello")
public String Hello(){
return "hello,provider";
} }
provider1测试方法
package com.xyz.provider1.controller; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class demoController {
@RequestMapping("/hello")
public String Hello(){
return "hello,another provider";
} }
启动provider和provider1
浏览器访问http://localhost:8500
有两个服务提供者节点实例
3.客户端
(1)添加依赖
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR4</spring-cloud.version>
</properties> <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> </dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
(2)添加配置
server.port=8015
spring.application.name=xyz-comsumer
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.register=false
spring.cloud.consul.discovery.health-check-url=/actuator/health
spring.cloud.consul.discovery.heartbeat.enabled=true
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
(3)测试方法
获取所有注册的服务,从注册的服务中选取一个,服务调用
package com.xyz.comsumer.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @RestController
public class HelloController {
@Autowired
private LoadBalancerClient loadBalancer;
@Autowired
private DiscoveryClient discoveryClient;
private String serviceName = "service-provider"; @RequestMapping("/services")
public Object services() {
return discoveryClient.getInstances(serviceName);
} @RequestMapping("/discover")
public Object discover() {
return loadBalancer.choose(serviceName).getUri().toString();
} @RequestMapping("/hello")
public String hello() {
ServiceInstance serviceInstance = loadBalancer.choose(serviceName);
String callServiceResult = new RestTemplate().getForObject(serviceInstance.getUri().toString() + "/hello", String.class);
return callServiceResult;
}
}
注:
客户端调用的服务名,是在服务端指定的,在服务端配置里使用 spring.cloud.consul.discovery.service-name指注册到 Consul 的服务名称
测试
启动Consul
启动provider和provider1
启动comsumer
测试地址 http://localhost:8015/services
返回结果
[
{
"instanceId": "provider-8010",
"serviceId": "service-provider",
"host": "hkgi-PC",
"port": 8010,
"secure": false,
"metadata": {
"secure": "false"
},
"uri": "http://hkgi-PC:8010",
"scheme": null
},
{
"instanceId": "provider-8011",
"serviceId": "service-provider",
"host": "hkgi-PC",
"port": 8011,
"secure": false,
"metadata": {
"secure": "false"
},
"uri": "http://hkgi-PC:8011",
"scheme": null
}
]
测试地址 http://localhost:8015/discover
返回结果
http://hkgi-PC:8011 或 http://hkgi-PC:8011
测试地址 http://localhost:8015/hello
返回结果
hello,provider 或 hello,another provider
注:
结果交替出现的,这是因为负载均衡器是采用的是轮询的方式
说明:
调用的过程:
A.通过LoadBalancerClient查询服务
B.通过RestTemplate调用远程服务
Ribbon负载均衡策略
BestAvailableRule
AvailabilityFilteringRule
WeightedResponseTimeRule
RetryRule
RoundRobinRule
RandomRule
ZoneAvoidanceRule
自定义Ribbon负载均衡——使用随机访问策略
修改启动类
package com.xyz.comsumer; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
public class ComsumerApplication { public static void main(String[] args) {
SpringApplication.run(ComsumerApplication.class, args);
} @Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
} }
服务调用
package com.xyz.comsumer.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @RestController
public class RibbonHelloController { @Autowired
private RestTemplate restTemplate;
private String serviceName = "service-provider"; @RequestMapping("/ribbon/hello")
public String hello() {
String callServiceResult = restTemplate.getForObject("http://"+serviceName+"/hello", String.class);
return callServiceResult;
}
}
配置添加
service-provider.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
重新启动 comsumer
测试地址 http://localhost:8015/ribbon/hello
输出的结果不再是交替出现,改为随机的了
spring boot2X整合Consul一使用RestTemplate实现服务调用的更多相关文章
- spring boot2X整合nacos一使用Feign实现服务调用
服务调用有两种方式: A.使用RestTemplate 进行服务调用 查看 B.使用Feign 进行声明式服务调用 上一次写了使用RestTemplate的方式,这次使用Feign的方式实现 服务注册 ...
- spring boot2X整合Consul一服务注册与发现
Consul 是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置. 关键特性: 服务注册/发现 数据强一致性保证 多数据中心 健康检查 key/value存储 1.下载 htt ...
- 使用RestTemplate进行服务调用的几种方式
首先我们在名为MSG的服务中定义一个简单的方法 @RestController public class ServerController { @GetMapping("/msg" ...
- VUE开发(一)Spring Boot整合Vue并实现前后端贯穿调用
文章更新时间:2020/03/14 一.前言 作为一个后端程序员,前端知识多少还是要了解一些的,vue能很好的实现前后端分离,且更便于我们日常中的调试,还具备了轻量.低侵入性的特点,所以我觉得是很有必 ...
- Spring Cloud项目中通过Feign进行内部服务调用发生401\407错误无返回信息的问题
问题描述 最近在使用Spring Cloud改造现有服务的工作中,在内部服务的调用方式上选择了Feign组件,由于服务与服务之间有权限控制,发现通过Feign来进行调用时如果发生了401.407错误时 ...
- Spring Cloud(十二)声名式服务调用:Feign 的使用(下)
前言 本文是对上一篇博文的扩充,很多平时用不到的特性就开始简略一写,Spring Cloud各版本之间的差距很大的,用不到的可能下一个版本就被kill掉了.由于笔者写本文开始的时候误解了Feign的继 ...
- Spring Cloud Feign 声明式服务调用
目录 一.Feign是什么? 二.Feign的快速搭建 三.Feign的几种姿态 参数绑定 继承特性 四.其他配置 Ribbon 配置 Hystrix 配置 一.Feign是什么? 通过对前面Sp ...
- Spring Cloud Ribbon---微服务调用和客户端负载均衡
前面分析了Eureka的使用,作为服务注册中心,Eureka 分为 Server 端和 Client 端,Client 端作为服务的提供者,将自己注册到 Server 端,Client端高可用的方式是 ...
- SpringBoot系列十一:SpringBoot整合Restful架构(使用 RestTemplate 模版实现 Rest 服务调用、Swagger 集成、动态修改日志级别)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot整合Restful架构 2.背景 Spring 与 Restful 整合才是微架构的核心,虽然在整 ...
随机推荐
- layui 日期插件一闪而过
关于一个layui插件日期的问题,在本地调试都是可以的,但发布到服务器上的时候,日期插件一闪而过,后来我以为是各个插件之间的冲突,我就每个插件的排除,但是还是无动于衷,然后我就去官网看了下是,需要加一 ...
- C# 通过反射获取winform上的控件
比如获取Button按钮: System.Reflection.FieldInfo[] fieldInfo = form.GetType().GetFields(System.Reflection.B ...
- MVC里模型常用的一些操作
学习也是做买卖,归根到底还是学习成本的问题. 下面把微软集合类型的增删改查稍微罗列一下,大家看看它能带来的便利,和你要学的新东西,还有风险(纯粹的数据操作,不用框架,风险其实不大)相比,是否值得.来决 ...
- 1-RocketMq 学习 中文文档(一)
原文:https://blog.csdn.net/weixin_40533111/article/details/84451096 1.基本概念及优势 rocketmq是一个基于发布订阅队列模型的消息 ...
- EntityFramework 基类重写
/* * ------------------------------------------------------------------------------ * * 创 建 者:F_Gang ...
- FileZilla_Server:425 Can't open data connection 问题解决
25 Can't open data connection 和 读取目录列表失败 问题解决 这个问题主要是由于使用Passive Mode模式造成的,解决这个问题很简单:1.在ftp服务软件中设置指定 ...
- 关于Jackson中JsonNode的取值asText()和textValue()区别
在 比较高版本的Jackson 中, 包名为 com.fasterxml.jackson String jsonText="{\"name\":\"张三\&qu ...
- django 基础进阶ORM 2
1.多表操作 添加记录: 针对一对多 book_obj=Book.objects.create(title="python葵花宝典",price=100,publishDate=& ...
- 第12节-BLE协议HCI层的数据格式
学习资料: 1. 蓝牙协议core_v5.0.pdf <Vol 2: Core System Package [BR/EDR Controller volume]>的“Part E: Ho ...
- 转: angularjs select 赋值 ng-options配置方式
摘自: http://blog.csdn.net/chwshuang/article/details/53861249 数组方式 数据是数组 $scope.years = [2014, 2015, 2 ...