调用Consul服务(消费服务)
调用Consul服务(消费服务)
依赖项
在spring-cloud-consul-client项目中添加依赖项,POM文件内容中添加如下依赖项:
<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-web</artifactId>
</dependency>
spring-cloud-starter-consul-discovery为Consul提供支持,spring-cloud-starter-openfeign为HTTP请求提供Feign风格的调用,spring-boot-starter-web仅仅为了使用HTTP MVC测试方便。
配置信息
spring.application.name=spring-cloud-consul-client
server.port=9002
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
#设置不需要注册到 consul 中
spring.cloud.consul.discovery.register=false
本测试不用把自己注册到服务中心里:spring.cloud.consul.discovery.register=false,所以在启动类里也不用声明@EnableDiscoveryClient注解。
远程服务调用接口
@FeignClient(name= "service-provider")
public interface ServiceProviderRemote {
@RequestMapping("/hello")
public String Hello(@RequestParam String name);
}
使用openfeign调用远程服务接口,openfeign是Spring封装后的Feign,本项目中openfeign需要使用spring-cloud-starter-consul-discovery。
测试消费服务接口
@RestController
public class TestConsul {
@Autowired
ServiceProviderRemote remote;
@RequestMapping("/TestHello")
public String TestHello(){
String first = remote.Hello("first-SWS");
String second = remote.Hello("second-SWS");
return first + " | " + second;
}
@RequestMapping("/Test")
public String Test(){
return "OK";
}
}
为了测试负载均衡所以调用两次服务接口。
启动类
@SpringBootApplication
@EnableFeignClients
public class SpringCloudConsulClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConsulClientApplication.class, args);
}
}
在启动类里需要使用@EnableFeignClients注解启动openfeign。
启动项目测试
访问http://127.0.0.1:9002/TestHello查看结果:

源码
Github仓库:https://github.com/sunweisheng/spring-cloud-example
调用Consul服务(消费服务)的更多相关文章
- 2-5 利用RestTemplateCore简化调用Consul中的服务
1.必须要安装RestTemplateCore包 2.请求服务,必须要知道 a Consul服务器的地址:b 请求的服务名 ;c 具体请求的api接口 利用RestTemplateCore简化调用Co ...
- .net core grpc consul 实现服务注册 服务发现 负载均衡(二)
在上一篇 .net core grpc 实现通信(一) 中,我们实现的grpc通信在.net core中的可行性,但要在微服务中真正使用,还缺少 服务注册,服务发现及负载均衡等,本篇我们将在 .net ...
- .Net Core Grpc Consul 实现服务注册 服务发现 负载均衡
本文是基于..net core grpc consul 实现服务注册 服务发现 负载均衡(二)的,很多内容是直接复制过来的,..net core grpc consul 实现服务注册 服务发现 负载均 ...
- dubbo注册服务和消费服务---入门篇
本文介绍如何用dubbo+zk来实现一个注册服务 + 消费服务的入门小demo 需要环境:zk服务器 两个maven项目,一个负责提供服务,一个负责消费服务. dubbo-service 服务端 po ...
- Dubbo学习笔记4:服务消费端泛化调用与异步调用
本文借用dubbo.learn的Dubbo API方式来解释原理. 服务消费端泛化调用 前面我们讲解到,基于Spring和基于Dubbo API方式搭建简单的分布式系统时,服务消费端引入了一个SDK二 ...
- 一、springcloud服务注册、发现、调用(consul/eureka)
1.Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全 ...
- Spring Cloud Feign 服务消费调用(三)
序言 Spring Cloud Netflix的微服务都是以HTTP接口的形式暴露的,所以可以用Apache的HttpClient或Spring的RestTemplate去调用 而Feign是一个使用 ...
- Spring Cloud Alibaba基础教程:支持的几种服务消费方式(RestTemplate、WebClient、Feign)
通过<Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现>一文的学习,我们已经学会如何使用Nacos来实现服务的注册与发现,同时也介绍如何通过LoadBal ...
- Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十九):服务消费(Ribbon、Feign)
技术背景 上一篇教程中,我们利用Consul注册中心,实现了服务的注册和发现功能,这一篇我们来聊聊服务的调用.单体应用中,代码可以直接依赖,在代码中直接调用即可,但在微服务架构是分布式架构,服务都运行 ...
随机推荐
- JavaScript 各种遍历方式详解及总结
JavaScript 各种遍历方式详解 在$.each中想要终止循环,但是它没有continue或者break这样的终止方式,所以尝试使用return来进行终止,但是发现并没有跳出循环.为了搞清楚js ...
- Android手机、电视(盒子) 打开ADB调试 一览表
手机.电视(盒子) 打开ADB调试 一览表 一.手机打开ADB调试方法 序号 名称 描述 方式 1 华为手机 EMUI 1.设置 ->关于手机-> 版本号 点击(4~5次)2.返回设置 - ...
- BUUCTF--easyer
测试文件下载:https://buuoj.cn/files/b66a080016da04abfc002a336c0132e5/easyre.zip?token=eyJ0ZWFtX2lkIjpudWxs ...
- 求解:为什么impala实现hive查询 可以使用ifnull()函数,不可以使用length() 函数
求大神解惑,找了很久都没有找到为什么??? hive支持length() 函数,不支持ifnull()函数??? impala实现hive查询 支持ifnull()函数,不支持length() 函数 ...
- 上传.cgi在252板子上跑
1.windows下写好.c程序 2.进入linux,准备交叉编译 arm-hisiv600-linux-gcc -g -Wall -o xxx.cgi xxx.c(交叉编译工具取决于板子) 3.把. ...
- ssh-add - 向认证代理添加 RSA 或 DSA 身份数据
总览 (SYNOPSIS) ssh-add [-lLdDx ] [-t life ] [file ... ] ssh-add -s reader ssh-add -e reader 描述 (DESCR ...
- Win10电脑查看已连接过WiFi密码的命令
运行中输入CMD,回车,打开命令行窗口. 输入:netsh wlan show profiles 执行后,会列出搜友已连接过的WiFi名字: 输入:netsh wlan show profile ...
- Django 项目环境搭建
环境 mkvirtualenv -p python3.6 mytest # 创建虚拟环境 workon mytest # 进入虚拟环境 # 按照基础环境依赖 pip install django==1 ...
- BZOJ2280 [Poi2011]Plot 二分+倍增+最小圆覆盖
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2280 https://loj.ac/problem/2159 题解 显然对于一段的 \(q_i ...
- poj 3468: A Simple Problem with Integers (树状数组区间更新)
题目链接: http://poj.org/problem?id=3468 题目是对一个数组,支持两种操作 操作C:对下标从a到b的每个元素,值增加c: 操作Q:对求下标从a到b的元素值之和. 这道题也 ...