欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringCloud入门(十六) ------ Springcloud 集成 consul

程序员文章站 2022-06-12 22:53:33
...
虽然Eureka还可以用,但是官网已经发出了停更公告,所以可以使用其他的注册中心(Zookeeper,Consul等)

安装Consul:

官网下载:

https://www.consul.io/

安装后启动(cmd):

SpringCloud入门(十六) ------ Springcloud 集成 consul
命令行窗口:consul agent -dev
SpringCloud入门(十六) ------ Springcloud 集成 consul
出现running 运行成功
浏览器rul输入: http://localhost:8500/ui/dc1/services
SpringCloud入门(十六) ------ Springcloud 集成 consul

SpringCloud 集成 consul 实现服务注册:

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

完整的依赖:

    <dependencies>
        <dependency>
            <groupId>com.jym.springCloud</groupId>
            <artifactId>jym-common-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId></dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

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

    </dependencies>
yml:
server:
  port: 8005

spring:
  application:
    name: jym-provider-payment  # 微服务名称
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}

启动类:
package com.jym.springCloud;

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

/**
 * @program: jym-springcloud-parent
 * @description: 启动类
 * @author: jym
 * @create: 2020/03/13
 */
@SpringBootApplication
@EnableDiscoveryClient
public class JymPaymentApplication8005 {
    public static void main(String[] args) {
        SpringApplication.run(JymPaymentApplication8005.class,args);
    }
}

启动之后查看consul界面:
SpringCloud入门(十六) ------ Springcloud 集成 consul

调用端与服务端的步骤一致

学习年限不足,知识过浅,说的不对请见谅。

世界上有10种人,一种是懂二进制的,一种是不懂二进制的。

相关标签: springcloud