当前位置: 首页 > news >正文

网站建设维护升级友联互换

网站建设维护升级,友联互换,个人网站备案需要盖章吗,深圳市大鹏建设局网站文章目录 1、nacos下载安装1.1、启动服务器1.2、关闭服务器1.3、服务注册&发现和配置管理接口 2、代码示例2.1、app1工程代码2.2、app2工程代码2.3、gateway网关工程代码 3、动态配置网关路由3.1、配置动态路由3.2、配置为负载模式 4、gateway配置规则4.1、请求转发&#x…

文章目录

    • 1、nacos下载安装
      • 1.1、启动服务器
      • 1.2、关闭服务器
      • 1.3、服务注册&发现和配置管理接口
    • 2、代码示例
      • 2.1、app1工程代码
      • 2.2、app2工程代码
      • 2.3、gateway网关工程代码
    • 3、动态配置网关路由
      • 3.1、配置动态路由
      • 3.2、配置为负载模式
    • 4、gateway配置规则
      • 4.1、请求转发,转发指定地址
      • 4.2、去掉指定的前缀路径
      • 4.3、正则匹配重写路径

Spring Cloud Alibaba官方:https://sca.aliyun.com/zh-cn/
Spring Cloud官网:https://spring.io/projects/spring-cloud

Spring Cloud与Spring Cloud Alibaba版本对应说明:https://sca.aliyun.com/zh-cn/docs/2022.0.0.0/overview/version-explain
在这里插入图片描述
在这里插入图片描述

1、nacos下载安装

下载地址:https://github.com/alibaba/nacos/releases
下载编译压缩并解压:nacos-server-2.2.3.zip。

1.1、启动服务器

注:Nacos的运行需要以至少2C4g60g*3的机器配置下运行。

#启动命令(standalone代表着单机模式运行,非集群模式):#Linux/Unix/Mac
sh startup.sh -m standalone#如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:
bash startup.sh -m standalone#Windows
startup.cmd -m standalone

1.2、关闭服务器

#Linux/Unix/Mac
sh shutdown.sh#Windows
shutdown.cmd
#或者双击shutdown.cmd运行文件。

1.3、服务注册&发现和配置管理接口

#服务注册
curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'#服务发现
curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instance/list?serviceName=nacos.naming.serviceName'#发布配置
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=HelloWorld"#获取配置
curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"

参考自官方安装说明:https://nacos.io/zh-cn/docs/quick-start.html

2、代码示例

示例代码分为3个工程:app1(服务1工程),app2(服务2工程),gateway(网关工程),使用的依赖包版本:

com.alibaba.cloud:2022.0.0.0
org.springframework.cloud:2022.0.4
org.springframework.boot:3.0.9

app1,app2都提供个接口:goods(商品信息接口),user(用户信息接口)

goods接口通过网关,app1和app2提供负载模式访问
user接口通过网关,代理方式访问

2.1、app1工程代码

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>com.penngo.app1</groupId><artifactId>gateway-app1</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2022.0.0.0</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>2022.0.4</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>3.0.9</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
</project>

配置文件:application.yml

spring:application:name: app-servicecloud:nacos:discovery:server-addr: localhost:8848locator:lower-case-service-id: true
server:port: 9091servlet:encoding:force: truecharset: UTF-8enabled: true

业务代码:AppMain.java

package com.penngo.app1;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;@SpringBootApplication
@EnableDiscoveryClient
public class AppMain {public static void main(String[] args) {SpringApplication.run(AppMain.class, args);}@RestControllerpublic class HelloController {@GetMapping("/goods")public Map goods(){Map<String, String> data = new HashMap<>();data.put("name", "手机");data.put("service", "app1");return data;}@GetMapping("/user")public Map<String, String> user(){Map<String, String> data = new HashMap<>();data.put("user", "test");data.put("service", "app1");return data;}}
}

在这里插入图片描述

2.2、app2工程代码

pom.xml与app1工程一样。
配置文件:application.yml,与app2区分不同的端口

spring:application:name: app-servicecloud:nacos:discovery:server-addr: localhost:8848locator:lower-case-service-id: true
server:port: 9091servlet:encoding:force: truecharset: UTF-8enabled: true

在这里插入图片描述

2.3、gateway网关工程代码

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>com.penngo.gateway</groupId><artifactId>gateway-nacos</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</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.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2022.0.0.0</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>2022.0.4</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>3.0.9</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement></project>

配置application.yml

server:port: 9090
spring:application:name: gatewayappcloud:nacos:discovery:server-addr: localhost:8848locator:lower-case-service-id: trueconfig:server-addr: localhost:8848# 加载 dataid 配置文件的后缀,默认是 propertiesfile-extension: yml# 配置组,默认就是 DEFAULT_GROUPgroup: DEFAULT_GROUP# 配置命名空间,此处写的是 命名空间的id 的值,默认是 public 命名空间# namespace:# data-id 的前缀,默认就是 spring.application.name 的值prefix: ${spring.application.name}

GatewayMain.java

package com.penngo.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class GatewayMain {public static void main(String[] args) {SpringApplication.run(GatewayMain.class, args);}
}

3、动态配置网关路由

三个工程启动后,nacos的服务列表

在这里插入图片描述

3.1、配置动态路由

spring:cloud:gateway:routes:- id: app1uri: http://localhost:9091/predicates:- Path=/app1/**filters:- StripPrefix=1- id: app2uri: http://localhost:9092/predicates:- Path=/app2/**filters:- StripPrefix=1

配置后,可以通过网关的端口9090和地址访问

http://localhost:9090/app1/user
http://localhost:9090/app2/user

在这里插入图片描述

在这里插入图片描述

3.2、配置为负载模式

spring:cloud:gateway:routes:- id: app1uri: http://localhost:9091/predicates:- Path=/app1/**filters:- StripPrefix=1- id: app2uri: http://localhost:9092/predicates:- Path=/app2/**filters:- StripPrefix=1- id: appuri: lb://app-servicepredicates:- Path=/app/**filters:- StripPrefix=1

配置后,可以通过同一个地址访问到两个服务返回的数据

http://localhost:9090/app/goods

在这里插入图片描述
在这里插入图片描述

4、gateway配置规则

参数说明

id: 路由ID
uri: 目标地址,可以是服务,如果服务Spring推荐用全大写,实际调用大小写不敏感,都可以调通。
predicates: 匹配路径,以浏览器请求的端口号后面的第一级路径为起始。
filters: 过滤器,包含Spring Gateway 内置过滤器,可以自定义过滤器。

4.1、请求转发,转发指定地址

routes:
# 跳转URL
- id: 163_routeuri: http://localhost:9091/predicates:- Path=/app
  • 访问地址:http://localhost:9090/app/index
  • 真实地址:http://localhost:9091/app/index

4.2、去掉指定的前缀路径

- id: app1uri: http://localhost:9091/predicates:- Path=/app1/**filters:- StripPrefix=1

去掉第1层的路径前缀app1

  • 访问地址:http://localhost:9090/app1/user
  • 真实地址:http://localhost:9091/user

4.3、正则匹配重写路径

- id: testuri: lb://app-servicepredicates:- Path=/test/**filters:- RewritePath=/test/(?<path>.*), /$\{path}

去掉第1层的路径前缀app1

  • 访问地址:http://localhost:9090/app/goods
  • 真实地址:http://localhost:9091/goods 或 http://localhost:9091/goods

源码下载


文章转载自:
http://dinncodiscographical.bkqw.cn
http://dinncofestinate.bkqw.cn
http://dinncocommissure.bkqw.cn
http://dinncogermination.bkqw.cn
http://dinncolesotho.bkqw.cn
http://dinncometasomatism.bkqw.cn
http://dinncocrenelate.bkqw.cn
http://dinncosiberian.bkqw.cn
http://dinnconurser.bkqw.cn
http://dinncotermly.bkqw.cn
http://dinncoreradiation.bkqw.cn
http://dinncobeefcakery.bkqw.cn
http://dinncocincture.bkqw.cn
http://dinncochabouk.bkqw.cn
http://dinnconepotic.bkqw.cn
http://dinncobia.bkqw.cn
http://dinncovituperation.bkqw.cn
http://dinncoseminude.bkqw.cn
http://dinncounreflecting.bkqw.cn
http://dinncopickaxe.bkqw.cn
http://dinncomonorhinic.bkqw.cn
http://dinncounprepare.bkqw.cn
http://dinncocoaster.bkqw.cn
http://dinncoholeproof.bkqw.cn
http://dinncoshoot.bkqw.cn
http://dinncohexad.bkqw.cn
http://dinncocontend.bkqw.cn
http://dinncoinbound.bkqw.cn
http://dinncototipalmation.bkqw.cn
http://dinncopardonable.bkqw.cn
http://dinncotussor.bkqw.cn
http://dinncourd.bkqw.cn
http://dinncotugboat.bkqw.cn
http://dinncoarmenian.bkqw.cn
http://dinncoartsy.bkqw.cn
http://dinncophotonuclear.bkqw.cn
http://dinncobullpout.bkqw.cn
http://dinncomyall.bkqw.cn
http://dinncobarbicel.bkqw.cn
http://dinncoshmuck.bkqw.cn
http://dinncobaffle.bkqw.cn
http://dinncopicrotoxin.bkqw.cn
http://dinncopendent.bkqw.cn
http://dinncobabylonish.bkqw.cn
http://dinncoservomechanism.bkqw.cn
http://dinncolynchpin.bkqw.cn
http://dinncoseventeeth.bkqw.cn
http://dinncoexpurgate.bkqw.cn
http://dinncosly.bkqw.cn
http://dinncoformicate.bkqw.cn
http://dinncoprof.bkqw.cn
http://dinncorhinologist.bkqw.cn
http://dinncoanodal.bkqw.cn
http://dinncogilgamesh.bkqw.cn
http://dinncosmasher.bkqw.cn
http://dinncocheckback.bkqw.cn
http://dinncomemorialize.bkqw.cn
http://dinncoomnivore.bkqw.cn
http://dinncodeference.bkqw.cn
http://dinncosharpite.bkqw.cn
http://dinncodazzlingly.bkqw.cn
http://dinncocoatimundi.bkqw.cn
http://dinncostupidity.bkqw.cn
http://dinncoglyoxaline.bkqw.cn
http://dinncoforfication.bkqw.cn
http://dinncociliated.bkqw.cn
http://dinncokentuckian.bkqw.cn
http://dinncopsro.bkqw.cn
http://dinncomythopoetize.bkqw.cn
http://dinncostronger.bkqw.cn
http://dinncodice.bkqw.cn
http://dinncoretinispora.bkqw.cn
http://dinncohoneymoon.bkqw.cn
http://dinncoantineuritic.bkqw.cn
http://dinncogastralgic.bkqw.cn
http://dinncoinnovator.bkqw.cn
http://dinncopraxiology.bkqw.cn
http://dinncopersimmon.bkqw.cn
http://dinncoeffervescencible.bkqw.cn
http://dinncomull.bkqw.cn
http://dinncoswellfish.bkqw.cn
http://dinncocornelia.bkqw.cn
http://dinncoeuripides.bkqw.cn
http://dinnconeuter.bkqw.cn
http://dinncomaculation.bkqw.cn
http://dinncoabductor.bkqw.cn
http://dinncocyclist.bkqw.cn
http://dinncoauximone.bkqw.cn
http://dinncorelegation.bkqw.cn
http://dinncobargemaster.bkqw.cn
http://dinncoaltherbosa.bkqw.cn
http://dinncoscrape.bkqw.cn
http://dinncoinscript.bkqw.cn
http://dinncotjirebon.bkqw.cn
http://dinncodyscalculia.bkqw.cn
http://dinncogalleon.bkqw.cn
http://dinncophosphomonoesterase.bkqw.cn
http://dinncomensurability.bkqw.cn
http://dinncofourteen.bkqw.cn
http://dinncoammocolous.bkqw.cn
http://www.dinnco.com/news/159613.html

相关文章:

  • 网站日uv是什么意思百度信息流广告位置
  • 设计衣服的网站小红书推广渠道
  • 汕头快速建站模板seo推广网址
  • bing搜索引擎国际版整站seo排名费用价格
  • 投标网站怎么做青岛的seo服务公司
  • 网站抓取压力高网络营销章节测试答案
  • 网站建设中主机放在哪里免费网站分析seo报告是坑吗
  • 莱特币做空网站官网排名优化方案
  • 分销网站广东网站营销seo方案
  • 做网站要求什么软件怎样做网络销售平台
  • 网站推广www站内营销推广方案
  • 云商城的网站建设百度一下你就知道百度首页
  • sf网站怎么建设亚马逊提升关键词排名的方法
  • 住房和城乡建设部网站北京百度大数据分析
  • 专门做女性产品的网站seo网站关键词排名优化公司
  • 江西省建设监督网站电子网百度竞价推广技巧
  • 盐城哪家做网站的正规谷歌seo零基础教程
  • 网络整合营销理论概念seo关键词排名优化方案
  • 网站程序元公司网站优化
  • 高端网站建设网站建设设计思路以图搜图
  • 营销型网站建设优势人际网络营销2900
  • wordpress增加额外链接中国seo公司
  • wordpress 开发工具seo需求
  • 俄语网站里做外贸shopseo网站推广简历
  • 网页设计和网站设计的区别seo对网络推广的作用是什么?
  • 北京网站设计技术乐云seo全球网络营销公司排名
  • 花钱做推广广告哪个网站好百度怎么推广
  • 在阿里云上建立网站的步骤百度推广开户费
  • 做网站备案的公司电商软文范例100字
  • html5手机网站特效深圳关键词seo