文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

springcloud整合gateway怎么实现网关

2023-06-26 10:00

关注

这篇文章主要介绍了springcloud整合gateway怎么实现网关的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇springcloud整合gateway怎么实现网关文章都会有所收获,下面我们一起来看看吧。

1.项目目录:

springcloud整合gateway怎么实现网关

创建项目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.version>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.RELEASE</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配置

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

控制层

@RestControllerpublic class ClientController {    @Value("${server.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-service

添加依赖

<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-service</artifactId>    <version>0.0.1-SNAPSHOT</version>    <name>gateway-service</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.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-cloud-starter-feign</artifactId>            <version>1.4.3.RELEASE</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.RELEASE</version>        </dependency>    </dependencies>

yml配置

server:  port: 8001spring:  application:    name: gateway-service #服务名  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("*");        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());        source.registerCorsConfiguration("/**", config);        return new CorsWebFilter(source);    }}

3.实现效果:

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

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

关于“springcloud整合gateway怎么实现网关”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“springcloud整合gateway怎么实现网关”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网行业资讯频道。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     801人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     348人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     311人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     432人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     220人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯