Installing Consul 1.11.1 on CentOS 8.2 for Service Discovery
Consul Installation on CentOS
To install Consul on CentOS 8.2, first add the HashiCorp repository:
yum install -y yum-utils
yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
yum -y install consul
Service Cofniguration
Create or modify the systemd service file at /etc/systemd/system/consul.service:
[Unit]
Description="HashiCorp Consul Service Mesh"
Documentation=https://www.consul.io/
Requires=network-online.target
After=network-online.target
ConditionFileNotEmpty=/etc/consul.d/consul.hcl
[Service]
EnvironmentFile=/etc/consul.d/consul.env
User=root
Group=root
ExecStart=/usr/bin/consul agent -config-dir=/etc/consul.d/
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
Consul Server Configuration
Create a JSON configuration file at /etc/consul.d/server_config.json:
{
"datacenter": "primary-dc",
"data_dir": "/var/lib/consul",
"log_level": "INFO",
"node_name": "server-node",
"server": true,
"ports": {
"http": 8500,
"dns": 8600,
"serf_lan": 8001,
"serf_wan": 8002,
"server": 8003
}
}
Modify /etc/consul.d/consul.hcl with these settings:
client_addr = "0.0.0.0"
ui_config {
enabled = true
}
bind_addr = "0.0.0.0"
advertise_addr = "127.0.0.1"
bootstrap_expect = 1
Spring Boot Integration
For Spring Boot integration, add these dependencies to your pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Configure bootstrap.yml:
spring:
cloud:
consul:
host: YOUR_SERVER_IP
port: 8500
discovery:
health-check-path: /actuator/health
health-check-interval: 5s
instance-id: ${spring.application.name}:${server.port}