javaweb -注册中心 consul简单使用

第一步:去官网下载consul 然后解压如图:

image.png

第二步:进入D:\Develop\consul 打开cmd 输入:consul agent -dev 启动consul服务 如图:

image.png

第三步:浏览器输入 localhost:8500 打开consul 管理平台 如图:

image.png

第四步: 创建springboot项目:结构目录如图:

image.png

根目录 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <modules>
        <module>cloud_client</module>
        <module>cloud_service</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.amn</groupId>
    <artifactId>cloud_demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>cloud_demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.5.0</version>
            <scope>test</scope>
        </dependency>

        <!--健康检查 要的配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--spring-cloud-starter-consul-discovery 启动器-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            <version>2.2.2.RELEASE</version>

        </dependency>
        <!--&lt;!&ndash;s使用 Consul 配置信息时需要引入 spring-cloud-starter-consul-config 依赖&ndash;&gt;-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-config</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

cloud-service 目录 application.properties

server.port=8506
spring:
spring.application.name=consul-producer

    cloud:
        consul:
            host:127.0.0.1
            port:8500

        discovery:
            #注册到consul的服务名称
            serviceName:consul-service
            spring.cloud.consul.discovery.instance-id=${spring.application.name}:${server.port}

注释:上述配置是 将自身服务注册到 consul 中

cloud-client目录下 application.properties

server.port=8504
spring:
spring.application.name=consul-client
    cloud:
        consul:
            host:127.0.0.1
            port:8500
        discovery:
            #注册到consul的服务名称
#            serviceName:consul-service
#            spring.cloud.consul.discovery.instance-id=${spring.application.name}:${server.port}
#            healthCheckPath: /hello
#            healthCheckInterval:10s
#            prefer-ip-address:true
            #设置不需要注册到 consul 中
            register:false
#

注释

server.port:8504 是自身服务的端口 8500 是consul服务的默认端口 spring.application.name=consul-client是自身服务的名称 当要获取consul注册过的服务时就用这个来

根目录启动项:

@SpringBootApplication
public class CloudDemoApplication {
   // 添加了 @EnableDiscoveryClient 注解表示支持服务发现。
    public static void main(String[] args) {
        SpringApplication.run(CloudDemoApplication.class, args);
    }

}

cloud-service目录启动项:

@SpringBootApplication
@EnableDiscoveryClient
public class CloudServiceApplication {
   // 添加了 @EnableDiscoveryClient 注解表示支持服务发现。
    public static void main(String[] args) {
        SpringApplication.run(CloudServiceApplication.class, args);
    }

}

cloud-client目录启动项:

@SpringBootApplication
@EnableDiscoveryClient
public class CloudClientApplication {
    //consul agent -dev 启动consul 服务 命令   打开consul管理平台   localhost:8500
    public static void main(String[] args) {
        SpringApplication.run(CloudClientApplication.class, args);
    }
}

第四步: 分别启动cloud-service cloud-client 启动完成后打开consul 管理平台如图:

image.png

image.png

第五步: 在cloud-client中 写一个获取consul 服务的contraller


@RestController
@RequestMapping
public class ClientContraller {

    @Autowired
    private DiscoveryClient discoveryClient;
    
    /**
     * 获取consul 中注册得服务
    * @return
     */
    @RequestMapping("/service")
    public  Object getServiceFromConsul(){
        List<ServiceInstance> instances = discoveryClient.getInstances("consul-producer");
        return instances;
    }
}

第六步:调用我们写的接口 :

image.png

总结:consul 中心注册原理

1、 cloud-service 启动的时候,会向 Consul 发送一个 post 请求,告诉 Consul 自己的 IP 和 Port 。Consul 接收到 cloud-service的注册后,每隔10s(默认)会向 cloud-service发送一个健康检查的请求,检验cloud-service是否健康。
2、当 clooud_client发送 GET 方式请求 /api/address 到 cloud-service时,会先从 Consul 中拿到一个存储服务 IP 和 Port 的临时表,从表中拿到 cloud-service 的 IP 和 Port 后再发送 GET 方式请求 /api/address。
3、该临时表每隔10s会更新,只包含有通过了健康检查的cloud-service

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。