採用docker-compose和consul-template實現nginx自動發現自動註冊-微服務治理

fengqiaoguke發表於2019-08-19

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}}
}

更多關注唐楓橋公眾號

相關文章