SpringCloud First: registration and discovery services (Eureka)

A, spring cloud Introduction

spring cloud provides a number of tools to quickly build distributed systems for developers, including configuration management, service discovery, circuit breakers, routing, micro broker, event bus, global lock, decision-making campaign, distributed session and so on. It is simple operating environment, it can run on a computer developers.

Second, create a service registry

Here, Spring Cloud Netflix Eureka on the use of the components we need, eureka is a service registration and discovery module

 

Personal-use tool that idea, first create a maven project (which we all now), and then create a model project in maven project as a service registry, namely Eureka Server, and the other as Eureka Client. , You can.

Create a module instance screenshots:

The next step -> select cloud discovery-> eureka server, and then been next on the list.

After pom.xml created as follows:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.7.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>eurekaserver</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>eurekaserver</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
	</properties>

	<dependencies>
		<!--eureka-->
		<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-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

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

 In the configuration file application.yml

Server: 
  Port: 9090 
eureka: 
  instance: 
    hostname: localhost 
# because eureka stand-alone, so you want to do some configuration 
  Client: 
    the Register-with-eureka: false 
    FETCH-Registry: false # shown itself to be a eureka Server. 
    Service-url : 
     defaultzone: HTTP: // eureka.instance.hostname} {$: $ {} the server.port / Eureka /

 In your startup class springboot in @EnableEurekaServer

 

@EnableEurekaServer
@SpringBootApplication
public class EurekaserverApplication {

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

}

 

 Start the service to access the page: http: // localhost: 9090

 

Third, create a service provider (eureka client)

When a client registers with the server, it will provide some metadata, such as the host and port, URL, home and so on. Eureka server receiving heartbeat messages from each client instance. If the heartbeat times out, it is usually the instance is deleted from the registration server.

And the creation of the service side of the same, only need to add on the start of class springboot @EnableEurekaClient

Application.yml configuration file

Server: 
  Port: 9091 
# Name services 
the Spring: 
  the Application: 
    name: Service-hi 
Eureka: 
  Client: 
    Service-url: 
     defaultzone: HTTP: // localhost: 9090 / Eureka /

 Writing a controller

@RestController
public class UserController {

    @Autowired
    private EurekaClient eurekaClient;
    @Value("${server.port}")
    String port;
    @RequestMapping("/user")
    public String home(@RequestParam String name) {
        return "访问的端口是:" +port;
    }
    @GetMapping("/info")
    public String getInfo(){
        InstanceInfo instanceInfo= eurekaClient.getNextServerFromEureka("service-hi",false);
        return  instanceInfo.getHomePageUrl();
    }

}

 Start the two services, and then you can see there is a registry of all registered services, service name: service-hi, the port number: 9091

finished. . . . . .

 

Guess you like

Origin www.cnblogs.com/xiaofuzi123456/p/11444310.html