当前位置: 首页 > news >正文

六、soul源码学习-SpringCloud项目本地运行

一、本地部署Nacos

Nacos本地部署可以参考:http://www.iocoder.cn/Nacos/install/?self

二、搭建SpringCloud项目

示例代码: https://github.com/wyc192273/soul-learn-project/tree/main/sofa-demo

  • 搭建了简单的SpringCloud服务后,需要在项目中引入如下依赖:
<!-- 引入 Soul 针对 Spring Cloud 的集成的依赖 -->
        <dependency>
            <groupId>org.dromara</groupId>
            <artifactId>soul-spring-boot-starter-client-springcloud</artifactId>
            <version>${soul.version}</version>
        </dependency>
  • 修改application.yaml文件,添加如下配置
soul:
  springcloud:
    admin-url: http://localhost:9095
    context-path: /springcloud
    full: true
# adminUrl: 为你启动的soul-admin 项目的ip + 端口,注意要加http://
# contextPath: 为你的这个mvc项目在soul网关的路由前缀,这个你应该懂意思把? 比如/order ,/product 等等,网关会根据你的这个前缀来进行路由.
# full: 设置true 代表代理你的整个服务,false表示代理你其中某几个controller
  • 在实现类上添加@SoulSpringCloudClient注解
@GetMapping("/get")
@SoulSpringCloudClient(path = "/user/get", desc = "获得用户详细")
public String getUser(@RequestParam("id") Integer id) {
  return "DEMO:" + id;
}
  • 设置SpringCloud插件

    需要设置soul的SpringCloud插件为开启状态,访问http://localhost:9095/#/system/plugin

    image.png
  • 启动项目,去http://localhost:9095/#/plug/springCloud看,发现注册成功了

    image.png

二、网关接入SpringCloud

在网关项目中pom引入如下依赖:

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

  <dependency>
       <groupId>org.dromara</groupId>
       <artifactId>soul-spring-boot-starter-plugin-httpclient</artifactId>
       <version>${last.version}</version>
   </dependency>
   <!--soul springCloud plugin end-->

   <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>
  • 如果你使用 eureka 作为 springCloud的注册中心新增如下配置:
<dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
       <version>2.2.0.RELEASE</version>
  </dependency>

在网关的yaml文件中 新增如下配置:

 eureka:
     client:
       serviceUrl:
         defaultZone: http://localhost:8761/eureka/ # 你的eureka地址
     instance:
       prefer-ip-address: true
  • 如果你使用 nacos 作为 springCloud的注册中心新增如下配置:
 <dependency>
       <groupId>com.alibaba.cloud</groupId>
       <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
       <version>2.2.0.RELEASE</version>
 </dependency>

在网关的yaml文件中 新增如下配置:

 spring:
     cloud:
       nacos:
         discovery:
            server-addr: 127.0.0.1:8848 # 你的nacos地址

重启网关

三、调用SpringCloud接口

image.png
image.png

调用都成功返回



喜欢的朋友记得点赞、收藏、关注哦!!!

http://www.dtcms.com/a/26626.html

相关文章:

  • 【核心算法篇十七】《深度解析DeepSeek概率图模型:贝叶斯网络推理引擎的技术内核》
  • 【如何基于Debian构建Kali Linux】
  • Linux-进程概念
  • 配置haproxy实现MySQL服务器负载均衡
  • 【技术前沿】中安购物小票识别系统:1秒内精准识别,助力商场智能化升级!
  • 撕碎QT面具(7):container控件被spacer挤扁,无法进行控件添加的处理方案。
  • YOLOv12从入门到入土(含结构图)
  • Mysql测试连接失败
  • 【机器学习】K折交叉验证(K-Fold Cross-Validation)
  • Jenkins插件管理切换国内源地址
  • 基于STM32+ESP8266+手机 APP+阿里云的智能小车设计与实现(系统资料)
  • B树和B+树
  • 2013年下半年试题一:论软件架构建模技术与应用
  • 堆和栈的区别
  • 自制操作系统分享第四天
  • 数据库服务器是指什么?
  • 每日一题——编辑距离
  • DeepSeek 助力 Vue 开发:打造丝滑的瀑布流布局(Masonry Layout)
  • 【读取filePath这个文件中的内容,并打印出来】+【if else 的优化】
  • ubuntu 磁盘恢复
  • 假期学习总结(25.2.19)
  • Ubuntu USB耳机找不到设备解决
  • 力士乐伺服电机MSK系列型号
  • 初识Redis
  • DeepSeek 解析
  • 深入理解 lua_KFunction 和 lua_CFunction
  • 网络安全java练习平台 js网络安全
  • 大数据组件(四)快速入门实时数据湖存储系统Apache Paimon(2)
  • 【Linux】【网络】Libevent整个的使用流程总结(与接口函数结合)
  • 关于SOC与CPU的那些事