一、Eureka简介
包含两个组件:
Eureka Server :提供服务发现能力,各个微服务启动时,会向Eureka Server注册自己的信息(IP、端口、微服务名称等),Eureka Server会存储这些信息。
Eureka Client:是一个java客户端,可以简化与服务器的交互、作为轮询负载均衡器,能提供服务的故障切换支持。
二、创建服务注册中心【Eureka Server】
按步骤,直接上图:
至此,点击Next,直至 完成,在窗口打开项目,如下:
1.pom.xml
继承了父pom文件,并引入了spring-cloud-starter-netflix-eureka-server依赖
2.springboot工程的启动application类:DemoEurekaServerApplication
添加@EnableEurekaServer,启动一个服务注册中心,只需要这一个注解。
package com.example.demo_eureka_server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
/*启动一个服务注册中心,只需要一个注解@EnableEurekaServer*/
@EnableEurekaServer
public class DemoEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(DemoEurekaServerApplication.class, args);
}
}
3.application.yml
eureka是一个高可用的组件,没有后端缓存,每一个实例注册之后需要向注册中心发送心跳,因此可以在内存中完成,在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。
server:
port: 8763
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eurka-server
通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server。
4.至此,一个eureka server 搭建完成,启动工程后,打开浏览器访问:
http://localhost:8763/ ,界面如下:
No application available 没有服务被发现 ……原因:还没有注册服务。
三、创建一个服务的提供者【Eureka Client】
按步骤:直接上图
至此,点击Next,直至 完成,在窗口打开项目,如下图:
client向server注册时,它会提供一些元数据,如主机和端口,URL,主页等。
Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。
1.pom.xml
继承了父pom文件,并引入了spring-cloud-starter-netflix-eureka-server依赖。
<?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.example</groupId>
<artifactId>demo_eureka_client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo_eureka_client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.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-netflix-eureka-client</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>
2.springboot工程的启动application类:DemoEurekaClientApplication
添加注解@EnableEurekaClient ,表明自己是一个Eureka Client。
package com.example.demo_eureka_client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
/*通过注解@EnableEurekaClient 表明自己是一个eurekaclient*/
@EnableEurekaClient
@RestController
public class DemoEurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(DemoEurekaClientApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam(value = "name", defaultValue = "zyy") String name) {
return "hello " + name + " ,I am from port:" + port;
}
}
3.application.yml
除了添加@EnableEurekaClient,还需在配置文件中注明自己的服务注册中心的地址。
特别说明:spring.application.name,很重要,在以后,服务与服务之间 相互调用一般都是根据这个name 。
server:
port: 8762
spring:
application:
name: eureka-client-zyy-demo
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8763/eureka/
4.至此,一个Eureka Client搭建完成,启动工程后,打开浏览器访问Eureka Server 的网址:
http://localhost:8763/ ,界面如下:
会发现有一个服务已经注册在服务中了,服务名为eureka-client-zyy-demo ,端口为8762。
再打开 http://localhost:8762/hi?name=zyy ,会在浏览器上看到 :