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

转载于:https://www.cnblogs.com/HushAsy/p/9908027.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Gateway可以与Ribbon组件整合,以实现负载均衡的功能。下面是一个示例,演示了如何使用Spring Cloud Gateway整合Ribbon: 1. 首先,确保你已经在项目的pom.xml文件中添加了Spring Cloud Gateway和Ribbon的依赖。 2. 创建一个Spring Boot应用程序,并在启动类上添加`@EnableEurekaClient`注解,以便将应用程序注册到Eureka Server。 3. 在应用程序的配置文件中,配置Eureka Server的地址和端口号。 4. 创建一个`@Configuration`类,用于配置Spring Cloud Gateway和Ribbon。 ```java @Configuration public class GatewayConfig { @Autowired private DiscoveryClient discoveryClient; @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("service-route", r -> r.path("/api/**") .filters(f -> f.stripPrefix(1)) .uri("lb://service")) .build(); } @Bean public LoadBalancerClient loadBalancerClient() { return new RibbonLoadBalancerClient(discoveryClient); } } ``` 在上面的配置中,我们创建了一个`RouteLocator` bean,用于定义路由规则。在这个例子中,我们将所有以`/api/`开头的请求转发到名为`service`的微服务上。同时,我们还创建了一个`LoadBalancerClient` bean,用于实现负载均衡。 5. 创建一个Controller类,用于处理请求并返回响应。 ```java @RestController public class GatewayController { @GetMapping("/api/hello") public String hello() { return "Hello from service!"; } } ``` 在上面的例子中,我们创建了一个`/api/hello`的GET请求处理方法,返回一个简单的字符串。 6. 启动应用程序,并访问`http://localhost:8080/api/hello`,你将会看到从`service`微服务返回的响应。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值