"Micro-Service Architecture" Springcloud Eureka (service registration and Service Discovery Basics) (ii)

Source: Internet
Author: User

The previous article explains Springcloud components and concepts, and then explains the use, principle, and role of each component of the Springcloud component, which mainly provides modules including: service Discovery (Eureka), Circuit breaker (Hystrix), Smart Path (Zuul), Client load balancer (Ribbon), Archaius,turbine etc today, Eureka is the registry

A: Eureka Introduction

Eureka is a sub-module of spring Cloud Netflix and one of the core modules. For cloud service discovery, a rest-based service for locating services to enable mid-tier service discovery and failover in the cloud.

Service registration and discovery is very important for microservices systems. With the service discovery and registration, you do not need to change the service call configuration file all day, you just need to use the service identifier, you can access to the service. His function is similar to Dubbo's registry.

Service discovery: Service discovery is one of the key principles of microservices infrastructure. Attempting to configure a contract for each client or a format can be said to be very difficult and very fragile. Eureka is a service and client discovered by the Netflix service. This service can be configured and deployed with high availability, and the status of each service can be replicated to each other within the registered service.

Service registration: When a client registers to Eureka, it provides metadata about itself (such as host and port, health metrics URL, home page, etc.) Eureka receives heartbeat information from each instance through a service. If the heartbeat fails to receive more than the configured time, the instance will normally be removed from the registration

is the basic service registration and discovery

II: Eureka Service Discovery and Registration (create Registry)

1: Create a basic spring boot project and introduce the required dependencies in Pom.xml

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667 <?xml version="1.0"encoding="UTF-8"?>    <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">        <modelVersion>4.0.0</modelVersion>        <groupId>com.demo.springcloud</groupId>        <artifactId>eureka_register_service</artifactId>        <version>1.0.0</version>        <packaging>jar</packaging>        <name>eureka_register_service</name>        <description>Spring Cloud project</description>        <parent>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-parent</artifactId>            <version>1.4.3.RELEASE</version>            <relativePath />         </parent>         <properties>            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>            <java.version>1.8</java.version>        </properties>         <dependencies>            <dependency>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-starter-test</artifactId>                <scope>test</scope>            </dependency>            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-starter-eureka-server</artifactId>            </dependency>                    <dependency>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-starter-security</artifactId>            </dependency>        </dependencies>        <dependencyManagement>            <dependencies>                <dependency>                    <groupId>org.springframework.cloud</groupId>                    <artifactId>spring-cloud-dependencies</artifactId>                    <version>Brixton.RELEASE</version>                    <type>pom</type>                    <scope>import</scope>                </dependency>            </dependencies>        </dependencyManagement>         <build>            <plugins>                <plugin>                    <groupId>org.springframework.boot</groupId>                    <artifactId>spring-boot-maven-plugin</artifactId>                </plugin>            </plugins>        </build>    </project>           

2: Create a Startup class application

1234567891011121314 packagecom.demo.springcloud; importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication publicclassApplication {   publicstaticvoidmain(String[] args) {       SpringApplication.run(Application.class, args);   } }

Start a service registry with @enableeurekaserver annotations to provide conversations to other apps.

3: Create a profile application.properties, be careful not to appear blank, no start error
1234  server.port=8000 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

4: Execute the bluid.sh to build and then execute the main method. I have not been manually compiled under Eclipse, the boot time has been not read application.properties.

5: View the registration center

Http://127.0.0.1:8000/can be seen as

is not very simple ah, then people to ask, why even login account and password are not, outside the network is not directly able to enter the registration center, so it is not safe ah. Okay, then we'll add the login ID and password.

Three: Eureka Registration CENTER Join permission

1: Join the registry need to introduce Jar, join in Pom.xml

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

2: Configuration file Create a new bootstrap.yml file

123456 security:    basic:      enabled: true    user:      name: admin      password: 123

Remember to recompile the package and then execute the main method.

Four: Register a service

First of all to remove the registration center to join the authority, or the server will not link to the registration Center for registration, I do not know how to match the server Connection registry account and password configuration.

Create a biz-serice-0 engineering service, related code I'm not here to explain that you can download Https://github.com/zhp8341/SpringCloudDemo directly from git

Execute demo should be able to see the above results, is the registry found the service biz-service-0, is the service exposed interface

Next talk about the high availability of the registration center, we are now using a single, easy to cause a single point of failure!!!

"Micro-Service Architecture" Springcloud Eureka (service registration and Service Discovery Basics) (ii)

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.