您的位置:首页 > 编程语言 > Java开发

Spring Cloud 之 Spring Cloud Eureka(四)

2017-08-18 15:48 525 查看

一 、简介

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 集群
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: