catalogue
2.3 add @ EnableZuulProxy to the main startup class
3.2 create a second zuul service
1. Project catalogue
2. Stand alone use
2.1 dependency
zuul gateway itself is also a micro service and needs to be registered in eureka. The complete dependencies are as follows.
Versions are uniformly defined in the parent project
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <exclusions> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> </dependencies>
2.2 configuration file
proxy-1 is the proxy name
/proxy-1 / * * is the proxy request path (this type of path will be proxy)
serviceId is the proxy service name and the service name registered in eureka
spring: application: name: springcloud-zuul server: port: 9410 eureka: instance: instance-id: springcloud-zuul1 appname: ${spring.application.name} prefer-ip-address: true lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 5 client: service-url: defaultZone: http://test1:9110/eureka/,http://test2:9120/eureka/ zuul: routes: proxy-1: path: /proxy-1/** serviceId: springcloud-common
2.3 add @ EnableZuulProxy to the main startup class
@SpringBootApplication @EnableEurekaClient @EnableZuulProxy public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
2.4 testing
Start the eureka service springcloud eureka *, service provider springcloud common *, zuul gateway springcloud-zuul1
Start service examples are as follows
Visit http://localhost:9410/proxy-1/com/get , return port 9230, indicating that the springcloud-common3 service has been called
3. Cluster configuration
It is realized by matching nginx, accessing the unified address of nginx proxy, distributing it to a zuul service by nginx, and then calling the specific service by zuul gateway.
3.1 nginx configuration
nginx download address http://nginx.org/en/download.html
nginx.conf configuration
The configuration file address is in the conf package under the nginx installation directory. My location is as follows
The specific configuration is as follows
http://localhost:90 Proxy path for nginx
http { client_max_body_size 200m; upstream backServer{ server 127.0.0.1:9410; #zuul1 service address server 127.0.0.1:9420; #zuul2 service address } server { #localhost:90 is the request path of nginx agent listen 90; server_name localhost; location / { proxy_pass http://backServer/; # The backserver configured above } } }
3.2 create a second zuul service
zuul1 port 9410
zuul2 port 9420
The creation method is the same as above.
Configuration file in spring cloud-zuul2
The port is different from the instance name in eureka, and the others are exactly the same as springcloud-zuul1
spring: application: name: springcloud-zuul server: port: 9420 eureka: instance: instance-id: springcloud-zuul2 appname: ${spring.application.name} prefer-ip-address: true lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 5 client: service-url: defaultZone: http://test1:9110/eureka/,http://test2:9120/eureka/ zuul: routes: proxy-1: path: /proxy-1/** serviceId: springcloud-common
3.3 testing
Start nginx
After startup, you can check whether nginx is started successfully in the task manager
Start the eureka service springcloud eureka *, service provider springcloud common *, zuul gateway springcloud-zuul1
Start service examples are as follows
Access: http://localhost:90/proxy-1/com/get
The returned result zuulport 9410 indicates that the calling zuul gateway service is springcloud-zuul1, and port 9210 indicates that springcloud-common1 is finally called
localhost:90 is nginx Nginx listening service path configured in conf
proxy-1 is the proxy path of zuul gateway service
com/get is the service request path in springcloud common for the actual request service configured in zuul
nginx adopts polling policy by default. The two request discovery requests two zuul gateway services respectively.
4. Custom filter
Please check other blog posts on the Internet for detailed explanation. Here is just a simple use.
1. Inherit ZuulFilter (note the definition of filterType and FilterConstants at this time)
2. Register filter
@Configuration public class FilterConfiguration { @Bean ZuulFilter myFilter() { return new MyFilter(); } @Bean ZuulFilter myPostFilter() { return new MyPostFilter(); } }