☝️ Scaffolding

To quickly create a Spring Cloud Gateway project, go to the [Spring Initializer] website (https://start.spring.io) or use IDEA.

The registry we chose here is Zookeeper. You can also choose other registries to register your project, such as Alibaba’s Nacos.

After configuring the relevant information, click the GENERATE button below to export the project’s ZIP package, unzip it and open it using the IDE.

This is what it looks like when it’s opened:

✌️ Configuring routes

Configured in Ymal mode

For configuration purposes, change application.properties to application.yml.

Then configure a forwarding to Baidu to route.

spring:
  cloud:
    gateway:
      routes:
        - id: route-demo
          uri: https://baidu.com
          predicates:
            - Path=/**
Copy the code

In the configuration, I add a predicate Path to indicate that all current requests will be matched to this route and then forwarded to the URI configuration to the URL. So when we open the browser to visit [http://localhost:8080/](http://localhost:8080/) is automatically jump to Baidu to the home page.

Java code configuration

In addition to configuring routes in configuration files, we can also configure routes in code.

Here is a code configured route:

@Bean
public RouteLocator routesConfig(RouteLocatorBuilder builder){
	return builder.routes()
			.route("route-demo",r -> r.path("/ * *").uri("https://baidu.com"))
			.build();
}
Copy the code

These lines of code implement the same function as the above configuration, when accessing [http://localhost:8080/](http://localhost:8080/) will also jump to the Baidu home page.

👌 Register with Zookeeper

Next, I’ll show you how to register the gateway with Zookeeper.

First of all, we set up Zookeeper locally. I directly started a Zookeeper with Docker.

Then add the following configuration to the configuration file:

spring:
  application:
    name: weidain-gateway
  cloud:
    zookeeper:
      connect-string: localhost:2181
Copy the code

Weidain-gateway is the name of the service we registered with Zookeeper, and the address [localhost:2181](http://localhost:2181) is the address we registered with our local Zookeeper registry.

After starting the project, we can use the Zookeeper visualization tool to see that the registry has an additional Services node with the Weidain-Gateway service we registered under the node

Here is our gateway service registered to Zookeeper data:

{
    "name": "weidain-gateway"."id": "8c802a81-12e7-4f72-9034-aee00c0745bb"."address": "169.254.238.114"."port": 8080."sslPort": null."payload": {
        "@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance"."id": "application-1"."name": "weidain-gateway"."metadata": {
            "instance_status": "UP"}},"registrationTimeUTC": 1620118042689."serviceType": "DYNAMIC"."uriSpec": {
        "parts": [{"value": "scheme"."variable": true
            },
            {
                "value": ": / /"."variable": false
            },
            {
                "value": "address"."variable": true
            },
            {
                "value": ":"."variable": false
            },
            {
                "value": "port"."variable": true}}}]Copy the code