Spring Cloud 之 Spring Cloud Eureka(四)

一 、简介

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
@EnableEurekaServer
public class EurekaApplication {

   public static void main(String[] args) {
      SpringApplication.run(EurekaApplication.class, args);
   }
}

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

spring:
  application:
    name: eureka-server
server:
  port: 8761
eureka:
  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
@RestController
public 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: 8000
spring:
  application:
    name: eureka-client-provider
eureka:
  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
@RestController
public 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: 8001
spring:
  application:
    name: eureka-client-consumer
eureka:
  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 集群




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值