Spring-Cloud-EureKa Service Registry

  

1. Spring-eureka (registration center)
Eureka infrastructure includes
registry, service provider, service consumer
1. Create service registry
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EureKaApp {
public static void main(String[] args) {
SpringApplication.run(EureKaApp.class, args);
}
}
2. In pom Inherit spring-boot-star core in the file, add spring-boot-web client, add spring-cloud-eureka-server, add spring-cloud core file
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring -boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
</parent>
<!-- dependencyManagement dependencies区别 如果在dependencyManagement 设置了版本信息
那在dependencies就不需要设置版本啦 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<!-- <finalName>eurekaservice</finalName> -->
</build>
3. In the application.properties file, add the service port and the previous application name, etc. #Port
name
server.port=6611 #Service
name
spring. application.name=server-service #Set
to false means not to register its own service
eureka.client.register-with-eureka=false #Because
the only test of the registry is an example of maintenance service, he does not need to retrieve the service, so set to false
eureka.client.fetch-registry=false #Address
local ip
eureka.instance.hostname=127.0.0.1 #Access
address defaults to its own service is the registry
eureka.client.serviceUrl.defaultZone=http://${eureka. instance.hostname}:${server.port}/eureka/
## How many seconds is the heartbeat interval service renewal task scheduling time to schedule a heartbeat
eureka.instance.lease-renewal-interval-in-seconds= 7
## Service expiration time : If you haven't received a request for a long time, you can delete the service
eureka.instance.lease-expiration-duration-in-seconds = 7
4. Open the entrance, enter 127.0.0.1 in the browser: the port name to open
the registry 5. A highly available registry is equivalent to creating two The service registration center registers the services in the two registration centers respectively to prevent the service from being affected after the registration center is down.

2. Spring-Cloud creates a client and registers it to the registry
1. Create a startup class (here, use the EnableEurekaClient annotation to inject into the registry, you can also use @EnableDiscoveryClient because EnableEurekaClient internally implements the @EnableDiscoveryClient annotation
or uses @SpringCloudApplication because it internally implements @EnableDiscoveryClien, and @EnableCircuitBreaker (circuit breaker) annotation)
@SpringBootApplication
@EnableEurekaClient
public class RequestApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(RequestApp.class, args);
}
}
2.在pom中增加 spring-boot核心,spring-cloud核心和,spring-cloud-start-eureka,spring-boot-start-web客户端
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
</parent>
<dependencies>
<!-- 添加对Web的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <dependency><!-- need to be referenced to be registered -->
</dependency>


<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<!--依赖管理,用于管理spring-cloud的依赖,其中Camden.SR3是版本号 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

3.application.properties file #Service server.port=6614port Note that this port cannot be the same as the registry portspring.application.name=server-request #Project
name is the name of the application in the registry



#Set the server address to set the address of the registration center, which is the eureka.client.serviceUrl.defaultZone configured above. If there are multiple, comma-separated
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:6611/eureka/
#Command execution timeout, default 1000ms This is configured when using the hystrix fault tolerance mechanism to set the request time (the time unit in milliseconds required for service A to call service B)
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds= 4000
# Whether to enable timeout for execution, true by default
hystrix.command.default.execution.timeout.enabled=true

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325247853&siteId=291194637