Consul安装启动

Posted 我也有过梦想呀

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Consul安装启动相关的知识,希望对你有一定的参考价值。

1.安装

sudo yum install -y yum-utils

sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo

sudo yum -y install consul

检查是版本号:consul --version

2.启动

consul agent -dev -ui -node=consul-dev -client=(主机ip,便于访问)

3.创建一个maven项目

4.导入相关pom文件

 <dependencies>

        <!--引入通用的api-commons.jar包-->
        <dependency>
            <groupId>com.qbb.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


    </dependencies>

5.修改yml文件

server:
  port: 8006


spring:
  application:
    name: consul-consumer-order
  cloud:
    consul:
      host: 192.168.140.132 # 使用的linux服务ip
      port: 8500 # 默认端口
      discovery:
        service-name: ${spring.application.name}

6.写主启动类

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

7.写controller层测试

@RestController
@Slf4j
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;

    @RequestMapping("/payment/consul")
    public String paymentZk() {
        return "springcloud with consul:" + serverPort + "\\t" + UUID.randomUUID().toString() + System.currentTimeMillis();
    }
}

结果

以上是关于Consul安装启动的主要内容,如果未能解决你的问题,请参考以下文章

Consul安装启动

Consul 安装的与启动

阿里云linux安装Consul启动

安装Consul服务中心

consul 使用

consul