Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Zuul Service Gateway in SpringCloud

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shares with you the content of a sample analysis of Zuul service gateways in SpringCloud. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

What is Zuul

   zuul is an open source API Gateway server from netflix, which is essentially a web servlet application.

   Zuul provides a framework for dynamic routing, monitoring, resiliency, security and other edge services on the cloud platform. Zuul acts as the front door for all requests at the back end of the Web website for devices and Netflix streaming applications.

The example of    zuul can be used by referring to netflix's simple webapp on github and following the documentation of netflix on github wiki.

Second, what problems have been solved

III. Introductory cases

1. Create a project

   creates a SpringBoot project

two。 Modify the pom file

   pay attention to adding dependencies of zuul

Org.springframework.cloud

Spring-cloud-dependencies

Dalston.SR5

Pom

Import

Org.springframework.boot

Spring-boot-starter-web

Org.springframework.cloud

Spring-cloud-starter-eureka

Org.springframework.cloud

Spring-cloud-starter-zuul

Org.springframework.boot

Spring-boot-maven-plugin

3. Modify configuration

Add service name, port and Eureka registry information to    application.properties

Spring.application.name=zuul-gateway

Server.port=9020

#\ u8BBE\ u7F6E\ u670D\ u52A1\ u6CE8\ u518C\ u4E2D\ u5FC3\ u5730\ u5740\ uFF0C\ u6307\ u5411\ u53E6\ u4E00\ u4E2A\ u6CE8\ u518C\ u4E2D\ u5FC3

Eureka.client.serviceUrl.defaultZone= http://dpb:123456@eureka1:8761/eureka/,http://dpb:123456@eureka2:8761/eureka/

4. Startup class

Note that zuul should be added to the    startup class.

@ SpringBootApplication

@ EnableZuulProxy

Public class StartZuul {

Public static void main (String [] args) {

SpringApplication.run (StartZuul.class, args)

}

}

5. test

  , we just start one of the front services.

I am starting a product service in the last case. If you need source code, you are welcome to visit my GitHub: https://github.com/q279583842q/SpringCloud-dpb-Demo.

Direct access: http://localhost:9001/product/findAll

Access through zuul Services Gateway: http://localhost:9020/e-book-product/product/findAll

Http:// gateway service address: gateway service port / name of the service accessed / address of the interface in the accessed service

IV. Routing rules

   to facilitate the demonstration, the routing case to create a new project to demonstrate, the content is the same as the above project.

1.URL specifies the route

The configuration in    application.properties is as follows:

Spring.application.name=zuul-gateway

Server.port=9030

#\ u8BBE\ u7F6E\ u670D\ u52A1\ u6CE8\ u518C\ u4E2D\ u5FC3\ u5730\ u5740\ uFF0C\ u6307\ u5411\ u53E6\ u4E00\ u4E2A\ u6CE8\ u518C\ u4E2D\ u5FC3

Eureka.client.serviceUrl.defaultZone= http://dpb:123456@eureka1:8761/eureka/,http://dpb:123456@eureka2:8761/eureka/

Zuul.routes.e-book-product-provider.path=/bobo/**

Zuul.routes.e-book-product-provider.url= http://127.0.0.1:9001/

Description:

Zuul.routes.e-book-product-provider.path

Zuul.routes is fixed, e-book-product-provider custom, path is fixed

/ bobo/** indicates the requested url address

Zuul.routes.e-book-product-provider.url= http://127.0.0.1:9001/

Indicates that when the user's request is routed to the http://127.0.0.1:9001/ service for processing by http://xxx/bobo/**

two。 The service name specifies the route

Mode one

Zuul.routes.e-book-product-provider.path=/bobo/**

Zuul.routes.e-book-product-provider.serviceId=e-book-product-provider

Description

When the url requested by the user is: http://xxxx/bobo/product/findAll, the

Zuul.routes.e-book-product-provider.serviceId=e-book-product-provider 's

E-book-product-provider service to process the request

Mode two

Zuul.routes.e-book-product-provider.path=/bobo/**

Description: how much is the flow of people in Wuxi http://mobile.bhnfkyy.com/

Zuul.routes.e-book-product-provider.path=/bobo/**

At this point, the name of the routed service must be between routes and path.

3. Exclusion of rout

   excludes routing, that is, the router will fool certain services and cannot access it even if the client sends a request

Zuul.ignored-services=e-book-product

If there are multiple services to exclude, the service name is connected through ","

Zuul.ignored-services=e-book-product,e-book-order

Because there are too many services, it is impossible to add them one by one by hand, so the route excludes all services and then aims at the

The routed service is added manually

# ignore all requests first

Zuul.ignored-services=*

# then release e-book-order 's service separately

Zuul.routes.e-book-order.path=/bobo/**

You can also exclude the path of the specified keyword

# exclude all requests containing the findAll keyword

Zuul.ignored-patterns=/**/findAll/**

# release the e-book-order service, and also exclude the findAll request

Zuul.routes.e-book-order.path=/bobo/**

4. Specify routing prefix

   means to add a prefix to the advance url.

# # http://127.0.0.1:9030/bobo/product-provider/product/findAll

Zuul.prefix=/bobo

Zuul.routes.e-book-product.path=/product-provider/**

Thank you for reading! This is the end of the article on "sample Analysis of Zuul Service Gateway in SpringCloud". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report