Eureka服务发现
cookqq ›博客列表 ›spring cloud

Eureka服务发现

2018-09-03 15:51:53.0|分类: spring cloud|浏览量: 3380

摘要: Eureka包含了服务器端和客户端组件。服务器端,也被称作是服务注册中心,用于提供服务的注册与发现。Eureka支持高可用的配置,当集群中有分片出现故障时,Eureka就会转入自动保护模式,它允许分片故障期间继续提供服务的发现和注册,当故障分片恢复正常时,集群中其他分片会把他们的状态再次同步回来。


服务发现组件单独拿出来

blob.png


服务发现组件有consul、zookeeper、eureka等,下面列出优缺点


特性

Consul

zooKeeper

etcd

eureka

编写语言

go

Java

go

java

客户端支持

http,

dns

跨语言弱,Curator组件

http,

Etcd3支持grpc

http,

非java(sidecar)

多数据中心

支持




KV存储

支持

支持

支持


健康检查

服务状态、内存、硬盘等

(弱)长连接,keepalive

连接心跳

可配支持

watch支持

全量/支持长轮询

支持

支持长轮询

支持长轮询, Eureka 2.0(正在开发中)也计划支持

自身监控

metrics


metrics

metrics

安全

acl /https

acl

https支持(弱)


一致性算法

raft

paxos

raft


CAP理论

CA

CP(牺牲可用性)

CP(牺牲可用性)

AP(一致性弱)

spring cloud

已支持

已支持

已支持

已支持

eureka官方架构图

blob.png

application service 服务提供者

application client 服务消费者


eureka组件有:eureka server和eureka client 


eureka server项目结构如下图:

blob.png

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>

  <groupId>com.cookqq</groupId>
  <artifactId>euraka</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
  <!-- 引入spring boot的依赖 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
  </parent>
  

  <name>euraka</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
   <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  
   <!-- 引入spring cloud的依赖 -->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Edgware.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>


application.yml

server:
  port: 8761                    # 指定该Eureka实例的端口
eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

启动类

package com.cookqq.euraka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * 
 *eureka注册中心
 */
@SpringBootApplication
@EnableEurekaServer
public class EurakaApp 
{
    public static void main( String[] args )
    {
        SpringApplication.run(EurakaApp.class, args);
    }
}

启动类,访问浏览器http://localhost:8761/

blob.pngblob.png



eureka client集成过程

blob.pngblob.png

provider-user/consumer-order配置eureka client


pom.xml增加依赖

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

application.yml增加

spring:
  application:
    name: provider-user
...    
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true



改变订单控制层代码OrderController.java

package com.cookqq.consumer.controller;

import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.cookqq.consumer.bean.User;

@RestController
public class OrderController {

	@Autowired
	private RestTemplate restTemplate;
	
	@Autowired
	private DiscoveryClient discoveryClient;
	
	@GetMapping("/user/{id}")
	public User findUserById(@PathVariable Long id){
//		return this.restTemplate.getForObject("http://127.0.0.1:8000/"+id, User.class);
		List<ServiceInstance> listInfo = discoveryClient.getInstances("provider-user");
		if(listInfo.size()>0){
			return this.restTemplate.getForObject(
					listInfo.get(new Random().nextInt(listInfo.size())).getUri()+"/"+id, 
					User.class);
		}
		return null;
	}
}

启动所有项目,访问浏览器

blob.png


一键分享文章

分类列表

  • • struts源码分析
  • • flink
  • • struts
  • • redis
  • • kafka
  • • ubuntu
  • • zookeeper
  • • hadoop
  • • activiti
  • • linux
  • • 成长
  • • NIO
  • • 关键词提取
  • • mysql
  • • android studio
  • • zabbix
  • • 云计算
  • • mahout
  • • jmeter
  • • hive
  • • ActiveMQ
  • • lucene
  • • MongoDB
  • • netty
  • • flume
  • • 我遇到的问题
  • • GRUB
  • • nginx
  • • 大家好的文章
  • • android
  • • tomcat
  • • Python
  • • luke
  • • android源码编译
  • • 安全
  • • MPAndroidChart
  • • swing
  • • POI
  • • powerdesigner
  • • jquery
  • • html
  • • java
  • • eclipse
  • • shell
  • • jvm
  • • highcharts
  • • 设计模式
  • • 列式数据库
  • • spring cloud
  • • docker+node.js+zookeeper构建微服务
版权所有 cookqq 感谢访问 支持开源 京ICP备15030920号
CopyRight 2015-2018 cookqq.com All Right Reserved.