Eureka是由Netflix开发的一款服务治理开源框架,Spring-cloud对其进行了集成。Eureka既包含了服务端也包含了客户端,Eureka服务端是一个服务注册中心(Eureka Server),提供服务的注册和发现,即当前有哪些服务注册进来可供使用;Eureka客户端为服务提供者(Server Provider),它将自己提供的服务注册到Eureka服务端,并周期性地发送心跳来更新它的服务租约,同时也能从服务端查询当前注册的服务信息并把它们缓存到本地并周期性地刷新服务状态。这样服务消费者(Server Consumer)便可以从服务注册中心获取服务名称,并消费服务。

关系如下:

#搭建服务注册中心:

说了那么多,我们先来搭建一个Eureka服务端来充当服务注册中心。

新建一个Spring Boot项目,artifactId填Eureka-Service,然后引入Spring Cloud Edgware.SR3spring-cloud-starter-eureka-server:

 <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency> </dependencies>

在启动类上添加@EnableEurekaServer注解,表明这是一个Eureka服务端:

接着在application.yml中添加一些配置:

server:
port: 8080 eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

上面配置了服务的端口为8080,剩下几个为Eureka配置:

  • eureka.instance.hostname指定了Eureka服务端的IP;

  • eureka.client.register-with-eureka表示是否将服务注册到Eureka服务端,由于自身就是Eureka服务端,所以设置为false;

  • eureka.client.fetch-registry表示是否从Eureka服务端获取服务信息,因为这里只搭建了一个Eureka服务端,并不需要从别的Eureka服务端同步服务信息,所以这里设置为false;

  • eureka.client.serviceUrl.defaultZone指定Eureka服务端的地址,默认值为http://localhost:8761/eureka

配置完毕后启动服务,访问http://localhost:8080/,可看到:

由于还没有Eureka客户端将服务注册进来,所以Instances currently registered with Eureka列表是空的。

下面我们接着搭建一个Eureka客户端来提供服务。

新建一个Spring Boot项目,artifactId填Eureka-Client,然后引入以下依赖:

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>

接着编写一个TestController,对外提供一些REST服务:

@RestController
public class TestController {
private Logger log = LoggerFactory.getLogger(this.getClass()); @Autowired
private DiscoveryClient client; @GetMapping("/info")
public String info() {
@SuppressWarnings("deprecation")
ServiceInstance instance = client.getLocalServiceInstance();
String info = "host:" + instance.getHost() + ",service_id:" + instance.getServiceId();
log.info(info);
return info;
}
@GetMapping("/hello")
public String hello() {
return "hello world";
}
}

上面代码注入了org.springframework.cloud.client.discovery.DiscoveryClient对象,可以获取当前服务的一些信息。

编写启动类,在启动类上加@EnableDiscoveryClient注解,表明这是一个Eureka客户端:

@EnableDiscoveryClient
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

最后配置application.yml:

server:
port: 8082

spring:
application:
name: Server-Provider

eureka:
client:
#Eureka服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为。
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://localhost:8080/eureka/

稍微说明下这些配置的意思:

  • server.port指定了服务的端口为8082;

  • spring.application.name指定服务名称为Server-Provider,后续服务消费者要获取上面TestController中接口的时候会用到这个服务名;

  • eureka.client.serviceUrl.defaultZone指定Eureka服务端的地址,这里为上面定义的Eureka服务端地址;

  • eureka.client.register-with-eurekaeureka.client.fetch-registry上面已经解释了其意思,虽然这两个配置的默认值就是true,但这里还是显式配置下,使Eureka客户端的功能更为直观(即向服务端注册服务并定时从服务端获取服务缓存到本地)。

配置好后,启动Eureka-Client,可以从控制台中看到注册成功的消息:

最新文章

  1. DPDK中断机制简析
  2. selenium for python 所有方法
  3. 元组:戴上了枷锁的列表 - 零基础入门学习Python013
  4. C语言实现的OOP
  5. Log4Net Config Appender
  6. Scala学习之for 循环和 yield 的例子
  7. 浙江大学2015年校赛B题 ZOJ 3861 Valid Pattern Lock
  8. SVM入门(十)将SVM用于多类分类
  9. JavaScript学习--(智能社视频)
  10. shell之路【第四篇】输入输出重定向
  11. Chapter 1 First Sight——7
  12. SSH使用TCP Wrappers实现访问控制
  13. 读《图解HTTP》有感-(HTTP首部)
  14. (一)Java工程化--Maven基础
  15. python,判断操作系统是windows,linux
  16. asyn proposals
  17. mysqldump 导出数据库各参数详细说明
  18. windows注册表
  19. spring 配置 线程池并使用 springtest 进行测试
  20. 【转】python mysql数据库 &#39;latin-1&#39; codec can&#39;t encode character错误问题解决

热门文章

  1. loadrunner11汉化时提示模块插件无法注册的解决方法
  2. uniapp input框聚焦时软键盘弹起整个页面上滑,固定页面不让上滑问题
  3. Python 文件操作(IO 技术)
  4. 牛客小白月赛65ABCD(E)
  5. 从开发属于你自己的第一个 Python 库,做一名真正的程序员「双语版」
  6. 【力扣】反转链表I和II(迭代和递归)
  7. day12-实现Spring底层机制-02
  8. Swagger2多包扫描
  9. springcloud 08 Hystrix图形化DashBoard
  10. python判断密码连续,重复,大小写、数字、符号混合密码复杂度要求