Spring Cloud microservice Eureka registration center construction and common problems

During the use of Spring cloud microservices, service registration and discovery are performed through Eureka.

One, EurekaServer construction

1. Import the required dependencies of EurekaServer

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

2. Add the annotation @EnableEurekaServer to the startup class , and add the configuration application.yml configuration

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false #false表示不向注册中心注册自己。
    fetchRegistry: false #表示自己就是注册中心,我的职责是维护服务实例,不需要去检索服务
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

The EurekaServer server is configured, and then you can visit the Eureka address to view

Two, EurekaClient service registration

Even if the above server is built, now we need to configure it in our callee and introduce the required dependency packages

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

Add the following configuration in application.yml

#指定注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

#服务的名称
spring:
  application:
    name: product-service

 

Guess you like

Origin blog.csdn.net/Damao1183297959/article/details/108894931