Eureka服务治理
Eureka是由Netflix开发的一款服务治理开源框架,Spring-cloud对其进行了集成。Eureka既包含了服务端也包含了客户端,Eureka服务端是一个服务注册中心(Eureka Server),提供服务的注册和发现,即当前有哪些服务注册进来可供使用;Eureka客户端为服务提供者(Server Provider),它将自己提供的服务注册到Eureka服务端,并周期性地发送心跳来更新它的服务租约,同时也能从服务端查询当前注册的服务信息并把它们缓存到本地并周期性地刷新服务状态。这样服务消费者(Server Consumer)便可以从服务注册中心获取服务名称,并消费服务。
关系如下:
#搭建服务注册中心:
说了那么多,我们先来搭建一个Eureka服务端来充当服务注册中心。
新建一个Spring Boot项目,artifactId
填Eureka-Service,然后引入Spring Cloud Edgware.SR3
和spring-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-eureka
和eureka.client.fetch-registry
上面已经解释了其意思,虽然这两个配置的默认值就是true,但这里还是显式配置下,使Eureka客户端的功能更为直观(即向服务端注册服务并定时从服务端获取服务缓存到本地)。
配置好后,启动Eureka-Client,可以从控制台中看到注册成功的消息:
最新文章
- DPDK中断机制简析
- selenium for python 所有方法
- 元组:戴上了枷锁的列表 - 零基础入门学习Python013
- C语言实现的OOP
- Log4Net Config Appender
- Scala学习之for 循环和 yield 的例子
- 浙江大学2015年校赛B题 ZOJ 3861 Valid Pattern Lock
- SVM入门(十)将SVM用于多类分类
- JavaScript学习--(智能社视频)
- shell之路【第四篇】输入输出重定向
- Chapter 1 First Sight——7
- SSH使用TCP Wrappers实现访问控制
- 读《图解HTTP》有感-(HTTP首部)
- (一)Java工程化--Maven基础
- python,判断操作系统是windows,linux
- asyn proposals
- mysqldump 导出数据库各参数详细说明
- windows注册表
- spring 配置 线程池并使用 springtest 进行测试
- 【转】python mysql数据库 &#39;latin-1&#39; codec can&#39;t encode character错误问题解决