Eureka
Usage Scenarios
Micro-services, the business module to 1 as divided into one service. Early, the call between services we can use RESTAPI to communicate, but to the late, after more services, but also through RESTAPI access to other services, because RESTAPI use Ip+port to access, the number of services to go up, The way code is embedded in an address or configuration file can cause administrative changes.
Figure 1
So, we started using a simpler way of registering the center (Eureka) to implement the invocation of the service,go!
Technology architecture
There is a full version of the Eureka Architecture Diagram , here we do not repeat, with a simple architecture diagram, to understand the Eureka architecture
Figure 2
According to Figure 2, we can tell that Eureka the participants to
• Service Discovery
• Service Providers
• Service Consumers
first , the service discovery is the server, and the provider and the consumer are the clients (the client);
The client registers itself on the server side, and the server can require the client to return the client in other services;
Benefits: between the client and client, only need to know each other on the server to register the name, regardless of the other party ip,port, content changes will not be affected;
Start combat
Service side
Pom
<!--parent Module Information-- <parent> <groupId>org.springframework.cloud</groupId> < Artifactid>spring-cloud-starter-parent</artifactid> <version>Finchley.SR1</version> </parent> <!--referencing Jars-- <dependencies> <!--Cloud ---<dependency > <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-starter </artifactId> </dependency> <!--Eureka Server-- <dependency> < Groupid>org.springframework.cloud</groupid> <artifactId> spring-cloud-starter-netflix-eureka-server</artifactid> </dependency> </dependencies >
Yml
Server: port:8080 # The port number for the current Eureka Server service is 8080eureka: instance: Hostname:localhost # current Eureka hostname for localhost client: Register-with-eureka:false # The current service does not need to be registered as a client fetch-registry on Eureka Server : False # Serviceurl: # Eureka service Address defaultzone:http://localhost:8080/eureka/
Client A
Pom
<!--parent Module Information--<parent> <groupId>org.springframework.cloud</groupId> <art Ifactid>spring-cloud-starter-parent</artifactid> <version>Finchley.SR1</version> </paren t> <!--referencing Jars--<dependencies> <dependency> <groupid>org.springframew Ork.boot</groupid> <artifactId>spring-boot-starter-web</artifactId> </dependency> ; <!--cloud--<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <!--Eureka Server--&G T <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring -cloud-starter-netflix-eureka-client</artifactid> </dependency> </dependencies>
Yml
Spring: application: name:provider-source #服务名称server: port:8082 #启动端口号eureka: instance: Hostname:localhost client: register-with-eureka:true #是否注册到eureka服务器 fetch-registry:true #是否可检索 Service-url: defaultzone:http://localhost:8080/eureka/#服务中心
Client B
Pom
<!--parent Module Information--<parent> <groupId>org.springframework.cloud</groupId> <artifact Id>spring-cloud-starter-parent</artifactid> <version>Finchley.SR1</version> </parent> <!--referencing Jars--<dependencies> <dependency> <groupid>org.springframework.b Oot</groupid> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--cloud--<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <!--Eureka Client -<dependency> <groupId>org.springframework.cloud</groupId> <artifa Ctid>spring-cloud-starter-netflix-eureka-client</artifactid> </dependency> </dependency> ; </dependencies&gT
Yml
Spring: application: Name:provider-speaker # Service Name server: port:8081 # Service occupancy port number Eureka: instance: Hostname:localhost client: register-with-eureka:true #是否注册到eureka服务器 fetch-registry:true #是否可检索 Service-url: defaultzone:http://tj:tj123456@localhost:8080/eureka/#服务中心
Service-side Startup class add annotations
@SpringBootApplication @enableeurekaserver # Eureka Service-side annotations public class discoveryapplication {public static void Main (string[] args) { springapplication.run (Discoveryapplication.class, args);} }
Client A, B, add annotations separately
Package Com.tj;import org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import Org.springframework.cloud.netflix.eureka.EnableEurekaClient, @SpringBootApplication @enableeurekaclient # Eureka Client Annotations public class speakerapplication {public static void main (string[] args) {Springapplication.run (Speakerapplication.class, args);}}
Next, start the server side, client A, client B;
Open, server-side address http://localhost:8080, you can see that with 3, the client is registered successfully
Figure 3
At this point, Eureka get started, has been completed.