Eureka Server Microservice Registration Center Construction

1. Preliminary construction of Eureka Server registration center

1. Add dependencies in pom.xml file

 

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

 

 2. Write a startup class and add the @EnableEurekaServer annotation to the startup class

 

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication
{
    public static void main( String[] args )
    {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

 

 

3. Create an application.yml configuration file in the resources directory and add configuration information

 

server:

  port: 8761

 

eureka:

  client:

    register-with-eureka: false

    fetch-registry: false

    service-url:

      default-zone: http://localhost:8761/eureka

 

eureka.client.registerWithEureka: Indicates whether to register itself with Eureka Server, the default is true. The current application is Eureka Server, so it is set to false.

eureka.client.fetch-registry: Indicates whether to obtain registration information from Eureka Server, the default is true. Because this is a single-point Eureka Server, it does not need to synchronize the data of other Eureka Server nodes, so it is set to false.

eureka.client.serviceUrl.defaultZone: Set the address for interacting with Eureka Server. Both query services and registration services need to rely on this address. Multiple addresses can be separated by , (comma)

 

2. Create a service provider and register

1. Create the maven project spring-cloud-eureka-client and add dependencies to the pom.xml file

 

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

 

 2. Write a startup class and add @EnableEurekaClient or @EnableDiscoveryClient annotations to the startup class

 

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication
{
    public static void main( String[] args )
    {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}

 

 

 

3. Create an application.yml configuration file in the resources directory and add configuration information

server:

  port: 8080

  

spring:

  application:

    name: spring-cloud-eureka-client

    

eureka:

  client: 

    service-url:

      default-zone: http://localhost:8761/eureka

  instance: 

    prefer-ip-address: true

 

spring.application.name: used to specify the name of the application registered on the Eureka Server.

eureka.instance.prefer-ip-address=true: Indicates to register your own IP with Eureka Server. If you do not configure this property or set it to false, it means that the hostname of the operating system where the microservice is registered is to Eureka Server.

 

4. Visit http://localhost:8761/eureka

 

3. Add user authentication to Eureka Server

 

1. Add dependencies to the pom file

  	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-security</artifactId>
	</dependency>

 

 2. Add configuration in application.yml

security:

  basic:

    enabled: true # Enable HTTP basic based authentication

  user:

    name: tuozixuan # Configure the login account to be user

    password: 123456 # Configure the login password to be password

 

Note: If the user's account and password are not set, the account defaults to user, and the password is a random value, which will be printed out at startup.

 

3. The microservice is registered to the Eureka Server that requires authentication

Just configure eureka.client.service-url.default-zone in the form of http://user:password@EUREKA_HOST:EUREKA_PORT/eureka/

如:http://tuozixuan:123456@localhost:8761/eureka

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326445679&siteId=291194637
Recommended