java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Springboot升级成Springcloud

将Springboot项目升级成Springcloud项目的图文教程

作者:君故于时

本文主要介绍了将Springboot项目升级成Springcloud项目,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

本文以nacos为例

分为以下几个步骤

一 . 下载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 for Spring Boot</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>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
        <repository>
            <id>repository.springframework.maven.release</id>
            <name>Spring Framework Maven Release Repository</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>Spring Maven MILESTONE Repository</name>
            <url>http://repo.spring.io/libs-milestone</url>
        </repository>
        <repository>
            <id>spring-release</id>
            <name>Spring Maven RELEASE Repository</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.1 生产者

生产者代码解析:

代码:

生产者application.yml:

server:
  port: 8081
spring:
  application:
    name: provider
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

生产者调用代码:

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

package com.example.test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;
@Controller
@RefreshScope
public class PrivodeController {
    @Value("${server.port}")
    String port;
    //传输一个参数
    @GetMapping("/hi")
    @ResponseBody
    public String hi(@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")
    @ResponseBody
    public String send1(@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")
    @ResponseBody
    public String send2(@RequestParam Map 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;
    }
}

生产者启动类:

package com.example;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RestController;
@MapperScan("com.example.mapper")
@SpringBootApplication
@RestController
@EnableDiscoveryClient
public class StudentsApplication {
    public static void main(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

消费者接口:

package com.example.test;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Map;
@FeignClient(value = "provider" )
@RefreshScope
public interface ProviderClient {
    @GetMapping("/hi")
    String hi(@RequestParam(value = "name", defaultValue = "hello", required = false) String name);
    @GetMapping("/send1")
    String send1(@RequestParam(value = "city", defaultValue = "Shanghai", required = false) String city,
                @RequestParam(value = "site", defaultValue = "School", required = false) String site);
    @GetMapping("/send2")
    String send2(@RequestParam  Map data);
}

消费者实现类:

package com.example.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@Controller
public class ConsumerController {
    @Autowired
    ProviderClient providerClient;
    //传输一个参数
    @GetMapping("/canos")
    @ResponseBody
    public String hiFeign(){
        System.out.println("哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈");
        return providerClient.hi("你好,我这边发出了信息。。。。。");
    }
    //传输多个参数
    @GetMapping("/test")
    @ResponseBody
    public String hiNacos(){
        System.out.println("666");
        return providerClient.send1("Beijing","Tiananmen");
    }
    //传输Map
    @GetMapping("/test2")
    @ResponseBody
    public String hiNacos2(){
        System.out.println("777");
        Map<String,String> data = new HashMap<String,String>();
        data.put("name","Jack");
        data.put("age","18");
        return providerClient.send2(data);
    }
}

五. 效果展示:

双击启动nacos:

登录nocas:

用户名和密码默认是:nacos

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

观察naocs控制面板:

可以对服务进行上线下线或者分配权重:

调用效果展示:

 到此这篇关于将Springboot项目升级成Springcloud项目的图文教程的文章就介绍到这了,更多相关Springboot升级成Springcloud内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文