Gateway Gateway registers with service center Consul
The Spring Cloud Gateway provides a default forwarding capability that proxies all services in the service center by default once the Spring Cloud Gateway is registered with the service center.
Step 1: Create the project
- Select Gateway and Consul
Step 2: configure the SRC/main/resources/application. The yml file
spring.cloud.gateway.discovery.locator.enabled
: Indicates whether to combine with the service registration and discovery component to forward the service to a specific service instance using the serviceId. The default value is false. If this parameter is set to true, the service center automatically creates routes based on the serviceId.- Specify the address of the registry to use the service discovery feature
Spring. Cloud. Consul. Host = 127.0.0.1 spring. Cloud. Consul. Port = 8500Settings do not need to be registered in Consul
spring.cloud.consul.discovery.register=false
Copy the code
Step 3: Create Consul container
1. Official website of the image: hub.docker.com/_/consul
2. Pull the mirror:
Docker pull consul: 1.6.0Copy the code
3. Create a container (default HTTP management port: 8500)
Docker run -p 85:8500 Consul :1.6.0Copy the code
4. Visit the management website
http://localhost:8500/
Copy the code
Step 4: Start the server
- Running projects: github.com/cag2050/spr…
Step 5 verify that the service gateway is successfully forwarded
- Project spring_cloud_consul_producer1_demo/hello service is: http://localhost:8501/hello, visit: http://localhost:8506/service – producer/hello, normal return similar words: hello, explain the service gateway forward success.
Step 6: Filter usage: Use AddRequestParameter GatewayFilter to add specified parameters to the request
- Modify the SRC/main/resources/application. Yml, add routes
- Access to the addresshttp://localhost:8501/fooThe page returns something like:
The value of the argument foo is: null
, the argument foo was not received; The service is invoked through the gateway, and the browser accesses the addresshttp://localhost:8506/fooThe page returns something like:The value of the argument foo is: bar
“, indicating that the value bar of parameter foo is successfully received, indicating that the gateway has added the parameters and values through filter during the forwarding process.
Step 7: Service-based routing and forwarding
- Restart the server: github.com/cag2050/spr…
- Modify the SRC/main/resources/application. Yml, will be
uri: http://localhost:8501
Modified intouri: lb://service-producer
HTTP ://localhost:8506/fooThe value of the argument foo is: bar, from port: 8501
和The value of the argument foo is: bar, from port: 8502
To prove that the request is forwarded evenly to the back-end service and the back-end service receives the value of parameter foo added to filter.
reference
The resources | note | The url |
---|---|---|
Spring Cloud GateWay services and filters | For this reference, the registry used in the project is Eureka. | www.ityouknow.com/springcloud… |
Code repository: github.com/cag2050/spr…