【Consul教程】2.SpringBoot整合Consul搭建服务注册与发现

前言

上篇讲到了consul的安装,这篇主要SpringBoot 和consul整合,springcloud 的eureka已经不更新了,可以过渡到consul。

整合

consul的注册中心是安装的服务器上,只需要搭建注册客户端就行了。

pom.xml

	<parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.4.2version>
    parent>
	<dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-consul-discoveryartifactId>
        dependency>
    dependencies>
    <dependencyManagement>
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Greenwich.SR3version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

启动类App.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * consul 服务注册客户端
 */
@EnableDiscoveryClient
@SpringBootApplication
public class App {
    
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

配置文件:

spring:
  application:
    name: consul-client
  cloud:
    # consul 注册中心地址
    consul:
      host: 192.168.126.156
      port: 8500
      discovery:
        # 服务名称
        service-name: ${spring.application.name}
        heartbeat:
          enabled: true

测试

访问Consul管理页:http://192.168.126.156:8500/

【Consul教程】2.SpringBoot整合Consul搭建服务注册与发现_第1张图片

注册成功

你可能感兴趣的:(consul,spring,boot,consul,spring,cloud)