带你快速体验一次简单的Spring Cloud的微服务(玩具服务)————附带详细代码

带你快速体验一次简单的Spring Cloud的微服务(玩具服务)————附带详细代码,第1张

文章目录
  • 0 背景
  • 1 创建项目
  • 2 创建微服务
  • 附录
    • 父项目的pom.xml
    • 子pom.xml

0 背景

本文只是为了给想体验Spring Cloud微服务项目的同学快速上手的一个玩具项目(本人也并非专业的Java网页开发,也只是想花很短的时间体验一下微服务),目的只有一个体验一下Spring Cloud。这里首先创建一个简单Sprong Boot项目,下一期再进行微服务的 *** 作,例如服务注册、负载均衡、容错保护(服务降级、服务熔断、请求缓存、请求合并、依赖隔离)等。

如果想系统的学习,可以根据相关的书籍进行学习。

1 创建项目

由于一个微服务项目中包含着很多子项目,这里首先建立一个大的父项目,然后再在父项目下创建其余的子项目。

这里开始创建父项目:

  • 1,新建一个Spring Initializr项目(如果觉得国外的服务器(Server URL)网址慢,可以换成国内的),根据自己情况填写Name(项目名称)、Group(项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构)、Artifact(项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称、Package name(包名)、Project SDD(项目的Software Development Kit)、Java(Java版本);

  • 2,选择SPring Boot的版本,然后选择相关的依赖(这里未进行任何依赖的选择,后面.pom文件中可以配置);

  • 3,创建完成后,删除其余的文件,只保留如下面第二张图的结构;


  • 4,更改maven仓库配置,设置自己的Mavne home path(maven版本),Local resposity仓库,Use setting file配置文件,然后点击Application,再点击Save


    然后再设置pom.xml自动更新,勾选Any Changes


  • 5,配置父pom.xml(完整配置见附录);

添加父模块:

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.13.RELEASEversion>
        <relativePath/> 
    parent>

项目信息(packaging不添加,会报错'packaging' with value 'jar' is invalid):

    <groupId>com.jkgroupId>
    <artifactId>SpringCloudTest2artifactId>
    <version>1.0-SNAPSHOTversion>
    <name>SpringCloudTest2name>
    <packaging>pompackaging>
    <description>SpringCloudTest2description>

添加指定版本的说明:

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <jacoco.version>0.8.3jacoco.version>
    properties>

添加相关依赖(这里多了一个jacoco,用于单元测试):

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>org.jacocogroupId>
            <artifactId>jacoco-maven-pluginartifactId>
            <version>0.8.3version>
        dependency>
    dependencies>

添加构建说明(这里主要都是jacoco的构建):

    <build>
        <plugins>
            <plugin>
                <groupId>org.jacocogroupId>
                <artifactId>jacoco-maven-pluginartifactId>
                <version>${jacoco.version}version>
                <configuration>
                    <includes>
                        <include>**/*include>
                    includes>
                    <destFile>SpringCloudTest/cloud-jk-1/target/jacoco.execdestFile>
                    
                    <rules>
                        <rule implementation="org.jacoco.maven.RuleConfiguration">
                            <element>BUNDLEelement>
                            <limits>  
                                
                                <limit implementation="org.jacoco.report.check.Limit">
                                    <counter>METHODcounter>
                                    <value>COVEREDRATIOvalue>
                                    <minimum>0.50minimum>
                                limit>
                                
                                <limit implementation="org.jacoco.report.check.Limit">
                                    <counter>BRANCHcounter>
                                    <value>COVEREDRATIOvalue>
                                    <minimum>0.50minimum>
                                limit>
                                
                                <limit implementation="org.jacoco.report.check.Limit">
                                    <counter>CLASScounter>
                                    <value>MISSEDCOUNTvalue>
                                    <maximum>0maximum>
                                limit>
                            limits>
                        rule>
                    rules>
                configuration>
                <executions>
                    <execution>
                        <id>pre-testid>
                        <goals>
                            <goal>prepare-agentgoal>
                        goals>
                    execution>
                    <execution>
                        <id>post-testid>
                        <phase>testphase>
                        <goals>
                            <goal>reportgoal>
                        goals>
                    execution>
                executions>
            plugin>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

2 创建微服务
  • 1,创建子模块;

  • 2,配置内容根据自己实际情况填写,然后选择maven版本后,点击完成;

  • 3,修改pom.xml

添加父模块:

    <parent>
        <artifactId>SpringCloudTest2artifactId>
        <groupId>com.jkgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>

添加项目信息:

    <artifactId>cloud-1artifactId>
    <name>cloud-1name>
    <description>cloud-1description>

添加版本说明:

    <properties>
        <java.version>1.8java.version>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <spring-cloud.version>Greenwich.SR5spring-cloud.version>
    properties>

添加依赖:


添加依赖管理:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

添加构建:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombokgroupId>
                            <artifactId>lombokartifactId>
                        exclude>
                    excludes>
                configuration>
            plugin>
        plugins>
    build>
  • 4,添加Spring Boot的组件,新建entity文件夹(实体)和Student类;

编写如下代码(lombok会自动帮忙生成实体Bean中大量的Getter/Setter方法,以及toString, hashCode代码):

package com.jk.cloud1.entity;

import lombok.Data;

@Data
public class StudentEntity {

    //姓名
    private String name;
    //年龄
    private int age;
    //备注
    private String remark;
}

  • 5,添加Spring Boot的组件,新建controller文件夹和StudentController类;

编写如下代码:

package com.jk.cloud1.controller;

import com.jk.cloud1.entity.StudentEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.apache.commons.lang.StringUtils;


/**
 *  

* 学生模块,控制器 *

* */
@RestController @RequestMapping(value = "/student") public class StudentController { /** * 获取一个学生 * @return JSON对象 */ @GetMapping(value = "/detail") public Object getStudent () throws InterruptedException { //模拟超时(用于显示熔断器Hystrix的作用) // TimeUnit.SECONDS.sleep(6); StudentEntity student = new StudentEntity(); student.setName("张三"); student.setAge(18); student.setRemark(""); return student; } @GetMapping(value = "/detail2/{var}") public Object getStudent2(@PathVariable String var) throws InterruptedException{ StudentEntity student = new StudentEntity(); student.setName("张三"); student.setAge(18); if (StringUtils.isNotEmpty(var) ){ student.setRemark(var); }else { student.setRemark(""); } return student; } }
  • 7,编写应用网页端口和ip,文件如下的路径,把application.properities改为application.yml(properities的优先级高,yml的文件编码风格更好);

  • 7,编写单元测试用例,路径如下图(如果进行测试,可以删除test文件夹);

    编写如下代码:

package com.jk.cloud1;

import com.jk.cloud1.controller.StudentController;
import com.jk.cloud1.StudentControllerTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Cloud1Application.class)
public class StudentControllerTest {
    @Resource
    StudentController studentController;

    @Test
    public void test() throws InterruptedException {
        studentController.getStudent2("handsome");
    }
}
  • 8,运行启动应用(Cloud1Application.java);

  • 9,访问接口对应的网页;

至此spring cloud的项目可以说已经小小的体验了一把。

附录 父项目的pom.xml

<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.0modelVersion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.13.RELEASEversion>
        <relativePath/> 
    parent>

    <modules>
        <module>
            cloud-1
        module>
    modules>

    <groupId>com.jkgroupId>
    <artifactId>SpringCloudTest2artifactId>
    <version>1.0-SNAPSHOTversion>
    <name>SpringCloudTest2name>
    <packaging>pompackaging>
    <description>SpringCloudTest2description>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <jacoco.version>0.8.3jacoco.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>

        <dependency>
            <groupId>org.jacocogroupId>
            <artifactId>jacoco-maven-pluginartifactId>
            <version>0.8.3version>
        dependency>
    dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.jacocogroupId>
                <artifactId>jacoco-maven-pluginartifactId>
                <version>${jacoco.version}version>
                <configuration>
                    <includes>
                        <include>**/*include>
                    includes>
                    <destFile>SpringCloudTest/cloud-jk-1/target/jacoco.execdestFile>
                    
                    <rules>
                        <rule implementation="org.jacoco.maven.RuleConfiguration">
                            <element>BUNDLEelement>
                            <limits>  
                                
                                <limit implementation="org.jacoco.report.check.Limit">
                                    <counter>METHODcounter>
                                    <value>COVEREDRATIOvalue>
                                    <minimum>0.50minimum>
                                limit>
                                
                                <limit implementation="org.jacoco.report.check.Limit">
                                    <counter>BRANCHcounter>
                                    <value>COVEREDRATIOvalue>
                                    <minimum>0.50minimum>
                                limit>
                                
                                <limit implementation="org.jacoco.report.check.Limit">
                                    <counter>CLASScounter>
                                    <value>MISSEDCOUNTvalue>
                                    <maximum>0maximum>
                                limit>
                            limits>
                        rule>
                    rules>
                configuration>
                <executions>
                    <execution>
                        <id>pre-testid>
                        <goals>
                            <goal>prepare-agentgoal>
                        goals>
                    execution>
                    <execution>
                        <id>post-testid>
                        <phase>testphase>
                        <goals>
                            <goal>reportgoal>
                        goals>
                    execution>
                executions>
            plugin>


            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

子pom.xml

<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.0modelVersion>

    <parent>
        <artifactId>SpringCloudTest2artifactId>
        <groupId>com.jkgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>cloud-1artifactId>
    <name>cloud-1name>
    <description>cloud-1description>

    <properties>
        <java.version>1.8java.version>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <spring-cloud.version>Greenwich.SR5spring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>commons-langgroupId>
            <artifactId>commons-langartifactId>
            <version>2.6version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-websocketartifactId>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>

    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombokgroupId>
                            <artifactId>lombokartifactId>
                        exclude>
                    excludes>
                configuration>
            plugin>
        plugins>
    build>

project>

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/791319.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存