-
Eureka Server:提供服务的注册于发现。zookeeper
-
Service Provider:将自身服务注册到Eureka中,从而使消费方能够找到。
-
Service Consumer:服务消费方从Eureka中获取注册服务列表,从而找到消费服务。
第一步,new 一个module springcloud-ureka-7001
导入依赖
org.springframework.cloud
spring-cloud-starter-eureka-server
1.4.6.RELEASE
org.springframework.boot
spring-boot-devtools
第二步、application.yml
server:
port: 7001
#Eureka配置
eureka:
instance:
hostname: localhost #Eureka服务器的实例名称
client:
register-with-eureka: false #表示是否向eureka注册中心注册自己
fetch-registry: false # 如果为false,则表示自己为注册中心
service-url: #监控页面~
defaultZone: http:// e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}: eureka.instance.hostname:{server.port}/eureka/
第三步、主启动类
@SpringBootApplication
@EnableEurekaServer //@EnableEurekaServer 服务端的启动类,可以接受别人注册进来~
public class EurekaServer_7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer_7001.class,args);
}
}
测试 访问 localhost:7001
Eureka : 服务注册-信息配置-自我保护机制
是对springcloud-provider-dept-8001的添加
第一步、加依赖
org.springframework.cloud
spring-cloud-starter-eureka
1.4.6.RELEASE
第二步、application.yml 添加eureka配置
eureka的配置 服务注册到哪里
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
instance-id: springcloud-provider-dept8001 #修改eureka上的默认描述信息!
第三步、主启动类加注解
//启动类
@SpringBootApplication
@EnableEurekaClient //在服务启动后自动注册到eureka中
public class DeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8001.class,args);
}
}
测试 访问 开启 8001 7001
点击UP【1】的链接 404 完善配置信息 8001的
1.加依赖
org.springframework.boot
spring-boot-starter-actuator
2.application.yml加配置
info 配置
info:
app.name: kuangshen-springcloud
company.name: blog.kuangstudy.com
自我保护机制
-
好死不如赖活着
-
综上,自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留),也不盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮和稳定
-
一句话总结:某时刻某一个微服务不可以用了,eureka不会立刻清理,依旧会对该微服务的信息进行保存!
-
在SpringCloud中,可以使用
eureka.server.enable-self-preservation m false
禁用自我保护模式【不推荐关闭自我保护机制】
扩展 用代码获取信息
- 8001 controller
//获取一些配置的信息,得到具体的微服务
@Autowired
private DiscoveryClient client;
//注册进来的微服务~ 获取一些消息~
@GetMapping(“/dept/discovery”)
public Object discovery(){
//获取微服务的清单
List services = client.getServices();
System.out.println("discovery==> "+services);
//得到一个具体的微服务信息 通过具体的微服务id,ApplicationName
List instances = client.getInstances(“SPRINGCLOUD-PROVIDER-DEPT”);
for (ServiceInstance instance : instances) {
System.out.println(
instance.getHost()+“\t”+
instance.getPort()+“\t”+
instance.getUri()+“\t”+
instance.getServiceId()
);
}
/*discovery==> [springcloud-provider-dept]
DESKTOP-2O58D28 8001 http://DESKTOP-2O58D28:8001 SPRINGCLOUD-PROVIDER-DEPT */
return this.client;
}
主启动类加上 @EnableDiscoveryClient
注解
测试 访问 http://localhost:8001/dept/discovery
创建两个和springcloud-eureka-7001一样的项目 为 7002 7003
- 7001 application.xml
server:
port: 7001
#Eureka配
置
eureka:
instance:
hostname: eureka7001.com #Eureka服务器的实例名称
client:
register-with-eureka: false #表示是否向eureka注册中心注册自己
fetch-registry: false # 如果为false,则表示自己为注册中心
service-url: #监控页面~
单机 defaultZone: http:// e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}: eureka.instance.hostname:{server.port}/eureka/
集群 (关联):
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
- 7002 application.xml
server:
port: 7002
#Eureka配置
eureka:
instance:
hostname: eureka7002.com #Eureka服务器的实例名称
client:
register-with-eureka: false #表示是否向eureka注册中心注册自己
fetch-registry: false # 如果为false,则表示自己为注册中心
e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}: eureka.instance.hostname:{server.port}/eureka/
集群 (关联):
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
- 7002 application.xml
server:
port: 7002
#Eureka配置
eureka:
instance:
hostname: eureka7002.com #Eureka服务器的实例名称
client:
register-with-eureka: false #表示是否向eureka注册中心注册自己
fetch-registry: false # 如果为false,则表示自己为注册中心