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

一、本地部署Nacos

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

二、搭建SpringCloud项目

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

  • 搭建了简单的SpringCloud服务后,需要在项目中引入如下依赖:

        
            org.dromara
            soul-spring-boot-starter-client-springcloud
            ${soul.version}
        
  • 修改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

    六、soul源码学习-SpringCloud项目本地运行_第1张图片
    image.png
  • 启动项目,去http://localhost:9095/#/plug/springCloud看,发现注册成功了

    六、soul源码学习-SpringCloud项目本地运行_第2张图片
    image.png

二、网关接入SpringCloud

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

                
  
       org.dromara
       soul-spring-boot-starter-plugin-springcloud
        ${last.version}
  

  
       org.dromara
       soul-spring-boot-starter-plugin-httpclient
       ${last.version}
   
   

   
        org.springframework.cloud
        spring-cloud-commons
        2.2.0.RELEASE
   
   
        org.springframework.cloud
        spring-cloud-starter-netflix-ribbon
        2.2.0.RELEASE
   
  • 如果你使用 eureka 作为 springCloud的注册中心新增如下配置:

       org.springframework.cloud
       spring-cloud-starter-netflix-eureka-client
       2.2.0.RELEASE
  

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

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

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

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

重启网关

三、调用SpringCloud接口

六、soul源码学习-SpringCloud项目本地运行_第3张图片
image.png
六、soul源码学习-SpringCloud项目本地运行_第4张图片
image.png

调用都成功返回



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

你可能感兴趣的:(面试题汇总与解析,spring,sql,java,大数据)