Create a moudle
The first step in the introduction of dependence:
<?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"> <parent> <artifactId>cloud-demo</artifactId> <groupId>com.company</groupId> <version>1.0.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>zuul-gateway</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> </dependencies> </project>
Step Two: Start writing class, add annotations to enable the @EnableZuulProxy Zuul
package com.company; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableZuulProxy public class ZuulGatewayApplication { public static void main(String[] args) { SpringApplication.run(ZuulGatewayApplication.class); } }
Step Three: Configure application.yml
# Connection eureka registry eureka: Client: Service - url: defaultzone: HTTP: // 127.0.0.1:8888/eureka # specified port Server: Port: 8989 # Specify the service name the Spring: the Application: name: Zuul - Gateway # Configure gateway
zuul Gateway has a default configuration, it is to match the service id, then forwards, and to achieve load balancing
Originally access address: HTTP: // localhost: 8080 / consumer3 / 1
Access address after the addition of Zuul: HTTP: // localhost: 8989 / Consumer-Service / consumer3 / 1
http: // localhost: 8989 / service the above mentioned id / consumer3 / 1
The default will take the list of services from eureka Latin America, and generates a default route mapping.
# Configure Gateway
zuul:
routes:
Consumer-Service: / Consumer-Service / ** # equivalent to the default configuration, this time eureka other services also like matching rules
ignored-services: # service configuration set to be ignored, so it zuul the service does not generate default mapping, it will not be exposed outside of the service
- user-service # service id
If you do not want to expose some services in zuul, you can add the above configuration