Spring-Cloud-EureKa 服务注册中心

  

1.Spring-eureka(注册中心)
Eureka基础架构包括
注册中心,服务提供者,服务消费者
1.创建服务注册中心
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.在pom文件中继承spring-boot-star核心 增加 spring-boot-web客户端 增加spring-cloud-eureka-server 增加spring-cloud核心文件
<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.在application.properties文件中增加服务端口以前 应用名称等
#端口名称
server.port=6611
#服务名称
spring.application.name=server-service
#设置为false代表不注册自己服务
eureka.client.register-with-eureka=false
#由于注册中心的只测就是维护服务示例,他并不需要去检索服务,所以设置为false
eureka.client.fetch-registry=false
#地址 本地ip
eureka.instance.hostname=127.0.0.1
#访问地址 默认为自己的服务就是注册中心
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
## 心跳间隔 服务续约任务的调度时间 多少秒调度一次 心跳
eureka.instance.lease-renewal-interval-in-seconds= 7
## 服务失效时间: 如果多久没有收到请求,则可以删除服务
eureka.instance.lease-expiration-duration-in-seconds = 7
4.开启入口,在浏览器中输入127.0.0.1:端口名 即可打开注册中心
5.高可用的注册中心也就是相当于创建两个服务注册中心 将服务分别注册到两个注册中心,防止注册中心宕机后影响业务。

2.Spring-Cloud创建客户端并注册到注册中心
1.创建启动类(这里使用EnableEurekaClient注解注入到注册中心的 也可以使用@EnableDiscoveryClient因为EnableEurekaClient内部实现了@EnableDiscoveryClient注解
或者使用@SpringCloudApplication因为他内部实现了@EnableDiscoveryClien,和@EnableCircuitBreaker(断路器)注解)
@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>
<!-- 要注册的需要引用 -->
<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文件
#服务名称 在注册中心中application的名称
spring.application.name=server-request
#项目端口 注意该端口不可和注册中心端口一直
server.port=6614
#设置服务器地址 设置注册中心的地址也就是上面所配置的eureka.client.serviceUrl.defaultZone 如果是多个的话逗号隔开
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:6611/eureka/
#命令执行超时时间,默认1000ms 这个是在使用hystrix容错机制时配置的用于设置请求时间(服务A调用服务B所需的时间单位毫秒)
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=4000
# 执行是否启用超时,默认启用true
hystrix.command.default.execution.timeout.enabled=true

猜你喜欢

转载自www.cnblogs.com/lvwqq/p/8978582.html