Eureka是SpringCloud第一代的注册中心框架,也是最早一批的微服务架构选配,由于Eureka已经停止更新了,诸如Consul、Etcd、Nacos可以替代Eureka,目前推荐使用Nacos。
服务注册中心Eureka注册中心是需要编写Java客户端的,代码如下:
pom.xml
org.springframework.boot spring-boot-starter-parent 2.4.2 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test org.projectlombok lombok org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.cloud spring-cloud-dependencies Greenwich.SR3 pom import
启动类 app.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class App {
public static void main(String[] args) {
SpringApplication.run( App.class, args );
}
}
application.yml
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eurka-register
启动项目成功,打开链接:http://localhost:8761/
服务注册者pom.xml
org.springframework.boot spring-boot-starter-parent 2.4.2 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test org.projectlombok lombok org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-dependencies Greenwich.SR3 pom import
创建启动类app.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class App {
public static void main(String[] args) {
SpringApplication.run( App.class, args );
}
}
application.yml
server:
port: 8080
spring:
application:
name: eureka-client
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
启动成功,打开注册中心:http://localhost:8761/,可以看到服务已经注册进去了



