微服务学习笔记(一)

201 阅读2分钟

微服务组件

服务注册中心:

Eureka Zookeeper Consul Nacos

服务调用

Ribbon LoadBalancer Feign OpenFeign

服务熔断降级

Hystrix Resillence4J Sentinel

服务网关

Zuul Gateway

服务配置

Config Apollo Nacos

服务总线

Bus Nacos

环境搭建(Idea 2020.2版)

设置字符集为UTF-8

File -> Settings -> Editer -> File Encoding :

  • Global Encoding: UTF-8
  • Project Encoding: UTF-8
  • Default encoding for properties file: UTF-8
  • Transparent nativ to ASCII conversion: Checked

启动注解

File -> Settings -> Build,Execution,Deployment -> Compiler -> Annotation Processors: Enable annotation processing: Checked

设置JDK版本

File -> Settings -> Build,Execution,Deployment -> Compiler -> Java compiler: 在Per-module bytecode version 中设置Target bytecode version 为8

设置父工程POM

  1. 设置打包类型为pom<packaging>pom</packaging>
  2. 添加依赖版本号
<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.3.4.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Hoxton.SR10</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>2.2.5.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.20</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.23</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.4</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.16</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.1</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

注意: 你可以将以上依赖版本放置到properties中. 另外,这里并没有使用如下所示的springboot starter的方式引入相关spring boot依赖,因为这种方式相当于一个依赖的集合,但是其中有些版本跟可能会跟spring cloud冲突. 建议还是按照上面的方式来引入

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.3.4.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>

开启热部署

为了防止修改代码后反复启停调试,我们可以开启热部署

  1. 引入依赖
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <version>2.3.4.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
  </dependency>
  1. 添加插件
  <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
      <fork>true</fork>
      <addResources>true</addResources>
    </configuration>
  </plugin>
  1. 开启选项 File -> Settings -> Compiler,选中以下选项
  • Automatically show first error in editor
  • Display notification on build completion
  • Build project automatically
  • Compile independent module in parallel
  1. 添加注册 同时按 ctrl alt shift / 四个键,在弹出的对话框中选择 register,然后选中以下两个选项 compiler.automake.allow.when.app.running actionSystem.assertFocusAccessFromEdt
  2. 重启Idea

启动Run Dashboard

在Idea 2020版本中,使用Service取代了之前的Run Dashboard来同时管理多个微服务的运行. 具体操作如下: View -> Tool Window -> Services -> 在窗口中点击右上角的"+", 添加Application即可显示你所有的微服务 参考链接:blog.csdn.net/zhang187331…