Spring Cloud Self-study Notes: Eureka Service Registry Building and Simple demo for Service Registry

First declare a pit that the Spring Cloud version must match the Spring Boot version, otherwise the annotation @EnableEurekaServer cannot be imported into the startup application.java
The corresponding relationship between Spring Cloud version and Spring Boot version can be found in blog https://blog.csdn.net/russle/article/details/80865288.

The Spring Book version used in this article is 2.1.8. The Spring Cloud version corresponding to RELEASE is Finchley.SR2.

The first is to build the eureka-server in the service registry and create a Spring Book project named eureka-server (whose name can be customized):
1. Adding the dependencies required by the eureka server to the pom.xml file:
Spring Boot version dependencies:

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.8.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

Spring Cloud's dependencies:

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
	</dependency>
</dependencies>
<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Finchley.SR2</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

2. Add the eureka server annotation in the startup item Appliction.java: @Enable eureka Server
3. Add configurations to the configuration file application.properties, and convert the configuration files in yml format by themselves:

server.port=1111

eureka.instance.hostname=serviceCenter
#By default, the registry will register with itself, so it is forbidden to register itself.
eureka.client.register-with-eureka=false
#The registry's duty is to maintain service instances, and it does not need retrieval services, so it is forbidden to retrieve services.
eureka.client.fetch-registry=false
#Service Center Address
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

After completing the above configuration, you can start the project and visit localhost:1111 to enter the eureka registry page for viewing.

After completing the configuration of the server, the client is configured and registered in the server. Build the client and create a Spring Book project named eureka-client-01 (name can be customized):
1. Adding the dependencies required by the eureka client to the pom.xml file:
In fact, basically the same as the server, the difference is that one relies on spring-cloud-starter-netflix-eureka-server, and the other relies on spring-cloud-starter-netflix-eureka-client.
Spring Boot version dependencies:

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.8.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

Spring Cloud's dependencies:

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>//The difference is here.
	</dependency>
</dependencies>
<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Finchley.SR2</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

2. Add Eureka service-side annotations in the startup application. java: @Enable Discovery Client or @Enable EurekaClient. The difference between them is that the former is for other registries and the latter is for Eureka registries.
3. Add configurations to the configuration file application.properties, and convert the configuration files in yml format by themselves:

server.port=1001

spring.application.name=eureka-client-01

#Specify the eureka registry address
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

4. As an experiment, write a simple hellocontroller

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(){
        return "Hello World!";
    }
}

Start the server first, then the client. When the client is started, the console will output the following code to represent the registration results:


On the server page, you can see the successful registered server:

Keywords: Spring Java xml

Added by 7724 on Mon, 16 Sep 2019 12:38:22 +0300