天天看点

SpringCloud Eureka服务治理模块

说明

SpringCloud Eureka是SpringCloud Netflix微服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的服务治理功能,Eureka是用于微服务注册和发现的工具。

服务治理:服务治理可以说是微服务架构中最为核心和基础的模块,它主要用于实现各个微服务实例的自动化注册与发现。主要通过服务注册与服务发现机制来完成对微服务应用实例的自动化管理

服务注册:在服务治理框架中,通常会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,,将主机与端口号、版本号、通信协议等一些附加信息高速注册中心,注册中心按服务名分类组织服务清单。

服务发现:在服务治理框架的运作下,服务间的调用不再通过指定具体的实例地址来实现,而是通过向服务名发起请求调用实现。所以,服务调用方在调用服务提供方接口时,并不知道具体的服务实例位置。因此,调用方需要向注册中心咨询服务,并获取所有服务的实例清单,以实现对具体服务实例的访问。

目标

使用SpringCloud Eureka搭建两个互相注册的注册中心来形成一个简单的高可用性Eureka集群。

快速使用

首先给出项目结构

SpringCloud Eureka服务治理模块

1 更新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>

	<groupId>yunlingfly</groupId>
	<artifactId>mavenspringcloud</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>mavenspringcloud</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.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>
	</properties>

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<!--<scope>provided</scope>-->
		</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>Dalston.SR2</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>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
</project>
           

2 编写配置文件application.yml

server:
  port: 8761
spring:
  application:
    name: peer1
eureka:
  instance:
    #填写服务器IP
    hostname: xxx.xxx.xxx.xxx
    #使得可以用IP配置地址
    prefer-ip-address: true
  client:
    # 注册自己
    registerWithEureka: true
    # 发现服务
    fetchRegistry: true
    service-url:
      #设置注册地址
      defaultZone: http://${eureka.instance.hostname}:8762/eureka/
info:
  name: peer1
  version: 0.0.1
           

3 编写启动类

package yunlingfly.mavenspringcloud;

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

@SpringBootApplication
@EnableEurekaServer
public class MavenspringcloudApplication {

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

4 重新新建一个项目,配置与上面一样,只需更改application.yml如下

server:
  port: 8762
spring:
  application:
    name: peer2
eureka:
  instance:
    hostname: xxx.xxx.xxx.xxx
    #使得可以用IP配置地址
    prefer-ip-address: true
  client:
    registerWithEureka: true
    fetchRegistry: true
    service-url:
      #设置注册地址
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/
info:
  name: peer2
  version: 0.0.1
           

5 分别启动两个服务->启动类右键->Run(无顺序要求),或者使用Maven install打包成jar,然后使用java -jar jar包名来运行。然后浏览器输入你前面填的IP地址:8761,例如http://localhost:8761,即可进入Eureka图形化界面

SpringCloud Eureka服务治理模块
SpringCloud Eureka服务治理模块

注意事项:

如果是在服务器上使用Eureka注意开启相应端口号否则外网无法访问,Eureka部署在服务器上(例如CentOS)需要保持运行状态,可以查看我的另一篇文章->使用tmux在CentOS7下开启SpringBoot项目

继续阅读