Spring Cloud 之 Spring Cloud Eureka(四)

来源:互联网 发布:我本知这世界 编辑:程序博客网 时间:2024/06/06 08:37

一 、简介

Eureka 是Netflix公司开源的一个服务注册与发现组件。Spring将它集成进来形成Spring Cloud Eureka 。

二 、构建eureka server 注册中心

创建一个Spring Boot项目 添加依赖 spring-cloud-starter-eureka-server .
<dependency>   <groupId>org.springframework.cloud</groupId>   <artifactId>spring-cloud-starter-eureka-server</artifactId></dependency>
创建程序入口类,增加注解@EnableEurekaServer
@SpringBootApplication@EnableEurekaServerpublic class EurekaApplication {   public static void main(String[] args) {      SpringApplication.run(EurekaApplication.class, args);   }}

配置文件application.yml 增加eureka相关配置

spring:  application:    name: eureka-serverserver:  port: 8761eureka:  instance:    hostname: localhost  client:    register-with-eureka: false    fetch-registry: false    service-url:      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/  server:    eviction-interval-timer-in-ms: 60000 #清理时间间隔

hostname:eureka所在服务器名(需要修改host 文件)
register-with-eureka:false 不将自身注册到eureka
fetch-registry:false 不获取注册服务类别
eviction-interval-timer-in-ms 扫描间隔毫秒

启动程序:访问localhost:8761查看eureka监控页面 

三、构建eureka-client-provider

创建spring boot 项目 添加依赖 spring-cloud-starter-web,spring-cloud-start-eureka

<dependency>   <groupId>org.springframework.cloud</groupId>   <artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-web</artifactId></dependency>

创建程序入口类 增加注解@EnableEurekaClient或者@EnableDiscoveryClient

@SpringBootApplication@EnableEurekaClient@RestControllerpublic class EurekaClientProviderApplication {   @RequestMapping("/hello")   public String hello(){      return "hello";   }   public static void main(String[] args) {      SpringApplication.run(EurekaClientProviderApplication.class, args);   }}

配置文件appliction.yml 增加eureka 配置

server:  port: 8000spring:  application:    name: eureka-client-providereureka:  client:    service-url:      defaultZone: http://localhost:8761/eureka/  #eureka服务地址 用来服务发现  instance:    lease-renewal-interval-in-seconds: 3 #3秒钟一次心跳    lease-expiration-duration-in-seconds: 5 #5秒超时

启动服务 访问 localhost:8000/hello  返回hello 程序正常

四、构建eureka-client-consumer
创建spring boot 项目 添加依赖 spring-cloud-starter-web,spring-cloud-start-eureka
<dependency>   <groupId>org.springframework.cloud</groupId>   <artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-web</artifactId></dependency>

创建程序入口类,并提供hello服务。

@SpringBootApplication@EnableEurekaClient@RestControllerpublic class EurekaClientConsumerApplication {   @Autowired   RestTemplate restTemplate;   @Bean   @LoadBalanced   RestTemplate restTemplate(){      return new RestTemplate();   }   @RequestMapping("/hello")   public String hello(){      return  restTemplate.getForObject("http://eureka-client-provider/hello", String.class);   }   public static void main(String[] args) {      SpringApplication.run(EurekaClientConsumerApplication.class, args);   }}

在这个类中,我们通过restTemplate来构建负载均衡,在构建是添加注解@LoadBanlance启用RestTemplate负载均衡能力。这里的访问的路径 http://eureka-client-provider/hello 使用的是eureka-client-provider而不是localhost:8000.

配置文件 application.yml
server:  port: 8001spring:  application:    name: eureka-client-consumereureka:  client:    service-url:      defaultZone: http://localhost:8761/eureka/  #eureka服务地址 用来服务发现  instance:    lease-renewal-interval-in-seconds: 3 #3秒钟一次心跳    lease-expiration-duration-in-seconds: 5 #5秒超时

启动服务 访问 localhost:8001/hello 会发现成功返回 hello.
访问localhost:8761 会发现有两个服务注册到eureka上。

下篇将讲解eureka 集群




原创粉丝点击