1.基本介绍
1.学前说明
2.当前架构分析
1.示意图
2.问题分析
3.引出Eureka
1.项目架构分析
2.上图解读
2.创建单机版的Eureka
1.创建 e-commerce-eureka-server-9001 子模块
2.检查父子pom.xml
1.子 pom.xml
2.父 pom.xml
3.pom.xml 引入依赖
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><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.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></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><dependency><groupId>org.example</groupId><artifactId>e_commerce_center-common-api</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
4.application.yml 配置eureka服务
server:port: 9001
eureka: instance:hostname: localhost client: register-with-eureka: false fetch-registry: false service-url:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
5.com/sun/springcloud/EurekaApplication.java 编写启动类运行测试
package com.sun.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {public static void main(String[] args) {SpringApplication.run(EurekaApplication.class, args);}
}
6.会员中心模块作为Eureka Client示意图
7.member-service-provider-10001 模块作为Eureka Client
1.pom.xml引入Eureka 客户端依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>
2.application.yml 配置Eureka Client
server:port: 10001
spring:application:name: member-service-provider-10001
eureka: client:register-with-eureka: true fetch-registry: true service-url:defaultZone: http://localhost:9001/eureka
3.修改启动类添加注解
8.测试
1.先启动Eureka 服务端,再启动Eureka 客户端
2.访问(http://localhost:9001/) 进入Eureka 服务端
9.服务消费模块作为Eureka Client示意图
10.member-service-consumer-81 模块作为Eureka Client
1.pom.xml 引入Eureka 客户端依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>
2.application.yml 配置Eureka Client
server:port: 81
spring:application:name: member-service-consumer-81
eureka: client:register-with-eureka: true fetch-registry: true service-url:defaultZone: http://localhost:9001/eureka
3.修改启动类添加注解
11.测试
1.先启动Eureka 服务端,再启动Eureka 客户端
2.访问(http://localhost:9001/) 进入Eureka 服务端
12.梳理Service Consumer Service Provider Eureka Server 维护机制
- 简单来说Eureka就两个功能,发现服务,注册服务!
- 基本流程就是会员中心启动之后将自己的信息注册到Eureka Server(注册服务),服务消费者启动之后通过Eureka Server来发现服务(发现服务),得到会员中心的调用地址,然后通过rpc远程调用
13.Eureka自我保护模式
1.基本介绍
2.小结
- 服务在注册之后会不断地向服务端发送心跳,正常情况下如果90s内没有心跳,则服务端会认为这个客户端down掉了,就会清除记录
- 但是一旦自我保护机制开启,即使90s内没有心跳,也不会请求记录
3.禁用自我保护模式
1.服务端配置
server:port: 9001
eureka: instance:hostname: localhost client: register-with-eureka: false fetch-registry: false service-url:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka server:enable-self-preservation: false eviction-interval-timer-in-ms: 2000
2.客户端配置
server:port: 10001
spring:application:name: member-service-provider-10001 datasource:type: com.alibaba.druid.pool.DruidDataSourceurl: username: password:
mybatis:mapper-locations: classpath:mapper/*.xml type-aliases-package: com.sun.springcloud.entity
eureka: client:register-with-eureka: true fetch-registry: true service-url:defaultZone: http://localhost:9001/eureka instance:lease-renewal-interval-in-seconds: 1 lease-expiration-duration-in-seconds: 2
3.启动后访问http://localhost:9001/
4.关闭掉客户端
3.搭建Eureka多服务集群
1.示意图
2.创建e-commerce-eureka-server-9002子模块作为第二个Eureka服务
3.pom.xml 引入依赖(跟9001子模块的一样)
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><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.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></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><dependency><groupId>org.example</groupId><artifactId>e_commerce_center-common-api</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
4.application.yml 配置
server:port: 9002
eureka: instance:hostname: eureka9002.com client: register-with-eureka: false fetch-registry: false service-url:defaultZone: http://eureka9001.com:9001/eureka/
5.修改9001模块的 application.yml 配置完成两个服务相互注册
server:port: 9001
eureka: instance:hostname: eureka9001.com client: register-with-eureka: false fetch-registry: false service-url:defaultZone: http://eureka9002.com:9002/eureka/
6.编写启动类
package com.sun.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication9002 {public static void main(String[] args) {SpringApplication.run(EurekaApplication9002.class, args);}
}
7.配置Hosts运行Eureka
1.解释
- 由于上面配置的两个Eureka服务使用的是域名eureka9001.com和eureka9002.com
- 所以需要配置hosts文件
2.hosts文件所在位置
3.修改Hosts文件
- 这样eureka9002.com和eureka9001.com都表示127.0.0.1了
- 如果这里不能直接修改就把文件复制一份在别的地方修改一下然后覆盖这个文件即可
- 注意:你如果现在开的代理服务器,hosts文件是不生效的!
4.启动这两个服务,访问测试
8.Eureka多个服务端互相注册配置文件理解
1.服务端一(eureka9001.com:9001)
server:port: 9001
eureka: instance:hostname: eureka9001.com client: register-with-eureka: false fetch-registry: false service-url:defaultZone: http://eureka9002.com:9002/eureka/
2.服务端二(eureka9002.com:9002)
server:port: 9002
eureka: instance:hostname: eureka9002.com client: register-with-eureka: false fetch-registry: false service-url:defaultZone: http://eureka9001.com:9001/eureka/
4.将客户端注册到Eureka服务集群
1.示意图
2.修改81客户端的application.yml
server:port: 81
spring:application:name: member-service-consumer-81
eureka: client:register-with-eureka: true fetch-registry: true service-url:defaultZone: http://eureka9001.com:9001/eureka/, http://eureka9002.com:9002/eureka/
3.修改10001客户端的application.yml
server:port: 10001
spring:application:name: member-service-provider-10001
eureka: client:register-with-eureka: true fetch-registry: true service-url:defaultZone: http://eureka9001.com:9001/eureka/, http://eureka9002.com:9002/eureka/
5.启动四个服务
6.测试访问
7.Eureka多个客户端注册多个服务端配置文件理解
1.客户端一(端口81)
server:port: 81
spring:application:name: member-service-consumer-81
eureka: client:register-with-eureka: true fetch-registry: true service-url:defaultZone: http://eureka9001.com:9001/eureka/, http://eureka9002.com:9002/eureka/
2.客户端二(端口10001)
server:port: 10001
spring:application:name: member-service-provider-10001
eureka: client:register-with-eureka: true fetch-registry: true service-url:defaultZone: http://eureka9001.com:9001/eureka/, http://eureka9002.com:9002/eureka/
5.搭建会员中心服务提供方集群
1.示意图
2.基本说明
3.创建 member-service-provider-10002 子模块与10001功能完全一致
4.打开10001模块的文件夹,复制main目录下的文件夹
5.打开10002模块的文件夹,将刚才复制的内容粘贴到main目录下
6.打开idea查看10002模块
7.将10001模块的pom.xml文件依赖粘贴到10002模块(刷新maven)
8.修改10002模块application.yml 的端口为10002
9.修改10002和10001模块的主启动类名称
10.启动所有服务进行测试
11.统一10001服务和10002服务的注册名并启动测试
6.服务消费方(81模块)使用Eureka服务集群
1.示意图
2.修改com/sun/springcloud/controller/MemberConsumerController.java 的前缀为服务提供者在Eureka服务端的key
3.修改com/sun/springcloud/config/CustomizationBean.java 增加调用时负载均衡注解
4.修改两个服务提供方的信息,以便区分
member-service-provider-10001
member-service-provider-10002
5.启动测试
1.启动全部服务
2.查看注册情况
3.postman测试两个服务提供方是否正常
member-service-provider-10001
member-service-provider-10002
4.发送请求到服务提供方 member-service-consumer-81
1.第一次请求
2.第二次请求
7.DiscoveryClient获取服务注册信息
1.示意图
2.需求分析
3.代码实现
1.com/sun/springcloud/controller/MemberConsumerController.java 服务消费者调用DiscoveryClient
1.以接口的形式注入DiscoveryClient
2.编写一个接口完成服务发现
@GetMapping("/member/consumer/discovery")public Object discovery() {List<String> services = discoveryClient.getServices();for (String service : services) {log.info("服务名小写={}", service);List<ServiceInstance> instances = discoveryClient.getInstances(service);for (ServiceInstance instance : instances) {log.info("id={}, host={}, port={}, uri={}", service, instance.getHost(), instance.getPort(), instance.getUri());}}return services;}
3.在启动类使用@EnableDiscoveryClient启动服务发现
4.启动所有服务进行测试
5.注意事项
8.小结
1.项目完整文件目录
2.项目架构图