Spring Cloud学习一:服务治理Spring Cloud Eureka搭建高可用注册中心

Spring Cloud Eureka是Spring Cloud NetFlix微服务套件中的一部分,基于NetFlix Eureka做了二次封装,主要负责完成微服务架构中的服务治理功能,是微服务架构中最为核心和基础的模块,既包含了服务端组件,也包含了客户端组件。

又涉及到服务注册与服务发现两个概念:服务注册指构建注册中心,每个服务单元向注册中心登记自己提供的服务,将主机与端口号、版本号、通信协议等一些附加信息告知注册中心,注册中心按照服务名组织服务清单;服务发现指服务间调用通过向服务名发起调用请求实现,调用方需要向服务注册中心咨询服务, 并获取所有服务的实例清单, 以实现对具体服务实例的访问。当然这些过于书面化,上代码吧,代码里放注释。

1.通过https://start.spring.io/创建注册中心eureka-server,添加eureka-server依赖

 使用idea创建也可以,但spring boot和eureka之间可能会有一些版本问题,这个目前还不是太清楚

2.使用idea打开刚刚创建的eureka-server项目,pom.xml内容如下

<?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com</groupId>
	<artifactId>eureka-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>eureka-server</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
	</properties>

	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
			<version>2.0.2.RELEASE</version>
		</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>-->
				<version>Brixton.SRS</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>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</repository>
	</repositories>

</project>

在启动类EurekaServerApplication.class添加@EnableEurekaServer注解,用以启动一个服务注册中心提供给其他应用进行对话。

package com.eurekaserver;

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

@EnableEurekaServer//启动一个服务注册中心提供给其他应用进行对话
@SpringBootApplication
public class EurekaServerApplication {

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

}

个人习惯,这类配置文件使用yml,创建application.yml、application-peer1.yml、application-peer2.yml

application.yml内容如下

spring:
  profiles:
    active: peer1

application-peer1.yml内容如下

server:
  servlet:
    context-path: /
  port: 1111
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: peer1
  client:
    register-with-eureka: true #由于该应用为注册中心,设置为false, 代表不向注册中心注册自己由于该应用为注册中心,要实现高可用的注册中心,我这里设置为true。
    fetch-registry: false  #由于注册中心的职责就是维护服务实例,它并不需要去检索服务, 所以也设置为false。
    service-url:
      defaultZone: http://peer2:1112/eureka/ #指定注册地址

application-peer2.yml内容如下

server:
  servlet:
    context-path: /
  port: 1112
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: peer2
  client:
    register-with-eureka: true #由于该应用为注册中心,设置为false, 代表不向注册中心注册自己,要实现高可用的注册中心,我这里设置为true。
    fetch-registry: false #由于注册中心的职责就是维护服务实例,它并不需要去检索服务, 所以也设置为false。
    service-url:
      defaultZone: http://peer1:1111/eureka/ #指定注册地址

貌似这里的eureka:instance:hostname不能相同,所以我使用了peer1、peer2来代替localhost,相应的,需要在C:\Windows\System32\drivers\etc\目录下修改hosts文件,添加下面两行,进行域名映射

127.0.0.1 peer1 

127.0.0.1 peer2

这里的eureka.client.register-with-eureka和eureka.client.fetch-registry需要提一下,见注释。

通过修改application.yml中的spring:profiles:active: peer1来启动两个注册中心,配置不同的启动项

启动成功之后,访问http://localhost:1111或者http://peer1:1111就能看到以下内容,注册中心已经有了两个服务,一个端口为1112,另一个为自身

同样的,打开http://peer2:1112或者http://localhost:1112也是一样的

至此注册中心创建完成

猜你喜欢

转载自blog.csdn.net/dandandeteng/article/details/85335918