0


如何将Springboot项目升级成Springcloud项目(有图详解)

本文以nacos为例

分为以下几个步骤
1. 下载nacos软件
2. pom文件配置
3. application.yml文件配置
4. 代码调用
5. 效果展示

一 . 下载nacos软件

1.1 下载nacos-server-2.2.0-BETA这个版本

1.2 修改nacos配置文件
打开bin目录下的startup.cmd,将第26行的
set MODE=“cluster”
改为:
set MODE=“standalone”

在这里插入图片描述
1.3 双击startup.cmd
如下图则表示启动成功

在这里插入图片描述

二. 项目中引入jar,配置文件pom.xml

Springboot升级为Springcloud版本一定要对应,不然就会有很多稀奇古怪的错误

本文是将2.7.5的Springboot升级为2021.0.7的Springcloud
如图:
在这里插入图片描述

以下是几个关键的配置:
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

完整的pom.xml配置文件

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.5</version><relativePath/><!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>cstudents</artifactId><version>0.0.1-SNAPSHOT</version><name>students</name><description>Demo project forSpringBoot</description><properties><java.version>1.8</java.version><!--<spring-cloud-alibaba-version>2.2.5.RELEASE</spring-cloud-alibaba-version>--></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>2021.0.7</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2021.0.5.0</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><!-- 依赖   --><dependencies><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-bootstrap</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><exclusions><exclusion><artifactId>jsr305</artifactId><groupId>com.google.code.findbugs</groupId></exclusion><exclusion><artifactId>HdrHistogram</artifactId><groupId>org.hdrhistogram</groupId></exclusion></exclusions></dependency><!--启动--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--测试--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--thymeleaf--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--加载JPA--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!--Mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.1</version></dependency><!--阿里云--><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.15</version></dependency><!--连接mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.31</version></dependency><!--分页的插件--><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.4.5</version></dependency><!-- 逆向工程依赖--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.2</version></dependency><!--Mybatis--><!--<dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.4.1</version></dependency>--><!--devtools热部署--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency><!--<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>--></dependencies><!--<pluginRepositories><pluginRepository><id>alimaven spring plugin</id><name>alimaven spring plugin</name><url>https://maven.aliyun.com/repository/spring-plugin</url></pluginRepository></pluginRepositories>--><repositories><repository><id>spring-milestones</id><name>SpringMilestones</name><url>https://repo.spring.io/milestone</url></repository><repository><id>repository.springframework.maven.release</id><name>SpringFrameworkMavenReleaseRepository</name><url>http://maven.springframework.org/milestone/</url></repository><repository><id>org.springframework</id><url> http://maven.springframework.org/snapshot</url></repository><repository><id>spring-milestone</id><name>SpringMavenMILESTONERepository</name><url>http://repo.spring.io/libs-milestone</url></repository><repository><id>spring-release</id><name>SpringMavenRELEASERepository</name><url>http://repo.spring.io/libs-release</url></repository></repositories><build><plugins><!--<plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.4.1</version></plugin>--><!-- 逆向工程插件--><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.6</version><configuration><configurationFile>GeneratorConfig.xml</configurationFile><verbose>true</verbose><overwrite>true</overwrite></configuration></plugin></plugins></build></project>

在升级为Springcloud时,有些jar会有冲突,本文pom.xml文件中注释掉的就是冲突的jar,但是读者得根据自己的实际情况进行增删冲突jar,不过一般就是文中的这几个

三. application.yml文件配置

server:
  port:9080

spring:
  application:
    name: consumer

  cloud:
    nacos:
      discovery:
        server-addr:127.0.0.1:8848

注意事项:
在这里插入图片描述

4. 代码调用:

项目说明,项目有一个消费者(调用方),和两个生产者(被调用方)

4.1 生产者

生产者代码解析:

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

代码:

生产者application.yml:

server:
  port:8081

spring:
  application:
    name: provider

  cloud:
    nacos:
      discovery:
        server-addr:127.0.0.1:8848

生产者调用代码:

本代码中设置了三种情况:
消费者调用时传输一个参数,两个参数,一个集合的三种情况:

packagecom.example.test;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.cloud.context.config.annotation.RefreshScope;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.ResponseBody;importjava.util.Map;@Controller@RefreshScopepublicclassPrivodeController{@Value("${server.port}")String port;//传输一个参数@GetMapping("/hi")@ResponseBodypublicStringhi(@RequestParam(value ="name", defaultValue ="forezp",required =false)String name){System.out.println("666");String msg ="I receive msg 【"+ name +"】"+", i'm provider ,my port:"+ port;System.out.println(msg);return msg;}//传输多个参数@GetMapping("/send")@ResponseBodypublicStringsend1(@RequestParam(value ="city", defaultValue ="Shanghai",required =false)String city,@RequestParam(value ="site", defaultValue ="School",required =false)String site){System.out.println("666");String msg ="My favorite city is "+ city+" , I want to go to "+ site;System.out.println(msg);return msg;}//传输Map@GetMapping("/send2")@ResponseBodypublicStringsend2(@RequestParamMap data){System.out.println("666");String msg ="My name is "+ data.get("name")+" , I am "+ data.get("age")+" years old.";System.out.println(msg);return msg;}}

生产者启动类:

packagecom.example;importorg.mybatis.spring.annotation.MapperScan;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.client.discovery.EnableDiscoveryClient;importorg.springframework.web.bind.annotation.RestController;@MapperScan("com.example.mapper")@SpringBootApplication@RestController@EnableDiscoveryClientpublicclassStudentsApplication{publicstaticvoidmain(String[] args){SpringApplication.run(StudentsApplication.class, args);}}

4.2 消费者

消费者代码解析:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

消费者代码:

消费者application.yml

server:
  port:9080

spring:
  application:
    name: consumer

  cloud:
    nacos:
      discovery:
        server-addr:127.0.0.1:8848

消费者接口:

packagecom.example.test;importorg.springframework.cloud.context.config.annotation.RefreshScope;importorg.springframework.cloud.openfeign.FeignClient;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestParam;importjava.util.Map;@FeignClient(value ="provider")@RefreshScopepublicinterfaceProviderClient{@GetMapping("/hi")Stringhi(@RequestParam(value ="name", defaultValue ="hello", required =false)String name);@GetMapping("/send1")Stringsend1(@RequestParam(value ="city", defaultValue ="Shanghai", required =false)String city,@RequestParam(value ="site", defaultValue ="School", required =false)String site);@GetMapping("/send2")Stringsend2(@RequestParamMap data);}

消费者实现类:

packagecom.example.test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.ResponseBody;importjava.util.HashMap;importjava.util.Map;@ControllerpublicclassConsumerController{@AutowiredProviderClient providerClient;//传输一个参数@GetMapping("/canos")@ResponseBodypublicStringhiFeign(){System.out.println("哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈");return providerClient.hi("你好,我这边发出了信息。。。。。");}//传输多个参数@GetMapping("/test")@ResponseBodypublicStringhiNacos(){System.out.println("666");return providerClient.send1("Beijing","Tiananmen");}//传输Map@GetMapping("/test2")@ResponseBodypublicStringhiNacos2(){System.out.println("777");Map<String,String> data =newHashMap<String,String>();
        data.put("name","Jack");
        data.put("age","18");return providerClient.send2(data);}}

5. 效果展示:

双击启动nacos:

在这里插入图片描述

登录nocas:
用户名和密码默认是:nacos
在这里插入图片描述

启动一个消费者CStudents_C、两个生产者CStudents_P1、CStudents_P2

在这里插入图片描述

观察naocs控制面板:

在这里插入图片描述
可以对服务进行上线下线或者分配权重:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

调用效果展示:

在这里插入图片描述


本文转载自: https://blog.csdn.net/weixin_44079964/article/details/131029238
版权归原作者 君故于时 所有, 如有侵权,请联系我们删除。

“如何将Springboot项目升级成Springcloud项目(有图详解)”的评论:

还没有评论