Dubbo结合Gateway实现微服务网关

前言

最近,我发布了Dubbo Demo项目以及Gateway网关的博客,于是奇思妙想到能不能将两者结合起来呢?首先我们可以在Dubbo的官网中看了开发者文档,它推荐使用的网关只有三种KongDubbo Proxy以及Zuul,而Gateway被认为是Zuul的替代品,所以从理论上是完全可行,所以来吧,展示!

Nacos安装以及其使用可参考我的博客:Nacos服务治理中心和配置中心

Gateway安装及使用可参考我的博客:微服务网关-Gateway

正文

网关模块:spring-dubbo-gateway

注入依赖

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        <version>2.1.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
        <version>2.1.2.RELEASE</version>
    </dependency>
</dependencies>


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>Greenwich.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

application.properties:配置文件

server.port=9000
spring.application.name=spring-dubbo-gateway
spring.main.allow-bean-definition-overriding=true

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true
spring.cloud.gateway.routes[0].id=gateway-producer-service
spring.cloud.gateway.routes[0].uri=lb://spring-dubbo-producer
spring.cloud.gateway.routes[0].predicates[0]=Path=/producer/**

代理服务模块:spring-dubbo-producer

注入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>0.9.0.RELEASE</version>
</dependency>

<!--dubbo-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.6</version>
</dependency>

<!-- Nacos Spring dependency -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo-registry-nacos</artifactId>
    <version>2.6.6</version>
</dependency>

<dependency>
    <groupId>com.alibaba.nacos</groupId>
    <artifactId>nacos-client</artifactId>
    <version>0.6.1</version>
</dependency>

<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
</dependency>

application.properties:配置文件

server.port=8080
spring.application.name=spring-dubbo-producer
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

验证

启动项目在Nacos可看到服务

验证

测试接口

@RestController
@Slf4j
@RequestMapping("/producer")
public class UserController {

    @GetMapping("/helloword")
    public String hello(String input){
        return "你好,"+input;
    }
}

访问测试接口:
127.0.0.1:9000/producer/helloword?input=溪源的奇思妙想
在这里插入图片描述

测试接口

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐