Eureka服务注册与发现
服务治理
服务注册
注意:CS是客户端和服务端
两组件
Eureka Server
Eureka Server 相当于一个商场,商场里的各个店铺都需要在商场进行签合同(注册)来在商场里开店
注意:在 Eureka Server 注册中心的启动类上添加 @EnableEurekaServer 注解
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
// @EnableEurekaServer 服务注册中心
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class, args);
}
}
在 pom.xml 添加以下依赖
<!-- Eureka Server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
在 application.yml 添加以下配置
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com # eureka 服务端的实例名称
client:
# false 表示不向注册中心注册自己
register-with-eureka: false
# false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
# defaultZone: http://eureka7002.com:7002/eureka/ # 集群版
defaultZone: http://eureka7001.com:7001/eureka/ # 单机版
Eureka Client
Eureka Client 相当于商场的店铺,每隔一个月交租金(每隔30秒向Eureka Server发送心跳),如果商场几个月没有收到某个店铺的租金(Eureka在多个心跳周期内没有收到某个节点的心跳),商场将会与该店铺结束合同(Eureka Server 将会从服务注册表中把这个服务节点移除)
注意:在 Eureka Client 客户端(在 Eureka Server 注册中心注册的各个服务)的启动类上添加 @EnableEurekaClient 注解
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient // 该注解用于将服务注册进注册中心
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}
在 pom.xml 添加以下依赖
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
在 application.yml 里添加以下配置
server:
port: 8001
eureka:
client:
# 表示将自己注册进EurekaServer默认为true
register-with-eureka: true
# 是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合Ribbon使用负载均衡
fetch-registry: true
service-url:
# 单机版
defaultZone: http://eureka7001.com:7001/eureka
# 集群版
# defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
Eureka集群
原理:多个Eureka相互注册,端口7001的Eureka注册到端口7002的Eureka里,端口7002的Eureka注册到端口7001的Eureka里如下图:
集群的配置yml文件
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com # eureka 服务端的实例名称
client:
# false 表示不向注册中心注册自己
register-with-eureka: false
# false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://eureka7001.com:7001/eureka
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #单机
# defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 多个服务器
服务发现 Discovery
对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息
在 Controller 类里
/**
* DiscoveryClient
* 服务发现,客户端(Client)
*/
@Resource
private DiscoveryClient discoveryClient;
@GetMapping("/payment/discovery")
public Object discovery() {
List<String> services = discoveryClient.getServices();
services.forEach(element -> {
log.info("****element:"+element);
});
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
instances.forEach(instance -> {
log.info(instance.getInstanceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
});
return this.discoveryClient;
}
Eureka 的自我保护
为什么产生自我保护机制
什么是自我保护机制
禁止自我保护
注册中心,在 Eureka Server 端的 yml 文件修改
生产者客户端 Eureka Client 端在 yml 修改
还没有评论,来说两句吧...