调用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服务(消费服务)的更多相关文章

  1. 2-5 利用RestTemplateCore简化调用Consul中的服务

    1.必须要安装RestTemplateCore包 2.请求服务,必须要知道 a Consul服务器的地址:b 请求的服务名 ;c 具体请求的api接口 利用RestTemplateCore简化调用Co ...

  2. .net core grpc consul 实现服务注册 服务发现 负载均衡(二)

    在上一篇 .net core grpc 实现通信(一) 中,我们实现的grpc通信在.net core中的可行性,但要在微服务中真正使用,还缺少 服务注册,服务发现及负载均衡等,本篇我们将在 .net ...

  3. .Net Core Grpc Consul 实现服务注册 服务发现 负载均衡

    本文是基于..net core grpc consul 实现服务注册 服务发现 负载均衡(二)的,很多内容是直接复制过来的,..net core grpc consul 实现服务注册 服务发现 负载均 ...

  4. dubbo注册服务和消费服务---入门篇

    本文介绍如何用dubbo+zk来实现一个注册服务 + 消费服务的入门小demo 需要环境:zk服务器 两个maven项目,一个负责提供服务,一个负责消费服务. dubbo-service 服务端 po ...

  5. Dubbo学习笔记4:服务消费端泛化调用与异步调用

    本文借用dubbo.learn的Dubbo API方式来解释原理. 服务消费端泛化调用 前面我们讲解到,基于Spring和基于Dubbo API方式搭建简单的分布式系统时,服务消费端引入了一个SDK二 ...

  6. 一、springcloud服务注册、发现、调用(consul/eureka)

    1.Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全 ...

  7. Spring Cloud Feign 服务消费调用(三)

    序言 Spring Cloud Netflix的微服务都是以HTTP接口的形式暴露的,所以可以用Apache的HttpClient或Spring的RestTemplate去调用 而Feign是一个使用 ...

  8. Spring Cloud Alibaba基础教程:支持的几种服务消费方式(RestTemplate、WebClient、Feign)

    通过<Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现>一文的学习,我们已经学会如何使用Nacos来实现服务的注册与发现,同时也介绍如何通过LoadBal ...

  9. Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十九):服务消费(Ribbon、Feign)

    技术背景 上一篇教程中,我们利用Consul注册中心,实现了服务的注册和发现功能,这一篇我们来聊聊服务的调用.单体应用中,代码可以直接依赖,在代码中直接调用即可,但在微服务架构是分布式架构,服务都运行 ...

随机推荐

  1. git stash 后"本地代码不见了"

    git stash 当本地代码不想提交覆盖,又忙于其他分支,可以先储存起来. git stash命令的作用就是将目前还不想提交的但是已经修改的内容进行保存至堆栈中,后续可以在某个分支上恢复出堆栈中的内 ...

  2. 77.LRU Cache(最近最久未使用算法)

    Level:   Hard 题目描述: Design and implement a data structure for Least Recently Used (LRU) cache. It sh ...

  3. 分析abex'crackme#2

    文件地址:https://www.wocloud.com.cn/webclient/share/sindex.action?id=i9K_Br6TgE4gbyGIlYkffWKcRy5TUdZ8U6_ ...

  4. 1.报表TIBCO Jaspersoft Studio工具教程入门--生成jrxml和jasper文件 然后拖拽到项目中 跟ireport一样

    转自:https://blog.csdn.net/KingSea168/article/details/42553781 2. 在接下来的教程中,我们将实现一个简单的JasperReports示例,展 ...

  5. Python中字符串的格式化

    字符串的格式化 格式化是对字符串进行格式表示的方式.使用槽({})按顺序格式化字符串. 格式化方式 <模板字符串>.format(<逗号分割参数>) "{ }:计算机 ...

  6. Java疯狂讲义笔记——Lambda表达式

    Java8新增的Lambda表达式 [特性]支持将代码块作为方法参数,Lambda表达式允许使用更简洁的代码来创建只有一个抽象方法的接口(这种接口被称为函数式接口)的实例. [组成部分]1,形参列表 ...

  7. 初学Java 求最大公约数

    import java.util.Scanner; public class GreatesCommonDivisor { public static void main(String[] args) ...

  8. 【串线篇】Mybatis缓存简介

    缓存:暂时的存储一些数据:加快系统的查询速度... CPU: 主频:4-2.7GHZ 内存:4G-8G    1333MHZ    2166MHZ CPU:一级缓存(4MB):二级缓存 (16MB); ...

  9. day15 python lambda函数 递归函数 二分法

    day15 python   一.匿名函数 lambda     1.lambda函数 def func(n):                #普通函数, 功能比较简单, 当函数多的时候, 起名也不 ...

  10. ruby语法之方法

    ruby中的方法相当于python的函数 其定义规则为: 方法名应以小写字母开头.如果您以大写字母作为方法名的开头,Ruby 可能会把它当作常量,从而导致不正确地解析调用. 方法应在调用之前定义,否则 ...