标签:服务发现 消费 架构 run enable 网络 操作 ice 进不去
遵循的是AP原则
Eureka是NetFlix的一个子模块,也是核心模块之一。Eureka是一个基于Rest的服务,用于定位服务,以实现云端中间层服务发现和故障转移,服务注册与发现对于微服务来说是非常重要的,有了服务发现与注册,只需要使用服务的标识符,就可以访问到服务,而不需要修改服务调用的配置文件了,功能类似于DUBBO的注册中心,比如Zookeeper
当我们新添加一个微服务实例的时候,微服务就会将自己的 ip 与 port 发送到注册中心,在注册中心里面记录起来。当 API gateway 需要访问某些微服务的时候,就会去注册中心取到相应的 ip 与 port。从而实现自动化操作。
1.编写Eureka的服务(注册中心)
? 1)导入依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
? 2)配置application.yml
server:
port: 7001
#Eureka配置
eureka:
instance:
hostname: localhost #Eureka服务端的实例名称
client:
register-with-eureka: false #表示是否向Eureka注册中心注册自己
fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
service-url: #监控页面
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
? 3)主启动类
package com.mjh.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* 启动之后,访问http://localhost:7001/
*/
@SpringBootApplication
@EnableEurekaServer //@EnableEurekaServer 服务端启动类,可以接受别人注册进来(即注册中心)
public class EurekaServer_7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer_7001.class,args);
}
}
? 4)启动
微服务架构下服务实例具有动态分配的网络地址,随着服务的自动扩展、故障和发布升级,导致服务实例的网络地址发生动态变更。因此,需要一种机制,支持服务消费者在服务提供者实例地址发生变更时,能够及时感知获取实例最新的地址,即服务发现机制。
2.想办法把上一次的服务提供者8001端口注册进来
? 1)导入依赖
<!--Eureka-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
? 2)在配置文件里把Eureka服务注册进去,有了Eureka服务就可以吧东西注册到注册中心里去了。
#Eureka服务
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
? 3)开启主启动类支持Eureka服务
//启动类
@SpringBootApplication
@EnableEurekaClient //服务启动后自动注册到Eureka中
public class DeptPrivider_8001 {
public static void main(String[] args) {
SpringApplication.run( DeptPrivider_8001.class,args);
}
}
? 4)分别启动7001端口和8001端口,之后刷新http://localhost:7001/
? 5)可以把status的地址干掉
#Eureka配置,服务注册到哪里
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
instance-id: springcloud-privider-dept8001 #修改Eureka的状态描述
当注册中心在开启的时候我们可能会把客户端停掉,这时候Eureka会自动开启自我保护机制
某时刻某一个微服务不可以用了,Eureka不会立刻清理,依旧会对该微服务的信息进行保存!
6)我们会发现,我们点status的地址时候会报出404页面,是因为我们没有导入依赖,所有进不去,我们需要的话也可以自己配置一下。
<!--actuator完善监控信息-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
#info配置
info:
app.name: mjh.springcloud
company.name: blog.mjh.com
我们也可以通过controller层获取微服务相关的信息,里面有一个服务发现,你需要告诉别人你写了什么东西(这些都是在企业中联合开发才用的到),然后还要在主启动类添加@EnableDiscoveryClient //服务发现 ,让其生效。
@GetMapping("/dept/discovery")
//注册进来的微服务,获取一些消息
public Object discovery(){
//获取微服务到表的清单
List<String> services = client.getServices();
System.out.println("discovet=>services:"+services);
//得到一个具体的微服务信息,通过具体的微服务id,applicationName
List<ServiceInstance> instances= client.getInstances("SPRINGCLOUD-PRIVIDER-DEPT");
for (ServiceInstance instance : instances) {
System.out.println(
instance.getHost()+"\t"+
instance.getPort()+"\t"+
instance.getUri()+"\t"+
instance.getServiceId()
);
}
return this.client;
}
标签:服务发现 消费 架构 run enable 网络 操作 ice 进不去
原文地址:https://www.cnblogs.com/mjjh/p/13340850.html