近来公司需要研究consul作为技术储备,本人研究了一下,所谓好记性不如烂笔头子,所以在此记录一下,也希望能帮助到需要的小伙伴,因本人技术能力有限,文章可能有不足或者错误之处,请大神指摘,好及时更改,及时刷新自己的认知。
由于集群和负载均衡需要多台服务器测试,公司临时也没有多余服务器,所以用了虚拟机。 本文使用了两台虚拟linux作为springboot业务服务,当然你也可以使用docker。
物理机:Mac OS X 10.13.3
虚拟机软件:VMware-Fusion-10.1.1
虚拟机操作系统:CentOS-7
开发工具:IntelliJ IDEA 2017.2.7
下面我们正式开始
工具 | 下载地址 | 本文使用版本 |
consul | https://www.consul.io/downloads.html | consul_1.0.7_linux_amd64.zip |
consul-template | https://releases.hashicorp.com/consul-template/ | consul-template_0.19.4_linux_amd64.zip |
nginx | http://nginx.org/en/download.html | nginx-1.12.2.tar.g |
IP | 功能 | 系统 |
192.168.188.143 | consul-template, nigix | CentOS-7 |
192.168.188.182 | consul | CentOS-7 |
192.168.188.71 | consul-agent, webapp | CentOS-7 |
192.168.188.185 | consul-agent, webapp | CentOS-7 |
[gerrard@consul ~]#unzip consul_1.0.7_linux_amd64.zip
[gerrard@consul ~]#mv consul /usr/bin/
[gerrard@consul ~]#consul agent -server -bootstrap -ui -bind 192.168.188.182 -client 192.168.188.182 -data-dir /home/gerrard/tmp/consul -node=gerrard_server01
[gerrard@consul ~]#nohup consul agent -server -bootstrap -ui -bind 192.168.188.182 -client 192.168.188.182 -data-dir /home/gerrard/tmp/consul -node=gerrard_server01 &
[gerrard@consul ~]#consul agent -data-dir /home/gerrard/tmp/consul_agent1 -node=gerrard_agent1 -bind=192.168.188.71 -join=192.168.188.182
[gerrard@consul ~]#consul agent -data-dir /home/gerrard/tmp/consul_agent2 -node=gerrard_agent2 -bind=192.168.185.185 -join=192.168.188.182
pom.xml
4.0.0
com.gerrard
cloud-consul
0.0.1-SNAPSHOT
jar
cloud-consul
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
UTF-8
UTF-8
1.8
Finchley.RC1
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-consul-discovery
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
application.properties
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.health-check-interval=10s
spring.cloud.consul.discovery.instance-id=spring-boot-consul
spring.cloud.consul.discovery.tags=test
spring.application.name=spring-boot-consul
server.port=8080
CloudConsulApplication.class
package com.gerrard.cloudconsul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class CloudConsulApplication {
public static void main(String[] args) {
SpringApplication.run(CloudConsulApplication.class, args);
}
}
HelloWorld.java
package com.gerrard.cloudconsul.ctrl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorld {
@RequestMapping("/")
public String index() {
return "This is index! From gerrard_agent 1!";
}
@RequestMapping("/hello")
public String hello() {
return "Hello World! From gerrard_agent 1!";
}
}
package com.gerrard.cloudconsul.ctrl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorld {
@RequestMapping("/")
public String index() {
return "This is index! From gerrard_agent 2!";
}
@RequestMapping("/hello")
public String hello() {
return "Hello World! From gerrard_agent 2!";
}
}
[gerrard@nginx /]# cd /usr/src/
[gerrard@nginx src /]# tar -zxvf nginx-1.12.2.tar.gz
[gerrard@nginx src]# yum -y install gcc gcc-c++ make openssl-devel pcre-devel
[gerrard@nginx src]# cd nginx-1.12.2/
[gerrard@nginx nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_realip_module --with-pcre --with-http_ssl_module
[gerrard@nginx nginx-1.12.2]# make -j 2
[gerrard@nginx nginx-1.12.2]# make install
启动nginx
[gerrard@nginx nginx-1.12.2]# /usr/local/nginx/sbin/nginx
[gerrard@nginx ]# unzip consul-template_0.19.3_linux_amd64.zip[gerrard@nginx ]# mv consul-template /usr/bin/
upstream http_backend {
{{range service "spring-boot-consul"}}
server {{ .Address }}:{{ .Port }};
{{ end }}
}
server {
listen 8000;
server_name localhost;
location / {
proxy_pass http://http_backend;
}
}
简单描述下模板:upstream 定义一个简单的模板,server监听8000端口(你自己随便改,只要不被占用就行),反向代理到upstream。
7.2 修改nginx.conf
打开nginx.conf
[gerrard@nginx consul]# vim /usr/local/nginx/conf/nginx.conf
在server同级处,添加一句 include /usr/local/nginx/consul/*.conf; 如下图:
切记不要写错位置,否则你永远也访问不到你代理的地址。
配置完毕,需要重新加载nginx
[gerrard@nginx consul]# /usr/local/nginx/sbin/nginx -s reload
[gerrard@consul]# consul-template --consul-addr 192.168.188.182:8500 --template "./nginx.ctmpl:vhost.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info
查看配置:
[gerrard@consul]# cat /usr/local/nginx/consul/vhost.conf