Eureka 提供服务的注册,服务可以通过注册到
Eureka
然后被其他应用调用。
看到Spring Cloud 的文档里面是先讲的是Spring Cloud Config
,为了方便,或者说参考其他大佬的教程,我也会把Config放到后面写。
Netflix 是一家媒体提供商,应该很厉害!
官网文档中先列出的是Eureka客户端,我尝试先写Eureka Client
发现会提示以下错误,应该是在请求 Eureka server
的时候报错, 因为我们根本没有创建。所以我先看Eureka server
NFO 25402 --- [main] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
ERROR 25402 --- [main] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error
官方说明 Finchley的构建和工作环境应该是 Spring Boot 2.0.x
, 预计不会在Spring Boot 1.5.x
中使用。
Finchley builds and works with Spring Boot 2.0.x, and is not expected to work with Spring Boot 1.5.x.
Spring Cloud 需要使用Spring Boot,如果不会创建Spring Boot项目的看这里《Spring Boot | 使用Spring Initializr快速创建》!
<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.0modelVersion>
<groupId>com.cyinfotechgroupId>
<artifactId>sc-f-e-01artifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>sc-f-e-01name>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.5.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Finchley.SR1version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
Eureka Server
<modules>
<module>eureka-servermodule>
modules>
To include Eureka Server in your project, use the starter with a group ID of
org.springframework.cloud
and anartifact ID of spring-cloud-starter-netflix-eureka-server
. See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.
在项目中使用Eureka Server
,需要配置 pom.xml
, 根据你当前的Spring Cloud 版本在Spring Cloud Project page中可以查看详细说明。
eureka-server
的 pom.xml
<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.0modelVersion>
<groupId>com.cyinfotechgroupId>
<artifactId>eureka-serverartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>eureka-servername>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>com.cyinfotechgroupId>
<artifactId>sc-f-e-01artifactId>
<version>0.0.1-SNAPSHOTversion>
<relativePath/>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
dependencies>
project>
Eureka Server
在启动类添加 注解 @EnableEurekaServer
EurekaServerApplication
package com.cyinfotech.eurekaserver;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaServerApplication.class).web(true).run(args);
}
}
application.yml
server:
port: 8761 # 端口
eureka:
instance:
hostname: localhost # 主机
client:
registerWithEureka: false #是否注册自己
fetchRegistry: false #是否注册自己
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 服务地址
Instances currently registered with Eureka 是空的, 因为有没有服务注册!
Eureka客户端可以理解为服务注册实际项目中需要曝光的服务。
右键项目 > New > Module 新建 eureka-client
子工程,和添加 server 一样
修改父工程pom.xml
文件, 添加子工程。
<module>eureka-clientmodule>
To include the Eureka Client in your project, use the starter with a group ID of
org.springframework.cloud
and an artifact ID ofspring-cloud-starter-netflix-eureka-client
. See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.
要使用Eureka Client
就要添加配置文件,完整pom.xml
配置文件如下:
<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.0modelVersion>
<groupId>com.cyinfotechgroupId>
<artifactId>eureka-clientartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>eureka-clientname>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>com.cyinfotechgroupId>
<artifactId>sc-f-e-01artifactId>
<version>0.0.1-SNAPSHOTversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
dependencies>
project>
When a client registers with Eureka, it provides meta-data about itself — such as host, port, health indicator URL, home page, and other details. Eureka receives heartbeat messages from each instance belonging to a service. If the heartbeat fails over a configurable timetable, the instance is normally removed from the registry.
当注册Eureka时,它提供一些包括自己的源数据,例如:主机、端口、监听(心跳)地址、主页和其它详细信息。Eureka 通过每个实例的服务接受心跳消息。 如果心跳在配置的时间失败结束,那这个实例通常会删除注册。
修改启动Eureka Client
启动类
package com.cyinfotech.eurekaclient;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class EurekaClientApplication {
@RequestMapping("/")
public String home() {
return "Hello world";
}
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaClientApplication.class).web(true).run(args);
}
}
Note that the preceding example shows a normal Spring Boot application. By having
spring-cloud-starter-netflix-eureka-client
on the classpath, your application automatically registers with the Eureka Server. Configuration is required to locate the Eureka server, as shown in the following example:
注意前面的正常的 Spring boot
程序, 你的应用程序中添加spring-cloud-starter-netflix-eureka-client
后将自动注册到Eureka Server
, 如下所示:
server:
port: 8781
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
启动步骤:
1.EurekaServerApplication
2.EurekaClientApplication
以上 Eureka 的服务端和客户端(注册与发现)就讲完了,我也是第一次这么认真看官方文档,其实看下来发现也不难,其他人的文档里面也无非就直译了官方文档,但是每个人的理解不一样,所以还是推荐自己去看原版文档, 没有想象的那么难。
欢迎关注我的公众号,跟我留言。
博客地址:https://blog.aprcode.com/sc-f-e-01/
教程源码Github地址:sc-f-e-01
教程源码Gitee地址:sc-f-e-01