1.导包
<!-- eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.写配置文件
#服务端口号
server:
port: 7001
eureka:
instance:
hostname: localhost
client:
#表示不向注册中心注册自己
register-with-eureka: false
#表示自己就是注册中心
fetch-registry: false
#交互地址
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3.写启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class);
}
}
4.访问地址localhost:7001
5.微服务注册到eureka注册中心
- 1.导包
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 2.添加配置
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
#将自己注册到Eureka中
register-with-eureka: true
#是否从Eureka中获取到自己的信息
fetch-registry: true
- 3.启动类加@EnableEurekaClient注解
@SpringBootApplication
@EnableEurekaClient
public class PaymentApplication {
public static void main(String[] args) {
SpringApplication.run(PaymentApplication.class,args);
}
}
-
4.测试
image.png











网友评论