首页 > 其他分享 >Spring Cloud Consul 入门指引

Spring Cloud Consul 入门指引

时间:2022-10-04 11:44:44浏览次数:66  
标签:Spring Consul 应用程序 Cloud spring 我们 cloud

1 概述

Spring Cloud Consul 项目为 Spring Boot 应用程序提供了与 Consul 的轻松集成。

Consul 是一个工具,它提供组件来解决微服务架构中一些最常见的挑战:

  • 服务发现——自动注册和注销服务实例的网络位置
  • 健康检查——检测服务实例何时启动并运行
  • 分布式配置——确保所有服务实例使用相同的配置

在本文中,我们将了解如何配置 Spring Boot 应用程序以使用这些功能。

2 前提条件

首先,建议快速浏览 Consul 及其所有功能。

在本文中,我们将使用在 localhost:8500 上运行的 Consul 代理。有关如何安装 Consul 和运行代理的更多详细信息,请参阅此 链接

首先,我们需要添加 [spring-cloud-starter-consul-all](https://search.maven.org/classic/#search|ga|1|a%3A"spring-cloud-starter- consul-all%22) 的 pom.xml 的依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-all</artifactId>
    <version>3.1.1</version>
</dependency>

3 服务发现

让我们编写我们的第一个 Spring Boot 应用程序并连接正在运行的 Consul 代理:

@SpringBootApplication
public class ServiceDiscoveryApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(ServiceDiscoveryApplication.class)
          .web(true).run(args);
    }
}

默认情况下,Spring Boot 将尝试连接到 localhost:8500 的 Consul 代理。 要使用其他设置,我们需要更新 application.yml 文件:

spring:
  cloud:
    consul:
      host: localhost
      port: 8500

然后,如果我们在浏览器中访问 Consul 代理的站点 http://localhost:8500 ,我们将看到我们的应用程序已在 Consul 中正确注册,标识符来自 "${spring.application.name}: ${用逗号分隔的配置文件}

标签:Spring,Consul,应用程序,Cloud,spring,我们,cloud
From: https://www.cnblogs.com/hunterzhang/p/16753508.html

相关文章

  • springboot项目 报错No mapping for GET /css/bootstrap.css,前端无法展示样式
    说来也奇怪,前几天刚写完的项目写的好好的现在打开他就加载不了前端的静态资源了报错NomappingforGET/css/bootstrap.css解决方法:新建一个配置类,将静态资源的路径......
  • Spring事务(二)-事务传播行为
    在Spring里,一个事务方法被另外一个事务方法调用时,两个方法的事务应该如何进行,说白话一点,就是说当出现异常需要回滚时,各个方法的数据操作是否要全部回滚,事务传播行为就......
  • Spring事务(四)-事务隔离级别
    Spring @Transactional注解isolation属性@Transactional注解通过isolation属性设置事务隔离级别。如下:@Transactional(isolation=Isolation.DEFAULT)publicvoid......
  • Springboot接收请求参数示例
    packagecom.example.demo.controller;importcom.example.demo.model.User;importorg.springframework.web.bind.annotation.*;importorg.springframework.web.mult......
  • SpringBoot运行报错
    在SpringBoot启动时报错发现自己写的测试类这个Mapper报错但是Mapper包里面与以前写的一样但是在SPringBoot里面这个Mapper必须要打上注解才能运行......
  • Spring事务(一)-事务配置
      事务是数据库操作最基本的单元,是逻辑上的一组操作,这一组操作在同一个会话中要么都执行成功,要么都失败,这也是事务的最基本特性--原子性。事务的作用是为了保证系统数据......
  • 如何打通 SAP Cloud for Customer 系统和微信公众号的双向消息通信功能
    本系列的前三篇文章,我们依次介绍了微信公众号开发环境的搭建,微信公众平台API的调用,以及地图功能的集成。本文作为该系列第四篇文章,介绍如何实现SAPCloudforCustomer......
  • 定时任务quartz与spring的集成
     我想要在spring的集成框架中使用spring,暂时采用quartz 根据下面的几篇博客实现了(懒得说了,直接丢链接):Quartz实现动态定时任务​​Spring3整合Quartz2实现定时任务二:......
  • spring
    依赖查找:BeanFactorybeanFactory=newClassPathXmlApplicationContext("basic_di/inject-set.xml");Personperson=beanFactory.getBean(Person.class);根据ty......
  • 【Spring boot】自动配置的开启原理
    本文结论源码使用springboot2.6.6版本开始自动配置的核心注解:@EnableAutoConfiguration@EnableAutoConfiguration中使用了@Import(AutoConfigurationImportSelector.c......