Eureka
What is the Eureka
Eureka is a reST-based service that is part of Netflix’s suite of microservices. Mainly responsible for the service governance function in the microservice architecture. It is used to realize automatic registration and discovery of each micro-service acceptance. Eureka consists of two parts: Eureka Server and Eureka Client.
The infrastructure
Eureka consists of two parts: Eureka Server and Eureka Client. Eureka Client includes service providers and service consumers. So Eureka’s infrastructure has three core elements: Eureka Server(service registry), service provider, and service consumer.
Service Registry
Provides registration and discovery of services.
Eureka Client
Eureka Client is a Java application that initiatively registers its services in the registry at startup time. It can also query the current registered service information from the server, cache it locally and periodically refresh the service status.
Eureka Client can be divided into two roles: Application Server(service provider) and Application Client(service consumer). One is a service provider and the other is a service consumer. There is no absolute division between the two. When a service is provided, it is a Server, and when other services are invoked, it is a Client.
Set up Eureka service
Since Spring Cloud is based on SpringBoot, the services built are based on the SpringBoot project, which can be created directly using IDEA or in Spring Initializr.
Service Registry
Pom file
<! -- SpringBoot version -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9. RELEASE</version>
<relativePath/>
</parent>
<! -- SpringCloud version -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Copy the code
The configuration file
Name = spring-cloud-eureka # service port server.port = 8761 Register-with-eureka =false The default is true eureka.client.fetch-registry=false # Service address eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/Copy the code
Start the class
The @enableEurekaserver annotation indicates that this is a EurekaServer registry. After startup, visit localhost:8761 in your browser and you can see the visual interface of the registry.
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); }}Copy the code
Service provider
Service providers need to register themselves in a registry to provide services to service consumers.
Pom file
<! -- SpringBoot version -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9. RELEASE</version>
<relativePath/>
</parent>
<! -- SpringCloud version -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Copy the code
** Configuration file **
# app name, Name =spring-cloud-provider # server.port=8080 # registry address eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/Copy the code
Start the class
The annotation @enableDiscoveryClient indicates that the service is a Eureka client.
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); }}Copy the code
producers
Create a Controller that returns a string of fields representing what the producer produced.
@RestController
public class ProviderController {
@GetMapping("/provider/service")
public String provider(a){
return "I'm provider"; }}Copy the code
Service consumer
Similarly, create a SpringBoot project to represent the client.
Pom file
<! -- SpringBoot version -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9. RELEASE</version>
<relativePath/>
</parent>
<! -- SpringCloud version -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Copy the code
The configuration file
# app name, Name =spring-cloud-consumer # service port server.port=9000 # registry address eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/Copy the code
Start the class
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); }}Copy the code
Service consumer
Create a Fegin interface to invoke the registry’s services. The @feignClient name is the same as spring.application.name to indicate which service is invoked. GetMapping is the path of methods on the production end.
@FeignClient(name= "spring-cloud-provider")
public interface ProviderInterface {
@GetMapping(value = "/provider/service")
public String hello(a);
}
Copy the code
Create a Controller and invoke the service
@RestController
public class ConsumerController {
@Autowired
private ProviderInterface providerInterface;
@GetMapping("/consumer/service")
public String consumer(a) {
returnproviderInterface.hello(); }}Copy the code
Finally in the browser visit: http://localhost:9000/consumer/service, if got the server returns a string, that builds success.
High availability Service registry
In a microservices architecture, if there is only one registry, the entire system becomes unavailable when the registry fails. In the single-node registry, we used two configurations to let the registry not register itself, so in high availability, we don’t add these two annotations to let the registry register itself with other services.
Register-with-eureka =false The default is true eureka.client.fetch-registry=falseCopy the code
Configure DNS
Add the MAPPING between IP and address in the C:\Windows\System32\drivers\etc\HOSTS file.
127.0.0.1 peer1
127.0.0.1 peer2
Copy the code
Create a SpringBoot project
Pom file
<! -- SpringBoot version -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9. RELEASE</version>
<relativePath/>
</parent>
<! -- SpringCloud version -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Copy the code
The configuration file
Configure the configuration of the two EureKas in the configuration file. To build a high availability architecture, can configure multiple values for the defaultZone: http://peer1:8762/eureka, http://peer1:8761/eureka, use the comma separated.
spring:
application:
name: eureka-cluster
profiles: peer1
server:
port: 8761
eureka:
instance:
hostname: peer1
client:
serviceUrl:
defaultZone: http://peer1:8762/eureka
---
spring:
application:
name: eureka-cluster
profiles: peer2
server:
port: 8762
eureka:
instance:
hostname: peer2
client:
serviceUrl:
defaultZone: http://peer1:8761/eureka
Copy the code
Start the class
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); }}Copy the code
Start the way
mvn clean package
java -jar xx.jar --spring.profiles.active=peer1
java -jar xx.jar --spring.profiles.active=peer2
Copy the code
The DS Replicas for peer1 and DS Replicas for Peer2 will present a visual representation of the registry. This means that the ha registry has been set up successfully.