一、搭建基本eureka注册中心
新建springboot项目
- 新建maven项目,然后引入相应的jar包
- 新建springboot 项目,选择需要引入的jar包
需要引入的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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cloud</groupId>
<artifactId>eureka</artifactId>
<version>1.0-SNAPSHOT</version>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud-version>Finchley.RELEASE</spring-cloud-version>
<spring-coud-eureka>2.0.0.RELEASE</spring-coud-eureka>
</properties>
<dependencies>
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>${spring-coud-eureka}</version>
</dependency>
<!--boot junit test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--spring-cloud -->
<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>
设置apllication配置文件
spring:
application:
name: eureka-register-center #服务名称,自定义
server:
port: 9999 #指定该Eureka实例的端口
eureka:
instance:
hostname: 127.0.0.1
client:
register-with-eureka: false #禁止注册自身
fetch-registry: false #因为该服务没有注册到其他注册中心,所以关闭从注册中心拉取服务列表。
#如果是服务注册中心集群,u需要开启,开启就是去掉该配置,采用默认配置即可
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
renewalpercent-threshold: 0.49 #设置心跳正常的阀值,默认为1,测试时可以调低
编写启动类
package com.cloud.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @description: spring cloud eureka
* spring cloud的注册中心
* @EnableEurekaServer,该注解表明标注类是一个Eureka Server。
* @author: Andy
* @time: 2019/12/24 14:23
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
启动
此时启动项目,输入http://127.0.0.1:9999 即可看到相关页面
踩过的坑
Cannot execute request on any known server
https://blog.csdn.net/qq_40981804/article/details/104017380