Spring Cloud Gateway整合Eureka

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

posted @ 2018-11-05 11:11  HushAsy  阅读(8435)  评论(1编辑  收藏  举报