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

  1. Select Gateway and Consul

Step 2: configure the SRC/main/resources/application. The yml file

  1. 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.
  2. 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

  1. Running projects: github.com/cag2050/spr…

Step 5 verify that the service gateway is successfully forwarded

  1. 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

  1. Modify the SRC/main/resources/application. Yml, add routes
  2. 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

  1. Restart the server: github.com/cag2050/spr…
  2. Modify the SRC/main/resources/application. Yml, will beuri: http://localhost:8501Modified intouri: lb://service-producerHTTP ://localhost:8506/fooThe value of the argument foo is: bar, from port: 8501The value of the argument foo is: bar, from port: 8502To 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…