004 springCloudAlibaba Gateway

2024-05-02 16:28

本文主要是介绍004 springCloudAlibaba Gateway,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 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

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>

这篇关于004 springCloudAlibaba Gateway的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/954674

相关文章

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.

Java调用Python代码的几种方法小结

《Java调用Python代码的几种方法小结》Python语言有丰富的系统管理、数据处理、统计类软件包,因此从java应用中调用Python代码的需求很常见、实用,本文介绍几种方法从java调用Pyt... 目录引言Java core使用ProcessBuilder使用Java脚本引擎总结引言python

SpringBoot操作spark处理hdfs文件的操作方法

《SpringBoot操作spark处理hdfs文件的操作方法》本文介绍了如何使用SpringBoot操作Spark处理HDFS文件,包括导入依赖、配置Spark信息、编写Controller和Ser... 目录SpringBoot操作spark处理hdfs文件1、导入依赖2、配置spark信息3、cont

springboot整合 xxl-job及使用步骤

《springboot整合xxl-job及使用步骤》XXL-JOB是一个分布式任务调度平台,用于解决分布式系统中的任务调度和管理问题,文章详细介绍了XXL-JOB的架构,包括调度中心、执行器和Web... 目录一、xxl-job是什么二、使用步骤1. 下载并运行管理端代码2. 访问管理页面,确认是否启动成功

Java中的密码加密方式

《Java中的密码加密方式》文章介绍了Java中使用MD5算法对密码进行加密的方法,以及如何通过加盐和多重加密来提高密码的安全性,MD5是一种不可逆的哈希算法,适合用于存储密码,因为其输出的摘要长度固... 目录Java的密码加密方式密码加密一般的应用方式是总结Java的密码加密方式密码加密【这里采用的

Java中ArrayList的8种浅拷贝方式示例代码

《Java中ArrayList的8种浅拷贝方式示例代码》:本文主要介绍Java中ArrayList的8种浅拷贝方式的相关资料,讲解了Java中ArrayList的浅拷贝概念,并详细分享了八种实现浅... 目录引言什么是浅拷贝?ArrayList 浅拷贝的重要性方法一:使用构造函数方法二:使用 addAll(

解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题

《解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题》本文主要讲述了在使用MyBatis和MyBatis-Plus时遇到的绑定异常... 目录myBATis-plus-boot-starpythonter与mybatis-spring-b

Java中switch-case结构的使用方法举例详解

《Java中switch-case结构的使用方法举例详解》:本文主要介绍Java中switch-case结构使用的相关资料,switch-case结构是Java中处理多个分支条件的一种有效方式,它... 目录前言一、switch-case结构的基本语法二、使用示例三、注意事项四、总结前言对于Java初学者

关于Java内存访问重排序的研究

《关于Java内存访问重排序的研究》文章主要介绍了重排序现象及其在多线程编程中的影响,包括内存可见性问题和Java内存模型中对重排序的规则... 目录什么是重排序重排序图解重排序实验as-if-serial语义内存访问重排序与内存可见性内存访问重排序与Java内存模型重排序示意表内存屏障内存屏障示意表Int

Java汇编源码如何查看环境搭建

《Java汇编源码如何查看环境搭建》:本文主要介绍如何在IntelliJIDEA开发环境中搭建字节码和汇编环境,以便更好地进行代码调优和JVM学习,首先,介绍了如何配置IntelliJIDEA以方... 目录一、简介二、在IDEA开发环境中搭建汇编环境2.1 在IDEA中搭建字节码查看环境2.1.1 搭建步