12-2108微服务课上问题分析及总结

Day01~微服务架构入门

核心知识点

  • 微服务架构诞生的背景(分而治之~北京一个火车站到多个火车站)
  • 微服务架构解决方案(大厂自研,Spring Cloud ~Netflix,Alibaba,…)
  • 微服务架构下Maven聚合项目的创建方式?(Maven聚合项目~资源复用,简化编译,打包,部署方式)
  • 微服务架构入门聚合项目创建?(01-sca,sca-consumer,sca-provider,sca-gateway,sca-common)
  • 微服务中聚合工程之间的引用设计?(将一个工程作为依赖添加到其它工程)

常见问题分析

  • 为什么需要微服务?(对系统分而治,解决因并发访问过大带来的系统复杂性(例如:业务,开发,测试,升级,可靠性等)
  • 微服务设计的特点?(单一职责,独立进程,开发测试效率高,可靠性高,升级难度小,但会带来一定的维护成本)
  • 微服务解决方案有哪些?(大厂自研,中小企业采用开源Spring Cloud Alibaba,Spring Cloud Netfix等 )
  • 微服务设计中需要哪些关键组件(服务的注册,发现,配置,限流降级,访问入口管理,分布式事务管理等)
  • 创建聚合工程的目的?(实现工程之间资源的的共享,简化工程管理)
  • 如何修改聚合工程中项目的编译和运行版本?(build->plugins->plugin->maven-compiler-plugin)
  • maven工程中build元素的作用?(定义项目的编译,打包方式)
  • maven父工程的packaging元素内的值是什么?(pom)
  • maven父工程中dependencyManagement元素的作用是什么?(定义项目的版本,当前工程或子工程不需要再指定版本)
  • Maven父工程中如何统一定义JDK编译和运行版本?(配置maven编译插件)

常见Bug分析

  • 依赖无法下载或加载?(本地库冲突,网络不好,maven镜像配置,指定版本在远程服务器不存在,清idea缓存后重启)
  • 项目的pom.xml文件有删除线?(idea/setting/build,Execution,Deployment/build Tools/maven/ignore Files)
  • 项目单元测试失败,提示找不到@SpringBootConfiguration,例如:
    在这里插入图片描述
  • 当项目中出现了多个启动类时,在项目启动或单元测试时会出现如下问题,例如:
    在这里插入图片描述
  • 单元测试依赖添加的有问题或者单元测试类写错了位置,例如:
    在这里插入图片描述
  • 方法调用错误,例如:
    在这里插入图片描述
  • 空指针异常,例如:
    在这里插入图片描述

课堂练习

练习一:公共工程设计及实现

第一步:创建01-sca工程的子工程,工程模块名sca-common,例如:
在这里插入图片描述
第二步:在sca-common模块工程中创建一个工具类com.jt.common.util.StringUtils,并在类中添加一个判断字符串是否为空的静态方法。

package com.jt.common.util;

public class StringUtils {
    /**
     * 通过此方法判定传入的字符串是否为空
     * @param str
     * @return
     */
    public static boolean isEmpty(String str){
        return str==null||"".equals(str);
    }
}

第三步:将sca-common工程以依赖的方式添加到sca-provider工程中?

<dependencies>
    <!--添加springboot web依赖(此依赖中包含了springboot工程需要的很多基础依赖)-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--添加sca-common依赖-->
    <dependency>
        <groupId>com.jt</groupId>
        <artifactId>sca-common</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

第四步:在sca-provider工程中添加一个springboot启动类,类名为com.jt.ProviderApplication

package com.jt;
import com.jt.common.cache.DefaultCache;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class ProviderApplication{
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class,args);
    }
}

第五步:在sca-provider工程中写一个单元测试类,类全名为com.jt.util.StringTests,并添加单元测试方法,在这个单元测试方法中使用sca-common工程中写的StringUtils类,测试一个字符串是否为空.

package com.jt.util;
import com.jt.common.util.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class StringTests {
    @Test //org.junit.jupiter.api.Test
    void testStringEmpty(){
         String str="";
         boolean empty = StringUtils.isEmpty(str);
         System.out.println(empty);
    }
}

练习二:熟悉工程中对象的管理以及测试

第一步:在sca-common工程中定义一个com.jt.common.cache.DefaultCache类型.

package com.jt.common.cache;
public class DefaultCache {
}

第二步:在sca-provider工程将DefaultCache类型的对象交给spring管理,写到启动类即可。

package com.jt;
import com.jt.common.cache.DefaultCache;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class ProviderApplication{
    @Bean
    public DefaultCache defaultCache(){//将DefaultCache对象交给spring管理
        return new DefaultCache();
    }
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class,args);
    }
}

第三步:在sca-provider工程中定义个单元测试类DefaultCache,并且在类中定义一个
单元测试方法,输出DefaultCache对象的toString方法的返回值。

package com.jt.util;

import com.jt.common.cache.DefaultCache;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class DefaultCacheTests {
    @Autowired //DI
    private DefaultCache defaultCache;
    @Test
    void testDefaultCache(){
        System.out.println(defaultCache.toString());
    }
}

课后作业

  • 总结课上知识点
  • 完成自己电脑中JAVA_HOME环境变量的配置。(JDK必须是8并且64Bit的)
  • 确保自己电脑中的mysql为5.7以上或者mariadb10.5以上版本并基于这个版本执行课前资料中的nacos-mysql.sql
  • 预习04-Nacos注册服务注册中心应用实践。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码

)">
< <上一篇
下一篇>>