springcloud实战篇一之Eureka-server创建

以下是springcloud的官方介绍翻译

Spring Cloud为开发人员提供了快速构建分布式系统中的一些通用模式(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁,领导选举,分布式 会话,群集状态)。 分布式系统的协调导致了锅炉板模式,并且使用Spring Cloud开发人员可以快速地站起来实现这些模式的服务和应用程序。 它们可以在任何分布式环境中正常工作,包括开发人员自己的笔记本电脑,裸机数据中心和受管平台,如Cloud Foundry

简单介绍一下Eureka

这是从springcloud的中文网截取的,可以看出,Eureka只是springcloud集成的一个子项目,由Netflix提供.这是开放的源码https://github.com/Netflix/eureka


Eureka是传统的CS架构,在springcloud里面的提供的是服务注册的作用,是其默认的注册中心,如下图所示



1、注册中心:


Eureka和zookeeper都是可以被用作注册中心,spring cloud默认使用eureka server,而dubbo默认使用的是zookeeper。eureka的注册信息是保存在一个双层的Map对象中的,换句话说在内存中,不象zookeeper是长久保存在节点中。另外Eureka遵循的是AP原则,也就是可用性和分区容错性,而zookeeper保证的是CP,确保强一致性


2、服务提供方:


spring-web(Spring MVC)提供了完善的http rest服务框架,用这一套就能提供rest服务。(目前spring cloud官方提供的示例基本上都是http rest服务,理论上讲,应该也可以扩展成rpc服务,而dubbo是以rpc为主的,这点有些区别)


3、服务消费方:


依赖于spring-web,负载均衡采用ribbon组件来完成,大致原理是从注册中心发现可用服务的信息,缓存在本地,然后按一定的负载均衡算法进行调用。(跟dubbo类似,只不过dubbo是自己实现的负载均衡)


下面给大家演示如何快速搭建一个Eureka-Server端

工程目录结构


1.EurekaServer1001服务启动类

package com.yxf.springcloud;

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

import com.yxf.springcloud.EurekaServer1001;

/**
 * EurekaServer服务器端启动类,接受其它微服务注册进来
 * 
 * @author yangxf  
 */
@SpringBootApplication 
@EnableEurekaServer    //表明这个eureka的服务端
public class EurekaServer1001
{
	public static void main(String[] args)
	{
		SpringApplication.run(EurekaServer1001.class, args);
	}
}

2.application.yml文件

server: 
  port: 1001
 
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/   

3.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.yxf.springcloud</groupId>
    <artifactId>mymicroservicecloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>mymicroservicecloud-config-eureka-client-1001</artifactId>
  
  	<dependencies>
		<!-- SpringCloudConfig配置 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<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>

最后访问结果 localhost:1001


猜你喜欢

转载自blog.csdn.net/weixin_39666581/article/details/81048028