Soul 网关(五)---- Spring Cloud 插件

Spring Cloud 插件介绍

该插件是用来将http协议 转成springCloud协议 的核心。soul会从springCloud的注册中心上面,根据应用名称获取对应的服务真实ip地址,发起http代理调用。

Spring Cloud 插件使用

1.首先启动 soul-admin

2.然后在 soul-bootstrap 的po m文件里添加soul-spring-boot-starter-plugin-springcloud 以及 eureka的依赖、

       <dependency>
            <groupId>org.dromara</groupId>
            <artifactId>soul-spring-boot-starter-plugin-springcloud</artifactId>
            <version>${
     project.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-commons</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
         
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>

3.这里我们使用 Eureka 作为 Spring Cloud的注册中心

4.启动 example 下的 springcloud-test,可以看到日志里打印了注册到 Eureka 相关的日志

2021-01-20 00:21:02.785  INFO 94452 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2021-01-20 00:21:02.786  INFO 94452 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2021-01-20 00:21:02.786  INFO 94452 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2021-01-20 00:21:02.786  INFO 94452 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Application is null : false
2021-01-20 00:21:02.786  INFO 94452 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2021-01-20 00:21:02.786  INFO 94452 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Application version is -1: true
2021-01-20 00:21:02.786  INFO 94452 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2021-01-20 00:21:02.848  INFO 94452 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SPRINGCLOUD-TEST/192.168.3.2:springCloud-test:8884: registering service...
2021-01-20 00:21:02.979  INFO 94452 --- [tbeatExecutor-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SPRINGCLOUD-TEST/192.168.3.2:springCloud-test:8884 - Re-registering apps/SPRINGCLOUD-TEST
2021-01-20 00:21:02.979  INFO 94452 --- [tbeatExecutor-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SPRINGCLOUD-TEST/192.168.3.2:springCloud-test:8884: registering service...
2021-01-20 00:21:02.999  INFO 94452 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : The response status is 200
2021-01-20 00:21:03.042  INFO 94452 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SPRINGCLOUD-TEST/192.168.3.2:springCloud-test:8884 - registration status: 204
2021-01-20 00:21:03.042  INFO 94452 --- [tbeatExecutor-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SPRINGCLOUD-TEST/192.168.3.2:springCloud-test:8884 - registration status: 204
2021-01-20 00:21:33.008  INFO 94452 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2021-01-20 00:21:33.009  INFO 94452 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2021-01-20 00:21:33.009  INFO 94452 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2021-01-20 00:21:33.009  INFO 94452 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Application is null : false
2021-01-20 00:21:33.009  INFO 94452 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2021-01-20 00:21:33.009  INFO 94452 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Application version is -1: false
2021-01-20 00:21:33.009  INFO 94452 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2021-01-20 00:21:33.142  INFO 94452 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : The response status is 200

同时在 Eureka 注册中心可以看到 spring cloud-test 服务已经注册上来了:
Soul 网关(五)---- Spring Cloud 插件_第1张图片

5.最后 springcloud-test 中的接口就注册到soul网关了,可以通过soul来访问Spring Cloud 项目

总结

  1. Soul 可以方便快捷的接入 Spring Cloud 项目,且对代码几乎没有侵入性

你可能感兴趣的:(Soul,网关源码)