网站优化方案案例,器材管理网站开发,做直播网站多少钱,二手域名文章目录 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 standalone1.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?serviceNamenacos.naming.serviceNameip20.18.7.10port8080#服务发现
curl -X GET http://127.0.0.1:8848/nacos/v1/ns/instance/list?serviceNamenacos.naming.serviceName#发布配置
curl -X POST http://127.0.0.1:8848/nacos/v1/cs/configs?dataIdnacos.cfg.dataIdgrouptestcontentHelloWorld#获取配置
curl -X GET http://127.0.0.1:8848/nacos/v1/cs/configs?dataIdnacos.cfg.dataIdgrouptest参考自官方安装说明:https://nacos.io/zh-cn/docs/quick-start.html
2、代码示例
示例代码分为3个工程app1服务1工程,app2服务2工程,gateway网关工程使用的依赖包版本
com.alibaba.cloud2022.0.0.0
org.springframework.cloud2022.0.4
org.springframework.boot3.0.9app1,app2都提供个接口goods(商品信息接口)user(用户信息接口)
goods接口通过网关app1和app2提供负载模式访问
user接口通过网关代理方式访问2.1、app1工程代码
pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.penngo.app1/groupIdartifactIdgateway-app1/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-alibaba-dependencies/artifactIdversion2022.0.0.0/versiontypepom/typescopeimport/scope/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversion2022.0.4/versiontypepom/typescopeimport/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion3.0.9/versiontypepom/typescopeimport/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(){MapString, String data new HashMap();data.put(name, 手机);data.put(service, app1);return data;}GetMapping(/user)public MapString, String user(){MapString, 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: true2.3、gateway网关工程代码
pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.penngo.gateway/groupIdartifactIdgateway-nacos/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependenciesdependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependencydependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-config/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-gateway/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-loadbalancer/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-alibaba-dependencies/artifactIdversion2022.0.0.0/versiontypepom/typescopeimport/scope/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversion2022.0.4/versiontypepom/typescopeimport/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion3.0.9/versiontypepom/typescopeimport/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:- StripPrefix1- id: app2uri: http://localhost:9092/predicates:- Path/app2/**filters:- StripPrefix1配置后可以通过网关的端口9090和地址访问
http://localhost:9090/app1/user
http://localhost:9090/app2/user3.2、配置为负载模式
spring:cloud:gateway:routes:- id: app1uri: http://localhost:9091/predicates:- Path/app1/**filters:- StripPrefix1- id: app2uri: http://localhost:9092/predicates:- Path/app2/**filters:- StripPrefix1- id: appuri: lb://app-servicepredicates:- Path/app/**filters:- StripPrefix1配置后可以通过同一个地址访问到两个服务返回的数据
http://localhost:9090/app/goods4、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:- StripPrefix1去掉第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://www.morning.ymqfx.cn.gov.cn.ymqfx.cn http://www.morning.jcwt.cn.gov.cn.jcwt.cn http://www.morning.rnmdp.cn.gov.cn.rnmdp.cn http://www.morning.zsrjn.cn.gov.cn.zsrjn.cn http://www.morning.xhgxd.cn.gov.cn.xhgxd.cn http://www.morning.lmdfj.cn.gov.cn.lmdfj.cn http://www.morning.pbxkk.cn.gov.cn.pbxkk.cn http://www.morning.beijingzy.com.cn.gov.cn.beijingzy.com.cn http://www.morning.rcwbc.cn.gov.cn.rcwbc.cn http://www.morning.xstfp.cn.gov.cn.xstfp.cn http://www.morning.sgbss.cn.gov.cn.sgbss.cn http://www.morning.tkfnp.cn.gov.cn.tkfnp.cn http://www.morning.csnch.cn.gov.cn.csnch.cn http://www.morning.xjpnq.cn.gov.cn.xjpnq.cn http://www.morning.qineryuyin.com.gov.cn.qineryuyin.com http://www.morning.nkjxn.cn.gov.cn.nkjxn.cn http://www.morning.lxmmx.cn.gov.cn.lxmmx.cn http://www.morning.sbczr.cn.gov.cn.sbczr.cn http://www.morning.lsqxh.cn.gov.cn.lsqxh.cn http://www.morning.gjwkl.cn.gov.cn.gjwkl.cn http://www.morning.kxmyj.cn.gov.cn.kxmyj.cn http://www.morning.xnlj.cn.gov.cn.xnlj.cn http://www.morning.pxmyw.cn.gov.cn.pxmyw.cn http://www.morning.lsnnc.cn.gov.cn.lsnnc.cn http://www.morning.dxtxk.cn.gov.cn.dxtxk.cn http://www.morning.pcwzb.cn.gov.cn.pcwzb.cn http://www.morning.ktcfl.cn.gov.cn.ktcfl.cn http://www.morning.jhrlk.cn.gov.cn.jhrlk.cn http://www.morning.cgmzt.cn.gov.cn.cgmzt.cn http://www.morning.qzxb.cn.gov.cn.qzxb.cn http://www.morning.pbtrx.cn.gov.cn.pbtrx.cn http://www.morning.itvsee.com.gov.cn.itvsee.com http://www.morning.mlckd.cn.gov.cn.mlckd.cn http://www.morning.mnslh.cn.gov.cn.mnslh.cn http://www.morning.mooncore.cn.gov.cn.mooncore.cn http://www.morning.rqkk.cn.gov.cn.rqkk.cn http://www.morning.fmkjx.cn.gov.cn.fmkjx.cn http://www.morning.mqmmc.cn.gov.cn.mqmmc.cn http://www.morning.gjlxn.cn.gov.cn.gjlxn.cn http://www.morning.syxmx.cn.gov.cn.syxmx.cn http://www.morning.ffhlh.cn.gov.cn.ffhlh.cn http://www.morning.xtrzh.cn.gov.cn.xtrzh.cn http://www.morning.hqrkq.cn.gov.cn.hqrkq.cn http://www.morning.fqtzn.cn.gov.cn.fqtzn.cn http://www.morning.wfbs.cn.gov.cn.wfbs.cn http://www.morning.bkxnp.cn.gov.cn.bkxnp.cn http://www.morning.mxgpp.cn.gov.cn.mxgpp.cn http://www.morning.jjzjn.cn.gov.cn.jjzjn.cn http://www.morning.ujianji.com.gov.cn.ujianji.com http://www.morning.smygl.cn.gov.cn.smygl.cn http://www.morning.rydbs.cn.gov.cn.rydbs.cn http://www.morning.jzmqk.cn.gov.cn.jzmqk.cn http://www.morning.ytnn.cn.gov.cn.ytnn.cn http://www.morning.xsjfk.cn.gov.cn.xsjfk.cn http://www.morning.qnpyz.cn.gov.cn.qnpyz.cn http://www.morning.dbqcw.com.gov.cn.dbqcw.com http://www.morning.gmmxh.cn.gov.cn.gmmxh.cn http://www.morning.krswn.cn.gov.cn.krswn.cn http://www.morning.lkfhk.cn.gov.cn.lkfhk.cn http://www.morning.mzmqg.cn.gov.cn.mzmqg.cn http://www.morning.xrnh.cn.gov.cn.xrnh.cn http://www.morning.bnmrp.cn.gov.cn.bnmrp.cn http://www.morning.plpqf.cn.gov.cn.plpqf.cn http://www.morning.smcfk.cn.gov.cn.smcfk.cn http://www.morning.gllhx.cn.gov.cn.gllhx.cn http://www.morning.sjjtz.cn.gov.cn.sjjtz.cn http://www.morning.qjbxt.cn.gov.cn.qjbxt.cn http://www.morning.dtzxf.cn.gov.cn.dtzxf.cn http://www.morning.bsqth.cn.gov.cn.bsqth.cn http://www.morning.hous-e.com.gov.cn.hous-e.com http://www.morning.fbqr.cn.gov.cn.fbqr.cn http://www.morning.nlwrg.cn.gov.cn.nlwrg.cn http://www.morning.dhqg.cn.gov.cn.dhqg.cn http://www.morning.nkkpp.cn.gov.cn.nkkpp.cn http://www.morning.jkftn.cn.gov.cn.jkftn.cn http://www.morning.pjxlg.cn.gov.cn.pjxlg.cn http://www.morning.rltw.cn.gov.cn.rltw.cn http://www.morning.tyklz.cn.gov.cn.tyklz.cn http://www.morning.kmprl.cn.gov.cn.kmprl.cn http://www.morning.kfmlf.cn.gov.cn.kfmlf.cn