一、前言
1、什么是Eureka?
Eureka是Netflix开源的服务注册与发现框架,Eureka由两个组件组成:Eureka服务器和Eureka客户端。
Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。
Eureka客户端是一个Java客户端,用来简化与服务器的交互、作为轮询负载均衡器,并提供服务的故障切换支持。
2、Eureka概览
行为 | ken.io 的说明 |
---|---|
1. Registry | 服务启动时,Eureka客户端会将Service(ServiceName、IP、Port)注册到Eureka Server |
2. Query | 应用通过Eureka客户端调用注册在Eureka Server的服务时,会通过ServiceName查询到服务实例列表 |
3. Call | 根据拿到的服务实例信息,通过负载均衡或其他策略访问指定服务实例 |
3、本篇环境信息
框架 | 版本 |
---|---|
Spring Boot | 2.0.0.RELEASE |
Spring Cloud | Finchley.BUILD-SNAPSHOT |
JDK | 1.8.x |
二、搭建Eureka Server
1、创建Eureka Server项目
- 创建一个Spring Boot工程
使用maven-archtype-quickstart模板创建项目
项 | 说明 |
---|---|
GroupId | io.ken.springcloud.eurekaserver |
ArtifactId | eurekaserver |
- 在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>io.ken.springcloud.eurekaserver</groupId>
<artifactId>eurekaserver</artifactId>
<version>1.0-SNAPSHOT</version>
<name>eurekaserver</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.BUILD-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<finalName>eurekaserver</finalName>
</build>
</project>
2、配置EurekaServer启动类
修改\src\main\java\io\ken\springcloud\eurekaserver\App.java
基于Spring Boot创建启动类,添加上 @EnableEurekaServer
注解
package io.ken.springcloud.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
3、配置Eureka Server
在\src\main下创建文件夹resources文件夹并设置为Resources Root
在resources文件夹下创建application.yml文件并配置Eureka Server
server:
port: 8800
spring:
application:
name: EurekaServer
eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:${server.port}/eureka/
Eureka默认会以客户端来注册自己,所以registerWithEureka、fetchRegistry要配置为false。
4、Eureka Server信息预览
启动Eureka Server项目后,访问:http://localhost:8800/ ,可以看到下面的页面,其中还没有发现任何服务。
三、搭建服务并注册(Eureka Client)
1、创建Eureka Client项目
- 创建一个Spring Boot工程
使用maven-archtype-quickstart模板创建项目
项 | 说明 |
---|---|
GroupId | io.ken.springcloud.helloservice |
ArtifactId | helloservice |
- 在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>io.ken.springcloud.helloservice</groupId>
<artifactId>helloservice</artifactId>
<version>1.0-SNAPSHOT</version>
<name>helloservice</name>
<url>http://ken.io</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.BUILD-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<finalName>helloservice</finalName>
</build>
</project>
2、配置Eureka Client启动类
修改\src\main\java\io\ken\springcloud\helloservice\App.java
基于Spring Boot创建启动类,添加上 @EnableDiscoveryClient
注解
package io.ken.springcloud.helloservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
3、创建测试服务
在\src\main\java\io\ken\springcloud\helloservice创建package:controller
然后创建HelloController.java
package io.ken.springcloud.helloservice.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
DiscoveryClient discoveryClient;
@RequestMapping("/")
public Object index() {
return "hello service";
}
@RequestMapping("/info")
public Object info() {
return discoveryClient.getServices();
}
}
4、配置Eureka Client
在\src\main下创建文件夹resources文件夹并设置为Resources Root
在resources文件夹下创建application.yml文件并配置Eureka Client
server:
port: 8601
spring:
application:
name: helloservice
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8800/eureka/
5、服务注册&访问测试
HelloService项目成功启动后,访问 http://localhost:8601 ,或者 http://localhost:8601/info
查看是否正常启动
访问 http://localhost:8800 ,会发现服务已注册
搞定!
四、备注
- 本篇示例代码
https://github.com/ken-io/springcloud-course/tree/master/chapter-01