spring cloud Eureka服务中心搭建

demo下载连接:https://download.csdn.net/download/spark_guo/10495858

简介:

spring cloud Eureka是spring cloud Netflix微服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的服务治理功能。Spring Cloud通过Eureka增加了Spring Boot风格的自动化配置,只需通过简单引入依赖和注解配置就能让Spring Boot构建的微服务应用轻松地与Eureka服务治理体系进行整合。

核心内容:
1、构建服务注册中心;
2、服务注册与服务发现;
3、Eureka的基础架构
4、Eureka的服务治理机制
5、Eureka的配置。


搭建服务注册中心
首先,创建一个基础的Spring Boot工程,名称为eureka-server,并在pom.xml中引入必要的依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.14.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR3</spring-cloud.version>
</properties>


<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

EurekaServerApplication启动类:
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {


public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}


application.properties
server.port=3333
eureka.instance.hostname=localhost
#不要向注册中心注册自己
eureka.client.register-with-eureka=false
#禁止检索服务
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka


配置说明:
eureka.client.register-with-eureka:该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
eureka.client.fetch-registry:注册中心的职责是维护服务实例,它并不需要去搜索服务,所以也设置false;


启动后,打开浏览器:http://localhost:3333/


高可用注册中心:

创建application-peer1.properties,并将service url指向peer2

spring.application.name=eureka-server
server.port=3333
eureka.instance.hostname=peer1
#不要向注册中心注册自己
eureka.client.register-with-eureka=false
#禁止检索服务
eureka.client.fetch-registry=false

eureka.client.service-url.defaultZone=http://peer2:3334/eureka

创建application-peer2.properties,并将service url指向peer1

server.port=3334
eureka.instance.hostname=peer2
#不要向注册中心注册自己
eureka.client.register-with-eureka=false
#禁止检索服务
eureka.client.fetch-registry=false

eureka.client.service-url.defaultZone=http://peer1:3333/eureka

在C:\Windows\System32\drivers\etc设置主机名 IP映射:

127.0.0.1 peer1

127.0.0.1 peer2

通过spring.profiles.active属性来分别启动peer1和peer2

java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1

java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2


http://localhost:3333/

http://localhost:3334/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值