Springcloud series five: Adding user authentication and metadata for Eureka server

Source: Internet
Author: User

1. Review

The previous blog explains the Eureka cluster and registers the microservices with the cluster. In the previous explanation, Eureka server allowed anonymous access, and this will explain how to build a Eureka server that requires login to access.

2. Add user authentication for Eureka server

> Copy the Project Microservice-discovery-eureka and change the Artifactid to microservice-discovery-eureka-authenticating.

> Adds a dependency to spring-boot-starter-seurity in Pom.xml that relies on the ability to provide user authentication for Eureka server.

< Dependency >   < groupId >org.springframework.boot</groupId>   <  Artifactid>spring-boot-starter-security</artifactid>  </dependency>

> Add the authentication configuration to the APPLICATION.YML. How to do this without configuration, the default username is user, and the default password is a random value that will be printed when the project starts.

Spring:  Security:    User:      name:root          # username      password:123456    # password

> Launch project, Access http://localhost:8761/to see the authentication dialog box, output the settings of the user name and password to enter.

> Change eureka.client.service-url.defaultzone in micro-service to Http://root:[email protected]: 8761/ eureka/can be. But my test has not been successful, the trace log discovery is because of the csrf problem, the microservices request does not contain the CSRF parameter, so it is Csrffilter intercept. Version EDGWARE.SR2 test succeeded, but the configuration in this version is different, and the configuration of this version is:

Security:  Basic:    enabled:true  User:    name:root    password:123456

3. Eureka Meta-data

There are two types of meta data for > Eureka, standard Metadata and custom metadata, respectively.

    Standard metadata : information such as hostname, IP address, port number, status page, and Health check are published in the service registry for calls between services.

    Custom metadata : Custom metadata can use the eureka.instance.metadata-map configuration, which can be accessed in remote clients, but generally does not alter the behavior of the client. Unless the client knows what the metadata means.

> Copy Project Microservice-provider-user, change Artifactid to Microservice-provider-user-metadata

> Modify APPLICATION.YML to add custom metadata for the microservices using the eureka.instance.instance.metadata-map property.

Eureka:    metadata-map:      my-metadata: My custom meta-data

> Copy Project Microservice-consumer-movie, change Artifactid to Microservice-consumer-movie-metadata

> Modify Moviecontroller, Add Method Showinfo (), display meta-data information for microservices Microservice-provider-user-metadata

/** * Query The information of the Microservice-provider-user service and return * * @return */@GetMapping ("/user-instance") public List < serviceinstance > Showinfo () {    return this.discoveryClient.getInstances ("Microservice-provider-user");}

> Start Microservice-discovery-eureka ( must first start )

> Start Microservice-provider-user-metadata

> Start Microservice-consumer-movie-metadata

> Visits http://localhost:8010/user-instance/and returns something similar to the following.

> Copy the content into the http://json.cn for a clearer look.

4. Summary

This is a very embarrassing article, for a long time have not been able to register the microservices to this version of the Eureka server, the subsequent update if resolved.

The following will explain Eureka's remaining knowledge: self-protection mode, health check, IP selection in multi-NIC environment. Please expect ~ ~ ~

5. Reference

Li---"Spring Cloud and Docker microservices architecture and Combat"

Springcloud series five: Adding user authentication and metadata for Eureka server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.