首页 > 作文

springcloud整合gateway实现网关的示例代码

更新时间:2023-04-04 16:22:24 阅读: 评论:0

目录
1.项目目录:2.代码实现:3.实现效果:

1.项目目录:

创建项目gateway作为父类

2.代码实现:

父类依赖

​<parent>    <groupid>org.springframework.boot</groupid>    <artifactid>spring-boot-starter-parent</artifactid>    <version>2.6.2</version>    <relativepath/> <!-- lookup parent from repository -->  </parent>  <groupid>com.cxh</groupid>  <artifactid>gateway</artifactid>  <version>0.0.1-snapshot</version>  <name>gateway</name>  <description>demo project for spring boot</description>  <packaging>pom</packaging>  <properties>    <java.version>8</java.version>    <spring-cloud-alibaba-dependencies.version>2021.1</spring-cloud-alibaba-dependencies.version>    <spring-cloud-dependencies.versi商铺租赁意向书on>2021.0.0</spring-cloud-dependencies.version>  </properties>  <dependencymanagement>    <dependencies>      <dependency>        <groupid>org.springframework.cloud</groupid>        <artifactid>spring-cloud-dependencies</artifactid>        <version>${spring-cloud-dependencies.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-dependencies.version}</version>        <type>pom</type>        <scope>import</scope>      </dependency>    </dependencies>  </dependencymanagement>​

创建module项目gateway-client

添加依赖

<parent>    <groupid>com.cxh</groupid>    <artifactid>gateway</artifactid>    <version>0.0.1-snapshot</version>    <relativepath/> <!-- lookup parent from repository -->  </parent>  <groupid>com.cxh</groupid>  <artifactid>gateway-client</artifactid>  <version>0.0.1-snapshot</version>  <name>gateway-client</name>  <description>demo project for spring boot</description>  <properties>    <java.version>1.8</java.version>  </properties>  <dependencies>    <dependency>      <groupid>org.springframework.boot</groupid>      <artifactid>spring-boot-starter</artifactid>    </dependency>    <dependency>      <groupid>org.springframework.boot</groupid>      <artifactid>spring-boot-starter-test</artifactid>      <scope>test</scope>    </dependency>    <dependency>      <groupid>org.springframework.boot</groupid>      <artifactid>spring-boot-starter</artifactid>    </dependency>    <dependency>      <groupid>org.springframework.boot</groupid>      <artifactid>spring-boot-starter-test</artifactid>      <scope>test</scope>    </dependency>    <!--服务注册-->雨之歌;    <dependency>      <groupid>org.springframework.cloud</groupid>      <artifactid>spring-cloud-starter-alibaba-nacos-discovery</artifactid>      <version>0.2.1.relea</version>    </dependency>    <!--服务调用-->    <dependency>      <groupid>org.springframework.cloud</groupid>      <artifactid>spring-cloud-starter-openfeign</artifactid>    </dependency>    <dependency>      <groupid>org.springframework.boot</groupid>      <artifactid>spring-boot-starter-web</artifactid>    </dependency>  </dependencies>

yml配置

rver: port: 8002spring: application:  name: gateway-client #服务名 profiles:  active: dev #环境设置 cloud:  nacos:   discovery:    rver-addr: 127.0.0.1:8848 #nacos服务注册

控制层

@restcontrollerpublic class clientcontroller {    @value("${rver.port}")    private string port;    @requestmapping("/index")    public string index(){        return "gateway-client端口:" + port;    }}

启动类添加注解

@springbootapplication@enablediscoveryclientpublic class gatewayclientapplication {  public static void main(string[] args) {    springapplication.run(gatewayclientapplication.class, args);  }}

创建module项目gateway-rvice

添加依赖

<parent>    <groupid>com.cxh</groupid>    <artifactid>gateway</artifactid>    <version>0.0.1-snapshot</version>    <relativepath/> <!-- lookup parent from repository -->  </parent>  <groupid>com.cxh</groupid>  <artifactid>gateway-rvice</artifactid>  <v一月一ersion>0.0.1-snapshot</version>  <name>gateway-rvice</name>  <description>demo project for spring boot</description>  <properties>    <java.特大城市2012好玩吗version>1.8</java.version>  </properties>  <dependencies>    <dependency>      <groupid>org.springframework.boot</groupid>      <artifactid>spring-boot-starter</artifactid>    </dependency>    <dependency>      <groupid>org.springframework.boot</groupid>      <artifactid>spring-boot-starter-test</artifactid>      <scope>test</scope>    </dependency>    <dependency>      <groupid>org.springframework.cloud</groupid>      <artifactid>spring-cloud-starter-gateway</artifactid>      <version>3.0.4</version>    </dependency>    <!--服务注册/发现中心依赖-->    <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-clo红色的月亮ud-starter-feign</artifactid>      <version>1.4.3.relea</version>    </dependency>    <dependency>      <groupid>org.springframework.cloud</groupid>      <artifactid>spring-cloud-loadbalancer</artifactid>    </dependency>    <dependency>      <groupid>org.springframework.cloud</groupid>      <artifactid>spring-cloud-starter-netflix-hystrix</artifactid>      <version>2.2.10.relea</version>    </dependency>  </dependencies>

yml配置

rver: port: 8001spring: application:  name: gateway-rvice #服务名 profiles:  active: dev #环境设置 cloud:  gateway:   routes:    # 透传服务    - id: gateway-client #设置路由id     uri: lb://gateway-client #设置路由的url lb://nacos服务注册名称     predicates:      - path=/client/** #路径匹配规则     filters:      - stripprefix=1

跨域配置

@configurationpublic class corsconfig {  @bean  public corswebfilter corsfilter() {    corsconfiguration config = new corsconfiguration();    config.addallowedmethod("*");    config.addallowedorigin("*");    config.addallowedheader("*");    urlbadcorsconfigurationsource source = new urlbadcorsconfigurationsource(new pathpatternparr());    source.registercorsconfiguration("/**", config);    return new corswebfilter(source);  }}

3.实现效果:

启动nacos后,再启动gateway-client, gateway-rvice项目,打开浏览器http://localhost:8001/client/index

返回信息:gateway-client端口:8002

到此这篇关于springcloud整合gateway实现网关的示例代码的文章就介绍到这了,更多相关springcloud gateway网关内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

本文发布于:2023-04-04 16:22:18,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/224fd441a90e7044d4285d8d500c227d.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:springcloud整合gateway实现网关的示例代码.doc

本文 PDF 下载地址:springcloud整合gateway实现网关的示例代码.pdf

标签:项目   代码   目录   网关
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图