SpringCloud微服务之网关Zuul

Zuul实现网关

pom.xml依赖添加



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.1.RELEASE
         
    
    top.solumon
    zuul
    0.0.1-SNAPSHOT
    zuul
    Demo project for Spring Boot

    
        1.8
        Hoxton.RELEASE
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-zuul
        
        
            org.springframework.cloud
            spring-cloud-starter-zookeeper-discovery
        

        
        
            org.apache.zookeeper
            zookeeper
            3.4.14
            
                
                    org.slf4j
                    slf4j-log4j12
                
                
                    log4j
                    log4j
                
            
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



application.yml配置

server:
  port: 8888
spring:
  application:
    name: springcloud-zuul-demo
  cloud:
    zookeeper:
      discovery:
        register: true
        enabled: true
        instance-id: 1
        root: /demo
      connect-string: 39.104.124.245:2181
# 在用户访问路径里面有api-member
zuul:
  routes:
    api-a:
      path: /api-member/**
      serviceId: app-member-service
    api-b:
      path: /api-order/**
      # 服务名称 zuul网关默认整合ribbon 自动实现负载均衡
      serviceId: app-order-service

启动Application 添加注解 @EnableZuulProxy

@EnableZuulProxy
@SpringCloudApplication
public class ZuulApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(ZuulApplication.class).web(true).run(args);
    }

}

实现Zuul集群

使用Nginx反向代理实现Zuul网关集群,保证每台网关配置数据相同

Nginx配置

http{
        upstream backServer {
        39.104.124.245:8900;
        39.104.124.245:8901;
         }
        server {
               location / {
                        # 指定上游服务器负载均衡服务器
                        proxy_pass http://backServer/;
                        index index.html index.htm;
               }
        }

}

你可能感兴趣的:(SpringCloud微服务之网关Zuul)