What is a routing gateway? What is the significance of its existence?
In microservices, we will have many microservices, but there is only one client. When it calls our remote interface, it must use different addresses, such as the following three microservices (also used in this project) Of the three microservices, see the end of the article for the code address)
Service Name | Item name | Example interface call address |
---|---|---|
Commodity module | gateway-goods-7001 | http://localhost:7001/goods/main |
Order module | gateway-order-8001 | http://localhost:8001/order/main |
User Center Module | gateway-user-9001 | http://localhost:9001/user/main |
In this way, when the client calls, it also needs to use a different address (host name and port), can it be called directly using an address? It's similar to the kind of project we wrote before. such as:
http://localhost:80/goods/main
Commodity modulehttp://localhost:80/order/main
Order modulehttp://localhost:80/user/main
User Center Module
GateWay Getting Started
- Introduce dependent pom.xml
- Placement application.yml
- Main startup class
Well, then directly start the three microservices and this project, in the browser, you can directly use http://localhost:80/goods/main
the address to access each service.
The three components of GateWay
Route
This has been demonstrated in the introductory operation above, and is configured spring.cloud.gateway.routes
to perform route matching.
However, the uri we configured above is the network address (of course, we can directly configure our localhost to other addresses on the network, such as baidu.com). In microservices, we must not directly write dead, and a microservice may also have multiple addresses, so we must be configured as the microservice name of the registry. The modified configuration content is as follows:
- In addition to the above application.yml, there is another way to configure routing (I guess you must not say applicin.properties). That is the way of injecting SpringBean, not to mention here, those who are interested can go to the official website to take a look. But it is recommended that you use the configuration file configuration, because Nacos can be integrated for dynamic configuration, which is more flexible than writing into the code
Assertion (Predicates)
In the configuration file above, the predicates in routes are a series of assertions, which means that what can be done only if such conditions are met. The Path attribute is used above, in addition, there is
The path is matched according to the path. Commonly used are Method (request method) and Host (requested address). These assertions are "and" relations, that is to say, if you configure multiple predicates, they must be satisfied at the same time in order to route forwarding. Detailed reference
Filter
It is the Filter used in web applications. What's the use of it? The most common use is in the front-end and back-end separation projects. We can verify the authorization of tokens and other authorizations at the gateway layer (to give you an example tomorrow).
Use SpringSecurity and Gateway for network authentication and authorization verification
Code address: Pay attention to WeChat public account "Xiaoyu and Java", reply to SpringCloud to obtain