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:结果 是 端口 轮询显示

相关推荐
工业互联网专业8 小时前
基于springcloud微服务架构的巡游出租管理平台
java·vue.js·spring cloud·微服务·毕业设计·源码·课程设计
喻米粒062211 小时前
RabbitMQ消息相关
java·jvm·spring boot·spring·spring cloud·sentinel·java-rabbitmq
小杨40420 小时前
springboot框架项目实践应用十四(扩展sentinel错误提示)
spring boot·后端·spring cloud
程序员 小柴20 小时前
SpringCloud概述
后端·spring·spring cloud
小杨4042 天前
springboot框架项目实践应用十三(springcloud alibaba整合sentinel)
spring boot·后端·spring cloud
m0_664047022 天前
基于Spring Boot+Layui构建企业级电子招投标系统源码
java·spring cloud·招投标系统源码·电子招标采购系统源码·企业电子招标采购系统源码
字节王德发2 天前
Ribbon负载均衡的深度解析与应用
spring cloud·ribbon·负载均衡
无眠_2 天前
【Spring Boot 与 Spring Cloud 深度 Mape 之七】服务容错与流量防护:Sentinel 全方位实战
spring boot·spring cloud·sentinel
A叶子叶2 天前
Kong网关部署研究
python·spring cloud·微服务·gateway·kong
快来卷java3 天前
深入剖析 JVM:从组成原理到调优实践
java·jvm·spring boot·spring cloud·数据挖掘·maven