Eureka
Spring Cloud Eureka 是 微服务套件中的一部分,它基于Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能
两大核心
Eureka - server
顾名思义,就是服务注册中心
pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
配置文件
server:
port: 8081
eureka:
instance:
hostname: localhost
client:
# 作为服务发现中心必要配置,区别于服务提供者
register-with-eureka: false
# 作为服务发现中心必要配置,区别于服务提供者
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启动类
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication
{
public static void main(String[] args)
{
SpringApplication.run(EurekaApplication.class, args);
}
}
Eureka - client
是服务提供方,注册到server中
pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
配置文件
spring:
application:
name: haha-auth
server:
port: 8083
eureka:
client:
service-url:
defaultZone: http://localhost:8081/eureka/
启动类
@EnableEurekaClient
@SpringBootApplication
public class AuthApplication
{
public static void main(String[] args)
{
SpringApplication.run(AuthApplication.class, args);
}
}
校验,先启动server项目,在启动client项目。访问server项目地址
本例地址: http://127.0.0.1:8081/
如图,即可成功使用eureka。图中红色部分,我单独讲解。优化是一步步的过程
附: 本例所有代码,均托管在码云,撸码不易,多多支持,谢谢大家。
项目地址: 戳这里