基于nginx+consul-template+consul+springboot实现自动发现服务的集群
近来公司需要研究consul作为技术储备,本人研究了一下,所谓好记性不如烂笔头子,所以在此记录一下,也希望能帮助到需要的小伙伴,因本人技术能力有限,文章可能有不足或者错误之处,请大神指摘,好及时更改,及时刷新自己的认知。 由于集群和负载均衡需要多台服务器测试,公司临时也没有多余服务器,所以用了虚拟机。 本文使用了两台虚拟linux作为springboot业务服务,当然
近来公司需要研究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
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gerrard</groupId>
<artifactId>cloud-consul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cloud-consul</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RC1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
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
更多推荐
所有评论(0)