Spring Cloud之三 网关 Gateway

1:Intellij 新建项目 spring-cloud-gateway

2:pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.tigee</groupId>
    <artifactId>spring-cloud-gateway</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-parent</artifactId>
                <version>2023.0.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!-- 引入 Spring Cloud 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>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-web</artifactId>-->
<!--        </dependency>-->
    </dependencies>
</project>

3:application.yml

server:
  port: 7101
  servlet:
    context-path: /
spring:
  application:
    name: spring-cloud-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true   #开启Eureka服务发现
          lower-case-service-id: true
eureka:
  client:
    service-url:
      defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
  instance:
    prefer-ip-address: true

4:启动类

package tigee;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ApplicationMain {
    static final Logger logger = LoggerFactory.getLogger(ApplicationMain.class);

    public static void main(String[] args) {
        logger.debug("服务启动..start......");
        SpringApplication.run(ApplicationMain.class, args);
        logger.debug("服务启动..end......");
    }

}

3:启动看 注册中心

4:请求接口

http://localhost:7101/user-service/hello?name=12

5:user-service 动多个端口

再请求 localhost:7101/user-service/hello?name=12

6:结果 是 端口 轮询显示

相关推荐
kerwin_code13 小时前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel
微微%13 小时前
SpringCloud微服务Gateway网关简单集成Sentinel
spring cloud·微服务·gateway
zzyh12345614 小时前
spring cloud如何实现负载均衡
spring·spring cloud·负载均衡
荆州克莱1 天前
Golang的图形编程基础
spring boot·spring·spring cloud·css3·技术
sjsjsbbsbsn1 天前
基于注解实现去重表消息防止重复消费
java·spring boot·分布式·spring cloud·java-rocketmq·java-rabbitmq
荆州克莱2 天前
Golang的网络编程安全
spring boot·spring·spring cloud·css3·技术
拾忆,想起2 天前
微服务入门:从零开始构建你的微服务架构
spring·spring cloud·微服务·架构
m0_548049702 天前
SpringCloud学习笔记【尚硅谷2024版】
笔记·学习·spring cloud
小扳2 天前
博客之星2024年度-技术总结:技术探险家小板的一年的征程
java·大数据·spring boot·elasticsearch·搜索引擎·spring cloud·微服务
晨的挥霍2 天前
spring cloud之gateway和JWT回顾
spring·spring cloud·gateway