80端口

80端口默认在url中可以不追加,让用户体验更好。

RunDashBoard

这个和run区分理解,run是对单个文件执行,runDashBoard则是同时运行父项目下的所有微服务。那启用这个RunDashBoard有以下几种方式

通过修改Idea配置

如果重启IDEA没有,那么就在项目中的.idea文件夹中,搜索name = “RunDashBoard”配置以下代码。
image.png

共享实体类

重构工程,解决实体类冗余问题

打包共享模块

clean - install

导入依赖

groupId 根据总模块maven下的groupId
artifactId 根据共享模块下的artifacId
version 直接调用${project.version}
调用包的时候,按照共享模块的,共享文件的(父级目录+共享类的类名)作为全路径来在其他模块调用

服务注册中心

两个概念

server/client

Eureka/
消费者和提供者对应关系太多,服务中心就是用来管理这中间的关系的。
心跳检测:服务注册中心用来检测各个微服务是否都还活动。
ps:服务注册中心一般配置多个,防止单点故障

配置

导包

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  4. </dependency>
  5. <!--boot web actuator-->
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-actuator</artifactId>
  13. </dependency>
  14. <!--一般通用配置-->
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-devtools</artifactId>
  18. <scope>runtime</scope>
  19. <optional>true</optional>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.projectlombok</groupId>
  23. <artifactId>lombok</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-test</artifactId>
  28. <scope>test</scope>
  29. </dependency>
  30. <dependency>
  31. <groupId>junit</groupId>
  32. <artifactId>junit</artifactId>
  33. </dependency>

application

  1. server:
  2. port: 7001
  3. eureka:
  4. instance:
  5. hostname: localhost #eureka服务端的实例名称
  6. client:
  7. register-with-eureka: false #false表示不向注册中心注册自己。
  8. fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
  9. service-url:
  10. #eureka访问地址
  11. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动类
@EnableEurekaServer

  1. package com.msims.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. * @Description Eureka注册中心启动
  6. * @date 2020/11/30 4:27 下午
  7. */
  8. @SpringBootApplication
  9. @EnableEurekaServer
  10. public class EurekaMain7001 {
  11. public static void main(String[] args) {
  12. SpringApplication.run(EurekaMain7001.class, args);
  13. }
  14. }

访问 localhost:7001
image.png

Eureka client

porm

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>

yml

  1. eureka:
  2. client:
  3. #表示是否将自己注册进EurekaServer默认为true。
  4. register-with-eureka: true
  5. #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
  6. fetchRegistry: true
  7. service-url:
  8. #单机版
  9. defaultZone: http://localhost:7001/eureka

主启动

在主启动类加上以下注解

  1. @EnableEurekaClient

运行访问注册中心
注册成功
注意:注册中心开启以后,才能开启其他服务,不然会运行不起来。
image.png
ps:
在客户端模块yml的以下代码,name其实就是注册服务的名,

  1. spring:
  2. application:
  3. name: import-Sample-8001

Eureka集群

其实eureka就是让服务高可用,一般不会是单机版的Eureka,一个Eureka会出现”单点故障”问题。所以要引出集群,实现”高可用,故障容错“

配置文件

在hosts文件中,新增代码框中代码
linux:etc/hosts文件
windows路径为:C:\Windows\System32\drivers\etc
mac : sudo vim /private/etc/hosts
ps : 不能使用localhost和127.0.0.1,这两个名字在给服务端注册名字的时候都没有分别,这让机器怎么让两个服务端之间相互识别?

  1. 127.0.0.1 eureka7001.com
  2. 127.0.0.1 eureka7002.com

配置yml

在端口号为7001的模块配置如下,会发现他在defaultZone中指向了除了他本身以外的其他eureka。不同eureka之间通过defaultZone相互调用

  1. server:
  2. port: 7001
  3. eureka:
  4. instance:
  5. hostname: eureka7001.com #eureka服务端的实例名称
  6. client:
  7. register-with-eureka: false #false表示不向注册中心注册自己。
  8. fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
  9. service-url:
  10. #集群指向其它eureka
  11. defaultZone: http://eureka7002.com:7002/eureka/

在端口号为7002的模块配置如下

  1. server:
  2. port: 7002
  3. eureka:
  4. instance:
  5. hostname: eureka7002.com #eureka服务端的实例名称
  6. client:
  7. register-with-eureka: false #false表示不向注册中心注册自己。
  8. fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
  9. service-url:
  10. #集群指向其它eureka
  11. defaultZone: http://eureka7001.com:7001/eureka/

注:当然,基本的Eureka配置必须有,主启动类注解,导包等等,这里不再赘述。。。
image.png
image.png

微服务集群

client端也通过defaultZone来让多个Eureka管理,用逗号分隔开多个Eureka地址
两个服务名要统一
其他的个性化配置就行。

修改yml

  1. spring:
  2. application:
  3. name: cloud-payment-service
  4. defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

修改controller

  1. @Value("${server.port}")
  2. private String serverPort;

这样我们的客户端集群就已经在Eureka中注册成功了,同一个名字,数量为2。
image.png