004 springCloudAlibaba Gateway

server/2024/10/18 8:26:37/

文章目录

    • gatewayServer
      • GatewayServerApplication.java
      • ServletInitializer.java
      • application.yaml
      • pom.xml
    • orderServer
      • OrderController.java
      • ProductClient.java
      • OrderServerApplication.java
      • ServletInitializer.java
      • application.yaml
      • pom.xml
    • productServer
      • ProductController.java
      • Product.java
      • ProductServerApplication.java
      • ServletInitializer.java
      • application.yaml
      • pom.xml
    • pom.xml

gatewayServer_1">gatewayServer

GatewayServerApplication.java


package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class GatewayServerApplication {public static void main(String[] args) {SpringApplication.run(GatewayServerApplication.class, args);}}

ServletInitializer.java


package com.example;import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;public class ServletInitializer extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(GatewayServerApplication.class);}}

application.yaml


#服务名称 端口
server:port: 9090
spring:application:name: gatewayServercloud:nacos:  # nacos urldiscovery:server-addr: localhost:8848sentinel: #sentinel urltransport:dashboard: localhost:8080gateway:   # gatewaydiscovery:locator:enabled: true #开启gateway从nacos上获取服务列表routes:- id: order_routeuri: lb://orderServer # 假设orderServer已经在服务注册中心(如Nacos)注册predicates:- Path=/order/**- Method=GET,POST- Before=2025-07-09T17:42:47.789-07:00[Asia/Shanghai]- id: product_routeuri: lb://productServer # 假设productServer已经在服务注册中心(如Nacos)注册predicates:- Path=/product/**feign:sentinel:enabled: true

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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.example</groupId><artifactId>springCloudAlibaba</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>com.example</groupId><artifactId>gatewayServer</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>gatewayServer</name><description>gatewayServer</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!--SpringCloud ailibaba sentinel --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><!--openfeign--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

orderServer

OrderController.java


package com.example.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("order")
public class OrderController {@Value("${server.port}")private String serverPort;@Autowiredprivate ProductClient productClient;@GetMapping("save")public String save(){//在订单中 要有商品的信息 proId = 9Integer proId = 9;String productResult = productClient.getProductById(proId);System.out.println("在订单中 要有商品的信息 proId = "+proId + productResult);//return "订单服务"+serverPort+"正在下订单";return "订单服务 "+serverPort+" "+productResult;}//    @GetMapping("order/{orderId}")
//    public String getById(@PathVariable("orderId") Integer orderId){
//        System.out.println("订单服务port="+serverPort+"上查询id="+orderId+"的订单");
//        return "port:"+serverPort +"查询到id="+orderId+"的订单";
//    }}

ProductClient.java


package com.example.controller;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;@FeignClient("productServer")
public interface ProductClient {@GetMapping("product/{proId}")public String getProductById(@PathVariable("proId") Integer proId);}

OrderServerApplication.java


package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OrderServerApplication {public static void main(String[] args) {SpringApplication.run(OrderServerApplication.class, args);}}

ServletInitializer.java


package com.example;import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;public class ServletInitializer extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(OrderServerApplication.class);}}

application.yaml


server:port: 9001spring:application:name: orderServercloud:nacos:server-addr: localhost:8848 # ?????URLsentinel:transport:dashboard: localhost:8080 #??Sentinel dashboard??port: 8719management:endpoints:web:exposure:include: '*'feign:sentinel:enabled: true # ??Sentinel?Feign???

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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.example</groupId><artifactId>springCloudAlibaba</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>com.example</groupId><artifactId>orderServer</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>orderServer</name><description>orderServer</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><!--        内置了 LoadBalancer 负载均衡器--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-loadbalancer</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

productServer

ProductController.java


package com.example.controller;import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.example.entity.Product;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("product")
public class ProductController {@Value("${server.port}")private Integer serverPort;@GetMapping("/{proId}")@SentinelResource(value = "getById",blockHandler = "handleBlock",fallback = "handleFallback")public String getById(@PathVariable("proId") Integer proId){Product product = new Product(proId,"保温杯",69.9F,"images/cup.png");System.out.println("商品服务"+serverPort+"正在查询商品:"+proId);//int i = 10/0;return product.toString();}/*** 违背流控规则,blockHandler* product/{proId}* 有流控规则,QPS <= 1* 若超过流量阈值,blockHandler*/public String handleBlock(@PathVariable("proId") Integer proId, BlockException exception) {return "商品查询请求QPS>1,超过流量阈值";}/*** 业务有异常,fallback*/public String handleFallback(@PathVariable("proId") Integer proId,Throwable e) {Product product = new Product();product.setProductId(proId);product.setProducutName("保温杯");return "[fallback]商品查询的信息是:" + product;}}

Product.java


package com.example.entity;public class Product {private Integer productId;private String producutName;private Float producutPrice;private String producutImg;public Product(){}public Product(Integer productId, String producutName, Float producutPrice, String producutImg) {this.productId = productId;this.producutName = producutName;this.producutPrice = producutPrice;this.producutImg = producutImg;}public Integer getProductId() {return productId;}public void setProductId(Integer productId) {this.productId = productId;}public String getProducutName() {return producutName;}public void setProducutName(String producutName) {this.producutName = producutName;}public Float getProducutPrice() {return producutPrice;}public void setProducutPrice(Float producutPrice) {this.producutPrice = producutPrice;}public String getProducutImg() {return producutImg;}public void setProducutImg(String producutImg) {this.producutImg = producutImg;}@Overridepublic String toString() {return "Product{" +"productId=" + productId +", producutName='" + producutName + '\'' +", producutPrice=" + producutPrice +", producutImg='" + producutImg + '\'' +'}';}
}

ProductServerApplication.java


package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDiscoveryClient
public class ProductServerApplication {public static void main(String[] args) {SpringApplication.run(ProductServerApplication.class, args);}}

ServletInitializer.java


package com.example;import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;public class ServletInitializer extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(ProductServerApplication.class);}}

application.yaml


server:port: 7001spring:application:name: productServercloud:nacos:server-addr: localhost:8848  # ???? nacos ???sentinel:transport:dashboard: localhost:8080 #??Sentinel dashboard??port: 8719management:endpoints:web:exposure:include: '*'feign:sentinel:enabled: true # ??Sentinel?Feign???

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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.example</groupId><artifactId>springCloudAlibaba</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>com.example</groupId><artifactId>productServer</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>productServer</name><description>productServer</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.6</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>springCloudAlibaba</artifactId><version>0.0.1-SNAPSHOT</version><name>springCloudAlibaba</name><description>springCloudAlibaba</description><modules><module>orderServer</module><module>productServer</module><module>gatewayServer</module></modules><packaging>pom</packaging><properties><java.version>1.8</java.version><spring-cloud.version>2021.0.4</spring-cloud.version><spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

http://www.ppmy.cn/server/28332.html

相关文章

深入探索达梦数据库:关键技术学习路径与实战指南

导语 随着国产数据库的崛起&#xff0c;达梦数据库作为国内自主研发的关系型数据库管理系统&#xff0c;以其优异的性能、强大的功能和高度的兼容性赢得了广泛认可。对于数据库工程师、开发人员以及IT专业人士来说&#xff0c;深入学习和掌握达梦数据库技术不仅是提升专业能力的…

实现并发请求数量控制:提高网页性能的关键

&#x1f90d; 前端开发工程师、技术日更博主、已过CET6 &#x1f368; 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 &#x1f560; 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》 &#x1f35a; 蓝桥云课签约作者、上架课程《Vue.js 和 E…

MySQL-配置文件

1、配置文件格式 配置文件中启动选项被分为若干组&#xff0c;每组都有一个’组名’&#xff0c;用[ ] 包裹每组下都可定义若干个启动选项配置文件中指定的启动选项不允许添加--前缀配置文件中每行只能指定一个具体启动选项相关分组示例如下&#xff1a; [server] (具体启动选…

第11章 数据库技术(第一部分)

一、数据库技术术语 &#xff08;一&#xff09;术语 1、数据 数据描述事物的符号描述一个对象所用的标识&#xff0c;可以文字、图形、图像、语言等等 2、信息 现实世界对事物状态变化的反馈。可感知、可存储、可加工、可再生。数据是信息的表现形式和载体&#xff0c;信…

Pytorch迁移学习训练病变分类模型

划分数据集 1.创建训练集文件夹和测试集文件夹 # 创建 train 文件夹 os.mkdir(os.path.join(dataset_path, train))# 创建 test 文件夹 os.mkdir(os.path.join(dataset_path, val))# 在 train 和 test 文件夹中创建各类别子文件夹 for Retinopathy in classes:os.mkdir(os.pa…

为什么叫“机器学习”Machine Learning 而不是叫“计算机学习”?

有一门学科“机器学习”火了起来&#xff0c;它是计算机科学与数学结合的产物&#xff0c;它的目的是使计算机“聪明”起来&#xff0c;实现人工智能。可是&#xff0c;令人困惑的是它明明就是计算机学习&#xff0c;为什么不叫“计算机学习”而叫“机器学习”呢&#xff1f;这…

Java 笔记 11:Java 方法相关内容

一、前言 记录时间 [2024-05-01] 系列文章简摘&#xff1a; Java 笔记 01&#xff1a;Java 概述&#xff0c;MarkDown 常用语法整理 Java 笔记 02&#xff1a;Java 开发环境的搭建&#xff0c;IDEA / Notepad / JDK 安装及环境配置&#xff0c;编写第一个 Java 程序 Java 笔记 …

acwing算法提高之数据结构--平衡树Treap

目录 1 介绍2 训练 1 介绍 本博客用来记录使用平衡树求解的题目。 插入、删除、查询操作的时间复杂度都是O(logN)。 动态维护一个有序序列。 2 训练 题目1&#xff1a;253普通平衡树 C代码如下&#xff0c; #include <cstdio> #include <cstring> #include …