80端口
RunDashBoard
这个和run区分理解,run是对单个文件执行,runDashBoard则是同时运行父项目下的所有微服务。那启用这个RunDashBoard有以下几种方式
通过修改Idea配置
如果重启IDEA没有,那么就在项目中的.idea文件夹中,搜索name = “RunDashBoard”配置以下代码。
共享实体类
重构工程,解决实体类冗余问题
打包共享模块
导入依赖
groupId 根据总模块maven下的groupId
artifactId 根据共享模块下的artifacId
version 直接调用${project.version}
调用包的时候,按照共享模块的,共享文件的(父级目录+共享类的类名)作为全路径来在其他模块调用
服务注册中心
两个概念
server/client
Eureka/
消费者和提供者对应关系太多,服务中心就是用来管理这中间的关系的。
心跳检测:服务注册中心用来检测各个微服务是否都还活动。
ps:服务注册中心一般配置多个,防止单点故障
配置
导包
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><!--boot web actuator--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--一般通用配置--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency>
application
server:port: 7001eureka:instance:hostname: localhost #eureka服务端的实例名称client:register-with-eureka: false #false表示不向注册中心注册自己。fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务service-url:#eureka访问地址defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启动类@EnableEurekaServer
package com.msims.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @Description Eureka注册中心启动* @date 2020/11/30 4:27 下午*/@SpringBootApplication@EnableEurekaServerpublic class EurekaMain7001 {public static void main(String[] args) {SpringApplication.run(EurekaMain7001.class, args);}}
Eureka client
porm
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>
yml
eureka:client:#表示是否将自己注册进EurekaServer默认为true。register-with-eureka: true#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡fetchRegistry: trueservice-url:#单机版defaultZone: http://localhost:7001/eureka
主启动
在主启动类加上以下注解
@EnableEurekaClient
运行访问注册中心
注册成功
注意:注册中心开启以后,才能开启其他服务,不然会运行不起来。
ps:
在客户端模块yml的以下代码,name其实就是注册服务的名,
spring:application: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,这两个名字在给服务端注册名字的时候都没有分别,这让机器怎么让两个服务端之间相互识别?
127.0.0.1 eureka7001.com127.0.0.1 eureka7002.com
配置yml
在端口号为7001的模块配置如下,会发现他在defaultZone中指向了除了他本身以外的其他eureka。不同eureka之间通过defaultZone相互调用
server:port: 7001eureka:instance:hostname: eureka7001.com #eureka服务端的实例名称client:register-with-eureka: false #false表示不向注册中心注册自己。fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务service-url:#集群指向其它eurekadefaultZone: http://eureka7002.com:7002/eureka/
在端口号为7002的模块配置如下
server:port: 7002eureka:instance:hostname: eureka7002.com #eureka服务端的实例名称client:register-with-eureka: false #false表示不向注册中心注册自己。fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务service-url:#集群指向其它eurekadefaultZone: http://eureka7001.com:7001/eureka/
注:当然,基本的Eureka配置必须有,主启动类注解,导包等等,这里不再赘述。。。
微服务集群
client端也通过defaultZone来让多个Eureka管理,用逗号分隔开多个Eureka地址
两个服务名要统一
其他的个性化配置就行。
修改yml
spring:application:name: cloud-payment-servicedefaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
修改controller
@Value("${server.port}")private String serverPort;
这样我们的客户端集群就已经在Eureka中注册成功了,同一个名字,数量为2。
