SpringCloud Learning Practice (1)--Service Registration and Discovery (Eureka)

I have time to learn about SpringCloud. According to the experience of the great god (this is very detailed, click to open the link for dry goods ), I actually operated it myself, and I still stepped on a lot of pits, so I summarized it so as not to forget.


1. Introduction to Spring Cloud

Spring Cloud provides developers with tools to quickly build distributed systems, including configuration management, service discovery, circuit breakers, routing, micro-agents, event buses, global locks, decision campaigns, distributed sessions, and more. It runs in a simple environment and can run on the developer's computer. Combining springboot for microservice architecture development is currently a very popular architecture, which is simple and convenient.

2.Eureka

eureka is a service registration and discovery module. Including Eureka Server and Eureka Client , the client registers the service to the server so that it can be discovered and monitored.

3. Eureka Server service construction

3.1 Create a new springboot project (maven project, jar package) eurekaserver, the pom file is as follows:

<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.yuanyuan</groupId>
  <artifactId>eurekaserver</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>eurekaserver</name>
  <description>Demo project for Spring Boot</description>
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <!-- spring boot test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>
tips: 

(1) The springboot version is best to use 1.5.2.RELEASE. If the version is too new, various problems will occur, and the pit has been stepped on.

(2) Corresponding to the springcloud version Dalston.RELEASE, I used Dalston.RC1 to refer to the great god and found that the class could not be found, and the annotation @EnableEurekaServer could not be imported. After changing the version, everything is normal.

3.2 The springboot startup class is as follows:

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

3.3  eureka is a highly available component. It has no back-end cache. After each instance is registered, it needs to send a heartbeat to the registry (so it can be done in memory). By default, the erureka server is also an eureka client, and one must be specified. server.

The configuration file appication.yml of eureka server:

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Use eureka.client.registerWithEureka: false and fetchRegistry: false to indicate that you are an eureka server instead of an eureka client

3.4 Start the project, visit http://localhost:8761/, you can see the main page of Eureka, there is no service registration at this time, so no service is discovered.


At this point, the eureka server is successful, and then build the eureka client.

4.  eureka client build

4.1 Create a new springboot project eurekaclient, the pom file is as follows:
<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.yuanyuan</groupId>
  <artifactId>eurekaclient</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>eurekaclient</name>
  <description>Demo project for Spring Boot</description>
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

4.2  The springboot startup class is as follows:

package com.yuanyuan.eurekaclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Hello world!
 *
 */
@RestController
@SpringBootApplication
@EnableEurekaClient
public class EurekaclientApplication
{
    public static void main( String[] args )
    {
        SpringApplication.run(EurekaclientApplication.class, args);
    }
    
    @Value("${server.port}") //Read the Port in the configuration file
    String port;
    
    @RequestMapping("/hi")
    public String sayHi(@RequestParam String name) {
    	return "hi" + name + ",my port is:" + port;
    }
}
Indicate yourself as an eureka client by annotating @EnableEurekaClient.
Interface + startup class, write a simple interface for easy access to tests.

4.3 application.yml:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8762
spring:
  application:
    name: service-hi

In addition to the @EnableEurekaClient annotation, you also need to specify the address of your own service registry in the configuration file .

tips:

application name is a very important attribute, which specifies the name of the current service, through which the corresponding service can be found, so the name needs to be uniquely identifiable in the entire project.

4.4 Start the project and visit http://localhost:8762/hi?name=yuanyuan to see:


After the eureka client is successfully built, refresh the server again (http://localhost:8761/) as follows:


At this time, a service has been registered in the service, the service name is SERVICE-HI, and the port is 8762

Guess you like

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