Spring Cloud学习(一):Eureka服务注册与发现

1.Eureka是什么

Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。

Eureka Server提供服务注册服务,各个节点启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到

有了服务发现与注册,只需要使用服务的标识符,就可以访问到服务,而不需要修改服务调用的配置文件了

Eureka包含两个组件:Eureka Server和Eureka Client。

2. Eureka的代码实现

Eureka服务注册和发现主要有三端构成:Eureka 服务端、Eureka 客户端 和服务消费端

2.1. Eureka 服务端

新建一个项目,这里直接使用的是idea新建 新建后的pom文件
Spring Cloud学习(一):Eureka服务注册与发现

<?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.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.pikaqiu.springcloud</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringCloud-Eureka Server</name>
<description>Spring Cloud学习(一):Eureka服务注册与发现</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-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>
</project>

application.yml文件

server:
port: 3001
eureka:
instance:
# 在服务中心显示的名称
hostname: eureka-server1
client:
# false表示不向注册中心注册自己
register-with-eureka: false
# false声明是服务端
fetch-registry: false
service-url:
# 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

如果使用hostname需要在host文件中添加127.0.0.1和hostname的映射

host文件地址在
Linux : /etc/hosts 
win: C:\Windows\System32\drivers\etc 
在该目录下的host文件中添加一行 
127.0.0.1 eureka-server1
同样,之后如果有服务也需要这样可以一起加上

在项目的启动类加上注解@EnableEurekaServer表示该服务是 Eureka Server,接受其他微服务注册进来

package com.pikaqiu.springcloud.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaServerApplication.class, args);
} }

启动项目,在浏览器输入localhost:3001,如果出现以下页面标识eureka server部署成功 
Spring Cloud学习(一):Eureka服务注册与发现

2.2 Eureka 客户端

创建方法和上面相同,只是在选择依赖的时候选择eureka client

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

注:同时也要选择web这个依赖,不然会启动失败

Completed shut down of DiscoveryClient

application.yml

server:
port: 3002
spring:
application:
# 可根据自己的业务命名
name: eureka-client1
eureka:
client:
service-url:
defaultZone: http://eureka-server1:3001/eureka/

在项目的启动类加上注解@EnableEurekaClient表示该服务是 Eureka Client,启动后会自动注册到指定的 Eureka Server

package com.pikaqiu.springcloud.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
@EnableEurekaClient
public class SpringCloudEurekaClientApplication { public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaClientApplication.class, args);
} }

刷新一下eureka server的可视界面就可以看到服务已经注册进来了

Spring Cloud学习(一):Eureka服务注册与发现

欢迎留言:http://pikaqiu.vip/article/2356.html
示例代码:https://github.com/Liangshan1994/SpringCloud

上一篇:Java—从文件中读取数据


下一篇:swoole Tcp服务器