pom
import
org.springframework.boot
spring-boot-maven-plugin
2、然后创建两个moudle工程,一个moudle工程作为服务注册中心,即Eureka Server,另一个作为Eureka Client。
Eureka Server
2.1、右键工程->创建module-> 选择spring initialir 如下图:

2.2、填好自己的项目名

2.3、选择cloud discovery->eureka server ,然后一直下一步就行了。

2.4、创建完后的工程,其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 https://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
com.riemann
microservice-eureka
0.0.1-SNAPSHOT
com.riemann
microservice-eureka-server
0.0.1-SNAPSHOT
microservice-eureka-server
Eureka Server
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
2.5、启动一个服务注册中心,只需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动application类上加:
package com.riemann.microserviceeurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class MicroserviceEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceEurekaServerApplication.class, args);
}
}
2.6、eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件appication.yml:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
default-zone: http://
e
u
r
e
k
a
.
i
n
s
t
a
n
c
e
.
h
o
s
t
n
a
m
e
:
{eureka.instance.hostname}:
eureka.instance.hostname:{server.port}/eureka/
spring:
application:
name: microservice-eureka-server
通过
eureka.client.registerWithEureka:false
和
fetchRegistry:false
来表明自己是一个
eureka server
.
2.7、eureka server 是有界面的,启动工程,打开浏览器访问:http://localhost:8761 ,界面如下:

No application available 没有服务被发现 ……
因为没有注册服务当然不可能有服务被发现了。
四、创建一个服务提供者 (Eureka Client)
当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。
Eureka Client
其他跟 Eureka Server 类似,只有这个不同:

创建过程同server类似,创建完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 https://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
com.riemann
microservice-eureka
0.0.1-SNAPSHOT
com.riemann
microservice-provider-service-hi
0.0.1-SNAPSHOT
microservice-provider-service-hi
Eureka Server
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-web
通过注解@EnableEurekaClient 表明自己是一个eureka client.
package com.riemann.microserviceproviderservicehi;
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
@RestController
public class MicroserviceProviderServiceHiApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceProviderServiceHiApplication.class, args);
}
本次面试答案,以及收集到的大厂必问面试题分享:

stController;
@SpringBootApplication
@EnableEurekaClient
@RestController
public class MicroserviceProviderServiceHiApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceProviderServiceHiApplication.class, args);
}
本次面试答案,以及收集到的大厂必问面试题分享:
[外链图片转存中…(img-zd3kA1jp-1714454560642)]
本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录
版权归原作者 2401_84023446 所有, 如有侵权,请联系我们删除。