springcloud学习笔记三:springcloud consul

本文详细介绍了如何使用Consul作为SpringCloud的服务注册与发现组件。从下载安装到配置应用,包括添加依赖、启用发现服务、配置健康检查路径及间隔,再到将其作为配置中心的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

consul在springcloud作为服务注册,服务发现的作用,consul服务不需要我们用代码实现,可以去官网上下载,安装即可使用

下载地址为:https://www.consul.io/downloads.html

我使用的window版本的consul,使用命令consul agent -dev ,启动consul server,

1、服务注册

1、pom添加consul依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

2、启动程序添加@EnableDiscoveryClient注解

@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudStuApplication {
   public static void main(String[] args) {
      SpringApplication.run(SpringCloudStuApplication.class, args);
   }
}

3、配置application.yml文件

spring:
   application:
      name: yc_consul
   cloud:
      consul:
         host: localhost
         port: 8500
         discovery:
           register: true
           healthCheckPath: /test/hello
           healthCheckInterval: 10s

 其中healthCheckPath,healthCheckInterval是作为健康检测的,这个就是每10s去请求/test/hello这个服务

@RestController
@RequestMapping("/test")
public class ZuulTestController {

    @RequestMapping("/hello")
    public String sayHello(){
        System.out.println("-------------------health------check---------------");
        return "hello";
    }

}

consul默认的健康检测服务是/health

4、consul作为资源配置中心

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值