, Distributed understand the theory and done distributed (years ago), but it felt in the moment of the interview asking too much for springcloud
As a micro-services distributed architecture, the standard form of too much, too fast, a lot of things do not have to care about, and really easy to some, but not all companies have similar programs
Or Not everyone has the relevant project experience
Say useless, tired
What springcloud that? To find it yourself
First, what Eureka that? Ye use, what position?
In a distributed architecture, will be involved in load balancing and clustering, when first appeared, I'm sure there are 10 machines as a cluster, then you know that ip and port 10 machine, Ever
10 machines will be the ip and port are written in code needs to call in, the use of polling, the earliest form of load balancing.
However, for the moment, cloud services and virtual machines are quite common, and can be a container of a docker, fine segmentation, service miniaturization, decoupling and a more independent operation, while also taking into account the cluster
Lateral expansion, so there are a cluster of several servers, how to configure each server is a variable, so the center needs a registration and found the service.
Eureka intention is: discover the amazing feeling of meaning. Of course, it is that springboot based communications protocol is still the http, its heartbeat mechanism to determine for each server
Health, those of us who do not care, while the new Eureka Eureka client requests the server to tell each other that they come automatically added to the cluster, then the cluster on more than one.
This standard of service discovery and registration mechanism, the formation of a standardized structure, that is, the Eureka.
Two, Eureka principles and action
The following picture shows the theft, there are comments contact me (I'll redraw a same !!!)
A Eureka cluster, simply divided into two parts servers Eureka (Eureka Server) client and Eureka (Eureka Client).
Eureka wherein the server is generally 2 or more, preferably physically separated, in order to achieve the effect of the entire cluster disaster recovery. Increase availability, reduce the likelihood of overall hang.
Eureka client, in fact, is the application, each application are written in the client, the client is more than the number, then every request, as long as know the name of the client,
I.e., each address of the client may choose a call from a server or the Eureka, to complete the request cluster.
Less specific to choose, why are a lot of example code from the client only take in an array List (0) the first to use it?
It was just the sample code, do not take it seriously. If the service requires a server cluster to solve, then select any, are possible, the first of several, you can use
A random number is calculated. If all requests can be understood as a kind of distributed, such as 10 clients, each one to carry out a task queue is, then the results
Statistics integration back, concurrent requests and the way this thing is big data is actually a thing.
Three, Eureka's get started
1, version
First, find the official website of the spring, to find examples of Eureka, in https://spring.io/projects springcloud found, and then locate the quick start point, as shown in FIG operation,
Found below the expanded tab Eureka server, as shown below
Then click on the bottom of the page
FIG obtained as follows:
Apply this information to modify pom, modify the contents include: parent, dependency, delete junit, I passed pom amended as follows:
Only posted pom server, where the client's dependence among them, pay attention to comments
<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> <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.lyh</groupId> <artifactId>lyh-eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>lyh-eureka-server</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <!-- SR2会报错,未解决 --> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <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> <dependencies> <!-- web的jar --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- eureka server的jar, 作为client也需要 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- eureka client的jar --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.配置文件
创建resources并且buildpath,编写application.yml配置文件,如下:
server的yml
server:
port: 9010
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eureka-server
client的yml
server:
port: 9020
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9010/eureka/
spring:
application:
name: eureka-client
3.server和client的启动类
编写server的启动类,代码如下:
package com.lyh.lyh_eureka_server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaServerRun { public static void main(String[] args) { SpringApplication.run(EurekaServerRun.class, args); } }
编写client的启动来,代码如下:
package com.lyh.lyh_eureka_server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class EurekaClientRun { public static void main(String[] args) { SpringApplication.run(EurekaClientRun.class, args); } }
4.启动并访问
先启动server,然后启动client,访问server的地址:http://localhost:9010,结果如下图则为正确:
5.一些问题
client的name注册入server的时候,会自动转为大写
红字表示client的心跳维持时间在90秒以上,将会自动删除该注册,是一种server的保护机制
将server中的yml自我保护机制修改,配置文件修改如下:
server:
port: 9010
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
enable-self-preservation: false
spring:
application:
name: eureka-server
修改以后,页面提示会发生变化,表示保护机制已经关闭,如下图
有空继续写!!!