调用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注册中心,实现了服务的注册和发现功能,这一篇我们来聊聊服务的调用.单体应用中,代码可以直接依赖,在代码中直接调用即可,但在微服务架构是分布式架构,服务都运行 ...
随机推荐
- window下 nginx 80端口被占用
问题:启动nginx没有反应,查看日志提示 bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in ...
- js中Date的构造函数解读
javascript中的内置对象是我们经常会用到的,那么今天我们就来说说Date的四种构造方法吧 一.new Date() 这是我们最常使用也最熟悉不过的Date对象的构造方法了,通过无参数的构造函数 ...
- CSS样式表能否控制文字禁止选择,复制, 焦点
div中禁止文字被选择 在做div的点击计数事件时,遇到一个小问题. 因为div里面有文字,所以当点击多次时,特别是鼠标点的比较快的时候,文字会被选中. 查了下,用css和javascript可以实现 ...
- vue,一路走来(9)--聊天窗口
闲暇时间,介绍一下我做一个聊天窗口的心得.如图: 首先要考虑的是得判断出是自己的信息还是对方发来的信息,给出如图的布局,切换不同的类. <li class="clearfix" ...
- Vue--事件处理(逐个学习事件修饰符)
.capture .self .once 主要学习这三个事件修饰符的用法先来看看capture capture即是给元素添加一个监听器,当元素发生冒泡时,先触发带有该修饰符的元素.若有多个该修饰符,则 ...
- 【串线篇】Mybatis之SSM整合
SSM:Spring+SpringMVC+MyBatis 建立Java web项目 一.导包 1).Spring: [aop核心] com.springsource.net.sf.cglib-2.2. ...
- P4707 重返现世 扩展 MinMax 容斥+DP
题目传送门 https://www.luogu.org/problem/P4707 题解 很容易想到这是一个 MinMax 容斥的题目. 设每一个物品被收集的时间为 \(t_i\),那么集齐 \(k\ ...
- Hibernate入门核心配置文件和orm元数据配置文件详解
框架是什么? 框架是用来提高开发效率的 封装了一些功能,我们需要使用这些功能时,调用即可,不用手动实现 所以框架可以理解为一个半成品的项目,只要懂得如何使用这些功能即可 Hibernate是完全面向对 ...
- Python中的"Special Method"
The first thing to know about special methods is that they are meant to be called by the Python inte ...
- map简单用法
let familyNames = []; familyNames = res.Data.map(item=> { return item.ArgText });