1. Eureka 服务注册与发现
- Eureka 是一个基于REST的服务,用于服务的的注册与发现;
- Eureka采用C-S的设计架构,Eureka Server作为服务注册功能的服务器,它是服务注册中心;
- Eureka包含两个组件:Eureka Server 和 Eureka Client
- Eureka Server提供服务注册和发现;
- Eureka Client是一个java客户端;
1.1 三大角色
- Eureka Server提供服务注册和发现;
- Service Provider服务提供方将自身服务注册到Eureka,从而使服务消费方能够找到;
- Service Consumer服务消费方从Eureka获取注册服务列表,从而能够消费服务;
1.2 Eureka 服务注册中心Module(microservicecloud-eureka-7001)
// pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.noodles.springcloud</groupId>
<artifactId>microservicecloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservicecloud-eureka-7001</artifactId>
<dependencies>
<!-- eureka-server 服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
// application.yml
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
register-with-eureka: false # false 表示不向注册中心注册自己
fetch-registry: false # false 表示自己就是注册中心,职责是维护服务实例,并不需要检索服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
#设置与Eureka Server交互的地址,查询和注册服务都需要依赖这个地址
// com.noodles.springcloud
// EurekaServer7001_App
@SpringBootApplication
@EnableEurekaServer // EurekaServer 服务器端启动类,接收其他微服务注册进来
public class EurekaServer7001_App {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7001_App.class, args);
}
}
// 测试
// 访问: http://localhost:7001
1.3 将已有的部门微服务注册进eureka服务中心(microservicecloud-provider-dept-8001)
// 修改microservicecloud-provider-dept-8001
// pom.xml 增加
<!-- 将微服务provider 注册进 eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
// application.yml 增加
eureka:
client: # 客户端注册进eureka服务列表内
service-url:
defaultZone: http://localhost:7001/eureka
// DeptProvider8001_App 主启动类
@SpringBootApplication
@EnableEurekaClient // 本服务启动后,自动注册进eureka服务中
public class DeptProvider8001_App {
public static void main(String[] args) {
SpringApplication.run(DeptProvider8001_App.class, args);
}
}
// 测试
// 先要启动 EurekaServer,然后启动 DeptProvicer8001_App
// 访问:http://localhost:7001/
1.3.1 actuator 与注册微服务信息完善
- 主机映射名称修改
- 访问信息有IP信息提示
- 微服务info内容详细信息
修改之前
修改之后
// 主机映射名称修改
// 修改microservicecloud-provider-dept-8001
// application.xml
eureka:
client: # 客户端注册进eureka服务列表内
service-url:
defaultZone: http://localhost:7001/eureka
instance:
instance-id: microservicecloud-dept8001 # 主机映射名称
// 访问信息有IP信息提示
// 修改microservicecloud-provider-dept-8001
// application.xml
eureka:
client: # 客户端注册进eureka服务列表内
service-url:
defaultZone: http://localhost:7001/eureka
instance:
instance-id: microservicecloud-dept8001 # 主机映射名称
prefer-ip-address: true # 访问路径可以显示IP地址
// 微服务info内容详细信息
// 修改microservicecloud-provider-dept-8001
// pom.xml
<!-- actutator 监控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
// 修改父工程 microservicecloud
// pom.xml
<!-- dependencyManagement -->
<build>
<finalName>microservicecloud</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<delimiters>
<delimit>$</delimit>
</delimiters>
</configuration>
</plugin>
</plugins>
</build>
<!-- modules -->
// 修改microservicecloud-provider-dept-8001
// application.yml
info:
app.name: noodles-microservicecloud
company.name: www.google.com
build.artifactId: $project.artifactId$
build.version: $project.version$
1.3.2 Eureka 自我保护机制
// 修改 microservicecloud-eureka-7001
// 将自我保护机制关闭(不推荐)
// application.yml
eureka:
server:
enable-self-preservation: false
1.3.3 Eureka 服务发现
// 修改 microservicecloud-provider-dept-8001
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/dept/discovery", method=RequestMethod.GET)
public Object discovery() {
List<String> list = client.getServices();
System.out.println("==========="+list);
List<ServiceInstance> srvList = client.getInstances("MICROSERVICECLOUD-DEPT");
for(ServiceInstance element : srvList) {
System.out.println(element.getServiceId()+"\\t"+element.getHost()+"\\t"+element.getPort()+
"\\t"+element.getUri());
}
return this.client;
}
}
// DeptProvider8001_App 主启动类
@SpringBootApplication
@EnableEurekaClient // 本服务启动后,自动注册进eureka服务中
@EnableDiscoveryClient // 服务发现
public class DeptProvider8001_App {
public static void main(String[] args) {
SpringApplication.run(DeptProvider8001_App.class, args);
}
}
// 修改 microservicecloud-consumer-dept-80 工程的 DeptController Consumer
@RequestMapping(value="/consumer/dept/discovery")
public Object discovery() {
return restTemplate.getForObject(REST_URL_PREFIX+"/dept/discovery", Object.class);
}
2. Eureka 集群
// 新建microservicecloud-eureka-7002/microservicecloud-eureka-7003
// 按照7001为模板粘贴POM
// 修改7002和7003的主启动类
// 修改映射配置 hosts
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
// 7001 application.yml
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
// 7002 application.yml
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名称
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
// microservicecloud-provider-dept-8001
// 微服务发布到3台eureka集群配置中, application.yml
eureka:
client: # 客户端注册进eureka服务列表内
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
// 测试访问:
// http://eureka7001.com:7001
// http://eureka7002.com:7002
// http://eureka7003.com:7003
3. Eureka和Zookeeper比较
- Eureka遵守AP原则(可用性,分区容错性)
- Zookeeper遵守CP原则(强一致性,分区容错性)
参考资料: