Spring Cloud Gateway registered to the server center (Consul)
Preparing the environment
Start Consul (./ consul agent -dev) as a service center, the default is port 8500, and then start the spring-cloud-provider (9001 ports) and spring-cloud-provider-second ( 9002 ports) two projects as micro-services.
In the background you can see the two Consul management service starts:
Add a dependency Spring Cloud Gateway project
POM dependent increase in the following:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
Modify Spring Cloud Gateway project configuration
server:
port: 9000
spring:
cloud:
consul:
host: 127.0.0.1
port: 8500
discovery:
register: true
gateway:
routes:
- id: test_route
uri: lb://service-provider
predicates:
- Path=/service-provider/{segment}
filters:
- SetPath=/{segment}
application:
name: PC-ApiGateWay
- host: IP address of Consul
- port: Consul port number
- register: whether to register itself to the Consul
- lb: // service-provider: Consul service name to {lb: // access service name}
- Path: the routing path to match the format
- SetPath: Filter setting the path, is to match the access path can be set according to the delimiter
- name: to register themselves in the name of Consul
Start Spring Cloud Gateway Project
After starting Consul background you can see the registered content Spring Cloud Gateway project
Test Access
Access "127.0.0.1:9000/service-provider/hello?name=sws" this address, and refresh the page load balancing test gateway.
It can be seen backstage micro gateway proxy functionality services, and act as polling access.
Source
Github repository: https: //github.com/sunweisheng/spring-cloud-example