SpringBoot Actual E-commerce Project Mall (20K + STAR)

Abstract

Spring Cloud Eureka is one of the core components of the Spring Cloud Netflix subproject, which is primarily used for service governance in the microservice architecture. This article will set up Eureka registry, set up Eureka client, set up Eureka cluster and add login authentication to Eureka registry.

Eureka profile

In a microservice architecture, there is usually a registry to which each microservice registers its address and port information. The registry maintains the relationship between the service name and the service instance. Each microservice periodically obtains a list of services from the registry and reports how it is running, so that when a service needs to call another service, it can get the instance address from the list of services it has obtained. Eureka implements this service registration and discovery mechanism.

Set up the Eureka registry

Here we take a look at the correct posture for creating and running a SpringCloud application in IDEA by creating and running the Eureka registry.

Use IDEA to create SpringCloud applications

  • Create a Eureka-server module and initialize a SpringBoot project using Spring Initializer

  • Fill in application Information

  • Select the SpringCloud components you want to create

  • After the configuration is complete, the pom. XML file already has a dependency on eureka-server
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
Copy the code
  • Enable the Euerka registry functionality by adding the @enableEurekaserver annotation on the startup class
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

    public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); }}Copy the code
  • Add the Eureka registry configuration to the configuration file application.yml
server:
  port: 8001 # Specifies the running port
spring:
  application:
    name: eureka-server Specifies the name of the service
eureka:
  instance:
    hostname: localhost # Specify host address
  client:
    fetch-registry: false # Specify whether to obtain services from the registry (registry does not need to be enabled)
    register-with-eureka: false # Specify whether to register with the registry (registry does not need to be enabled)
  server:
    enable-self-preservation: false # Disable the protected mode
Copy the code

Use IDEA’s Run Dashboard to Run the SpringCloud application

Now that the service is created, click on the main method of the launch class to run it. But in the microservices project we start a lot of services, and for ease of administration, we start using IDEA’s Run Dashboard.

  • Open the Run Dashboard. By default, when IDEA checks that you have SpringBoot in your project, it will prompt you to enable it. If you don’t, you can do so using the following method.

  • Run the SpringCloud application

  • After running, visit http://localhost:8001/ to view the Eureka registry interface

Set up the Eureka client

  • Create a new eureka-client module and add the following dependencies to pom.xml
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Copy the code
  • Add the @enableDiscoveryClient annotation to the startup class to indicate that it is a Eureka client
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {

    public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); }}Copy the code
  • Add the configuration of the Eureka client to the configuration file application.yml
server:
  port: 8101 # Run port number
spring:
  application:
    name: eureka-client # Service name
eureka:
  client:
    register-with-eureka: true # Register with Eureka's registry
    fetch-registry: true # Get the list of registered instances
    service-url:
      defaultZone: http://localhost:8001/eureka/ Configure the registry address
Copy the code
  • Eureka – client operation

  • Check the registry http://localhost:8001/. The Eureka client has been registered successfully

Set up the Eureka registry cluster

Set up two registries

Since all services are registered to the registry, calls between services are made using a list of services obtained from the registry, and all service calls will be problematic if the registry is down. So we need a cluster of multiple registries to provide services. We will set up a two-node cluster of registries.

  • Add application-replica1.yml to eureka-sever to configure the first registry
server:
  port: 8002
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: replica1
  client:
    serviceUrl:
      defaultZone: http://replica2:8003/eureka/ # Register with another Eureka registry
    fetch-registry: true
    register-with-eureka: true
Copy the code
  • Add application-replica2.yml to eureka-sever to configure the second registry
server:
  port: 8003
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: replica2
  client:
    serviceUrl:
      defaultZone: http://replica1:8002/eureka/ # Register with another Eureka registry
    fetch-registry: true
    register-with-eureka: true
Copy the code

In this case, we set up a two-node cluster in the registry by registering with each other through the two registries. Since defaultZone uses a domain name, we need to configure it in the host file of the local computer.

  • Modify the local host file
127.0.0.1 replica1
127.0.0.1 replica2
Copy the code

Run the Eureka registry cluster

In IDEA we can launch the same SpringBoot application by using different configuration files.

  • Add two configurations and start eureka-server using application-Replica1. yml and application-replica2.yml respectively

Copy one from the original boot configuration

Configure the startup configuration file

  • Start two Eureka-servers and access one registry http://replica1:8002/ to find that the other has become its backup

  • Modify eureka-client to connect to the cluster

Add the configuration file application-Replica. yml of eureka-client to register it in two registries.

server:
  port: 8102
spring:
  application:
    name: eureka-client
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://replica1:8002/eureka/,http://replica2:8003/eureka/ # Register with both registries
Copy the code

Access to any registry node after starting with this configuration file will see eureka-Client

Add authentication to the Eureka registry

Create a eureka-security-server module and add the following dependencies to pom.xml

You need to add the SpringSecurity module.

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

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

Add the application.yml configuration file

The user name and password for logging in to the registry are configured.

server:
  port: 8004
spring:
  application:
    name: eureka-security-server
  security: # Configure SpringSecurity login username and password
    user:
      name: macro
      password: 123456
eureka:
  instance:
    hostname: localhost
  client:
    fetch-registry: false
    register-with-eureka: false
Copy the code

Add Java configuration WebSecurityConfig

By default, applications that add SpringSecurity dependencies require a CSRF token to access each request. Eureka client registration does not require a CSRF token, so you need to configure the/Eureka /** path that does not require a CSRF token.

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http); }}Copy the code

Run eureka-security-server to accesshttp://localhost:8004Discovery requires login authentication

Eureka-client registers with a registry with login authentication

  • The registry address format needs to be modified in the configuration file
http://${username}:${password}@${hostname}:${port}/eureka/
Copy the code
  • Add the application-security.yml configuration file and change the user name and password according to the format
server:
  port: 8103
spring:
  application:
    name: eureka-client
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://macro:123456@localhost:8004/eureka/
Copy the code
  • Run eureka-client with the application-security.yml configuration. You can see that eureka-client has been registered successfully on the registry page

Common Eureka configurations

eureka:
  client: Eureka client configuration
    register-with-eureka: true Whether to register yourself with the Eureka server
    fetch-registry: true Get a list of eureka services registered on the eureka server
    service-url:
      defaultZone: http://localhost:8001/eureka/ Specifies the registry address
    enabled: true # Enable eureka client
    registry-fetch-interval-seconds: 30 # Define the time interval when you go to eureka server to get the service list
  instance: Eureka client instance configuration
    lease-renewal-interval-in-seconds: 30 # Define how long a service should go to the registry for renewal
    lease-expiration-duration-in-seconds: 90 # Define how long it takes for a service to expire before it is renewed
    metadata-map:
      zone: jiangsu # Location
    hostname: localhost # Service host name
    prefer-ip-address: false # Whether IP is preferred as host name
  server: Eureka server configuration
    enable-self-preservation: false # Disable the protection mechanism on the Eureka server
Copy the code

The module used

Springcloud - learning ├ ─ ─ eureka - server-- Eureka Registry├ ─ ─ eureka ws-security - server-- Eureka registry with login authentication└ ─ ─ eureka - client-- Eureka client
Copy the code

Project source address

Github.com/macrozheng/…

The public,

Mall project full set of learning tutorials serialized, pay attention to the public account for the first time.