Eureka入门教程
一如码农深似海,今朝有酒今朝醉。入坑已经五年多了,人总有所坚持,立个flag,想把spring、springboot、springcloud从入门教程到源码分析,希望能对于读者有用,ekko写文章喜欢详尽,源代码有点多,不喜勿喷。
环境搭建
- IDEA(个人推荐,功能强大不多说)、其次Spring Tools 4,工作这么多年,很大牛用这工具,也是不错的选择;其次eclipse。
- jdk1.8环境。(google或者百度)
- maven(gradle少部分公司用,暂不介绍,当然,gradle更强大)
版本
参考官方https://spring.io/projects/spring-cloud
spring cloud版本 | springboot版本 |
---|---|
Hoxton | 2.2.x |
Greenwich | 2.1.x |
Finchley | 2.0.x |
Edgware | 1.5.x |
Dalston | 1.5.x |
我选择spring cloud Hoxton.SR4、springboot版本2.2.5.RELEASE。(如果是公司项目建议用老版本,原因无它,坑少网上解决办法多)。
父级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>com.duoduo</groupId>
<artifactId>duoduo-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>duoduo-parent</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimit>$</delimit>
</delimiters>
</configuration>
</plugin>
</plugins>
</build>
<modules>
<module>duoduo-eureka</module>
<module>duoduo-provider-service</module>
</modules>
</project>
duoduo-eureka子项目POM文件
<?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">
<parent>
<artifactId>duoduo-parent</artifactId>
<groupId>com.duoduo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>duoduo-eureka</artifactId>
<dependencies>
<!--eureka-server服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
eureka启动类
package com.duoduo.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author ekko
* eureka启动类
*/
@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class,args);
// 官方文档,编译报错,编译报错,可以使用下面代码,个人还是喜欢上面那行代码,启动
//new SpringApplicationBuilder(ServerApplication.class)
.web(true).run(args);
//new SpringApplicationBuilder(ServerApplication.class)
.web(WebApplicationType.SERVLET).run(args);
}
}
application.yml文件
server:
port: 8761 #端口
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
registerWithEureka: false #false表示不向注册中心注册自己。
fetchRegistry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
ServerApplication方法启动
2020-05-23 14:49:39.025 INFO 8031 --- [ Thread-17] c.n.e.r.PeerAwareInstanceRegistryImpl : Renew threshold is: 1
2020-05-23 14:49:39.025 INFO 8031 --- [ Thread-17] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP
2020-05-23 14:49:39.042 INFO 8031 --- [ Thread-17] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2020-05-23 14:49:39.112 INFO 8031 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8761 (http) with context path ''
2020-05-23 14:49:39.114 INFO 8031 --- [ restartedMain] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8761
控制台显示以上信息说明,eureka服务端启动成功。可以访问http://localhost:8761/。
duoduo-provider-service POM文件
<?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">
<parent>
<artifactId>duoduo-parent</artifactId>
<groupId>com.duoduo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>duoduo-provider-service</artifactId>
<dependencies>
<!--eureka-server服务端 -->
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
启动类
package com.duoduo.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author ekko 不需要添加@EnableEurekaClient,后续有说明
* eureka启动类
*/
@SpringBootApplication
@RestController
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class,args);
}
@RequestMapping("/")
public String home() {
return "Hello world";
}
}
application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: DUODUO-PROVIDER-SERVICE # 项目服务名,如果不写服务名,默认服务名UNKNOWN
启动ServiceApplication方法。在访问http://localhost:8761/,会发现eureka控制台Instances currently registered with Eureka多了一个DUODUO-PROVIDER-SERVICE的服务。
可能遇到问题
-
eureka自我保护机制(不用看到控制台保红就认为eureka出问题了)
不知道广大读者有没有了解过CAP模型,eureka是典型AP模型,这里提一句题外话,zookeeper是典型的CP模型,在eureka server运行期间,会统计客户端心跳的比例,15分钟是否超过85%,超过则把会把实例注册信息保护起来。再说一嘴zookeeper,在zk集群中,网络出现问题,zk重新选举leader,需要耗费不少的时间,这个期间,不可以zk注册服务,而eureka不同,可以继续注册。原因说这么多,面试官经常会问zk、eureka的区别。这就是这道题的痛点吧。