spring cloud:gateway-eureka

gateway-server-eureka

1. File-->new spring starter project

2.add dependency

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

 

3.Edit application.yml

server:
  port: 8080
spring:
  application:
    name: gateway-server-eureka
  cloud:
    gateway:
#      routes:
#      - id: neo_route
#        uri: https://www.cnblogs.com/alittlesmile/
#        predicates:
#        - Path=/alittlesmile
      discovery:
        locator:
          enabled: true

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8000/eureka/

 

配置说明:

  • spring.cloud.gateway.discovery.locator.enabled:是否与服务注册于发现组件进行结合,通过 serviceId 转发到具体的服务实例。默认为 false,设为 true 便开启通过服务中心的自动根据 serviceId 创建路由的功能。
  • eureka.client.service-url.defaultZone指定注册中心的地址,以便使用服务发现功能

修改完成后启动 gateway-server-eureka 项目,访问注册中心地址 http://localhost:8000/ 即可看到名为 GATEWAY-SERVER-EUREKA的服务。

4.program

package com.smile;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class GatewayServerEurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayServerEurekaApplication.class, args);
    }

}

 

5.Run

将 Spring Cloud Gateway 注册到服务中心之后,网关会自动代理所有的在注册中心的服务,访问这些服务的语法为:

http://网关地址:端口/服务中心注册 serviceId/具体的url

启动producer,producer-1

在浏览器多次访问地址:http://localhost:8080/PRODUCER/getHello?name=xx,页面交替返回以下信息:

hello xx
hello xx,this is producer 1

说明后端服务自动进行了均衡负载。

 

转载于:https://www.cnblogs.com/alittlesmile/p/10912252.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值