采用docker-compose和consul-template实现nginx自动发现自动注册-微服务治理

docker结合consul-template实现nginx的服务发现

docker-compose.yml

version: '3'
services:    
  go-nginx:
    build: 
      context: ./consul
      dockerfile: Dockerfile
    restart: always
    depends_on:
        - consul4
    ports: 
      - "81:80"    
    volumes:
      - ./nginx/logs:/wwwlogs
      - ./nginx/vhost:/etc/nginx/conf.d
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./consul:/consul
      - ../apidoc:/www/apidoc
    ulimits:
      nproc: 60000
      nofile:
        soft: 60000
        hard: 60000   
    networks:
      - gonetwork    
    tty: true
    command:
    - /bin/sh
    - -c
    - |
      nginx && ./consul-template -consul-addr=consul4:8500 -template="./nginx.tpl:/etc/nginx/conf.d/site.conf:nginx -s reload" 
  go-redis:
    image: redis:5.0.5
    volumes:
      - ./redis/data:/data  
    ports:
      - "6379:6379"
    networks:
      - gonetwork    
    tty: true 
    #command: redis-server --requirepass aaaaaaaa
  consul1:
    image: consul
    container_name: node1
    command: agent -server -bootstrap-expect=3 -node=node1 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
    networks:
      - gonetwork

  consul2:
    image: consul
    container_name: node2
    command: agent -server -retry-join=node1 -node=node2 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
    depends_on:
        - consul1
    networks:
      - gonetwork

  consul3:
    image: consul
    container_name: node3
    command: agent -server -retry-join=node1 -node=node3 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
    depends_on:
        - consul1
    networks:
      - gonetwork

  consul4:
    image: consul
    container_name: node4
    command: agent -retry-join=node1 -node=ndoe4 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1 -ui 
    ports:
      - 8500:8500
    depends_on:
        - consul2
        - consul3
    networks:
      - gonetwork    
networks:
  gonetwork:
    driver: bridge

consul-template生成nginx模板

{{range services}} {{$name := .Name}} {{$service := service .Name}}
upstream {{$name}} {
  zone upstream-{{$name}} 64k;
  {{range $service}}server {{.Address}}:{{.Port}} max_fails=3 fail_timeout=60 weight=1;
  {{else}}server 127.0.0.1:80; # force a 502{{end}}
} {{end}}

server {
    listen 80;	
	charset utf-8;
	default_type application/json;
    server_name  api.fengqiaoju.com;
	
	location /apidoc {
		root /www ; 
	}
	
	{{range services}} {{$name := .Name}}
    location /{{$name}} {
        proxy_pass  http://{{$name}};
 
        #Proxy Settings
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
   }
   {{end}}
}

更多关注唐枫桥公众号

你可能感兴趣的:(nginx自动发现,nginx自动注册,consul,consul-template,consul-template,docker)