Spring Cloud Gateway features:
-
Built on Spring Framework 5, Project Reactor and Spring Boot 2.0
-
Able to match routes on any request attribute.
-
Predicates and filters are specific to routes.
-
Hystrix Circuit Breaker integration.
-
Spring Cloud DiscoveryClient integration
-
Easy to write Predicates and Filters
-
Request Rate Limiting
-
Path Rewriting
如果對於spring cloud gateway不太了解的同學可以看下這篇博客:http://www.spring4all.com/article/961
Eureka server配置:
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
---------------------
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
---------------------
application.yaml配置
server.port: 8888 eureka.instance.hostname: localhost eureka.client.registerWithEureka: false eureka.client.fetchRegistry: false eureka.client.serviceUrl.defaultZone: http://localhost:8888/eureka/
訪問url:localhost:8080
注冊中心配置完畢后,需要有服務提供者發布服務以及服務消費者訂閱服務
服務發布端
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
</dependencies>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
@RequestMapping(value = "/info", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public Person findPerson(){ Person person=new Person(); person.setId(1); person.setAge(18); person.setName("mike"); return person; }
application.yaml
server: port: 8000 spring: application: name: serviceProvider eureka: client: service-url: defaultZone: http://localhost:8888/eureka/
發布服務指定應用名稱,名稱中不要帶"-"。
consumer配置
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> <version>1.4.6.RELEASE</version> </dependency>
@RequestMapping("index") public String router() { // 根據應用名稱調用服務 String json = restTemplate.getForObject( "http://serviceProvider/info", String.class); return json; }
application.yaml
server: port: 8080 servlet: context-path: / spring: application: name: serviceConsumer eureka: client: instance: hostname: localhost service-url: defaultZone: http://localhost:8888/eureka/
直接通過localhost:8080/index接口訪問
gateway配置
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
server: port: 8084 spring: cloud: gateway: routes: - id: path_route uri: lb://serviceConsumer predicates: - Path=/index/** application: name: demogateway eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://localhost:8888/eureka/
指定注冊中心地址;並進行路由配置 uri:lb表示從注冊中心訂閱服務。
github地址:https://github.com/HushAsy/ans_gateway