Gateway

搭建Gateway,并注册到Eureka

pom

<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>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

application.yml

spring:
  application:
    name: gateway

bootstrap.yml

server:
  port: 8080

eureka:
  instance:
    # 以IP注册到注册中心
    preferIpAddress: ${EUREKA_INSTANCE_PREFER_IP_ADDRESS:true}
    # 续约间隔时间,默认30秒
    leaseRenewalIntervalInSeconds: ${EUREKA_INSTANCE_LEASE_RENEWAL_INTERVAL_IN_SECONDS:30}
    # 实例故障摘除时间,默认90秒,实际是90*2=180秒,配置45就是90秒
    leaseExpirationDurationInSeconds: ${EUREKA_INSTANCE_LEASE_EXPIRATION_DURATION_IN_SECONDS:45}
  client:
    service-url:
      defaultZone: http://root:123456@127.0.0.1:8761/eureka
    registryFetchIntervalSeconds: 30

启动类

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {

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

}

启动项目,查看注册中心

通过网关访问demo项目

调整gateway的application.yml

spring:
  application:
    name: gateway
  cloud:
    gateway:
      routes:
        # 路由id,可以任意设置
        - id: demo
          # lb: 使用负载均衡策略
          uri: lb://demo

通过postman访问demo项目的hello接口

package com.learn.demo.api.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author :wm
 * @description : 欢迎Controller
 */
@RestController("helloController.v1")
@RequestMapping(value = "/v1")
public class HelloController {
    @GetMapping(value = "/hello")
    public String sayHello() {
        return "hello";
    }
}

参考资料

[1].eureka搭建

[2].gateway项目

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值