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

Spring Cloud Eureka 服务注册与发现

2017-12-16 11:17 856 查看
Spring Cloud Eureka是基于Netflix Eureka的二次封装,主要负责完成微服务架构中的服务治理功能。

服务治理是微服务架构中最为核心和基础的模块,主要用来实现各个微服务实例的自动化注册和发现。Spring Cloud Eureka就是围绕这服务注册和服务发现机制来完成对微服务应用实例的自动化管理。

之前使用dubbo来进行服务调用时,借助于Zookeeper来实现服务的注册与发现,在Spring Cloud中,整合了eureka,使配置更加简单。

Eureka基础架构

服务端和客户端

服务端:服务注册中心

客户端:各个提供接口的微服务应用

三个核心要素

服务注册中心:Eureka提供的服务端,提供服务注册与发现功能

服务提供者:提供服务的应用,可以是Spring Boot应用,也可以是其他技术平台且遵循Eureka通信机制的应用。它将自己提供的服务注册到Eureka,以供其他英语发现调用。

服务消费者:消费者应用从服务注册中心获取服务列表,从而使消费者可以知道从何处去调用其所需要的服务。

使用Eureka

1、搭建服务注册中心

创建一个基础的Spring Boot工程(eureka-server),在pom.xml中引入eureka依赖。

pom.xml

...
...
<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-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
...
...


在EurekaServerApplication.java文件中加入注解@EnableEurekaServer,表示启动一个服务注册中心提供给其他应用。

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


application.properties中对服务注册中心的属性进行设置

在默认情况下,服务注册中心会将自己作为客户端来注册自己,通过eureka.client.register-with-eureka=false可以禁止它的客户端注册行为

server.port=1111

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
#仅仅维护服务实例,不去获取服务
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone = http://${eureka.instance.hostname}:${server.port}/eureka/[/code] 
高可用的注册中心(集群形式)

Eureka的高可用实际上就是将自己作为服务向其他服务注册中心注册自己,这样就形成了一组互相注册的服务注册中心,以实现服务清单的互相同步,以达到高可用效果。

构建一个双节点的服务注册中心

application-peer1.properties

spring.application.name=eureka-server
server.port=1111

eureka.instance.hostname=peer1
eureka.client.service-url.defaultZone = http://peer2:1112/eureka/[/code] 
application-peer2.properties

spring.application.name=eureka-server
server.port=1112

eureka.instance.hostname=peer2
eureka.client.service-url.defaultZone = http://peer1/1111/eureka/[/code] 
修改系统中的hosts文件,加上

127.0.0.1 peer1

127.0.0.1 peer2

分别启动peer1,peer2

启动命令:

java -jar C:\Users\lujie\Desktop\eureka-server-0.0.1-SNAPSHOT.jar –spring.profiles.active=peer1

java -jar C:\Users\lujie\Desktop\eureka-server-0.0.1-SNAPSHOT.jar –spring.profiles.active=peer2

访问http://localhost:1111



访问http://localhost:1112



如果有三个节点或更多节点,同样的方式进行设置

截图上的红色部分的字,是Eureka开启了自我保护机制

服务在注册到Eureka Server后,会维护一个心跳连接,告诉Eureka Server自己还活着。在Eureka Server运行期间,它会统计心跳失败的比例在15分钟内是否低于85%,如果低于,则会把当前的服务实例保护起来,让这些实例不会过期。这种做法存在一定的问题,如果服务实例的这段保护期内出现了问题,则客户端就很有可能会拿到实际已经不存在的服务实例,会出现调用失败的情况,所以客户端要有容错机制,比如可以使用请求重发、断路器等机制。

可以使用eureka.server.enable-self-preservation=false来关闭保护机制

2、注册服务提供者

同样创建一个基础的Spring Boot工程(eureka-server),在pom.xml中引入eureka、web依赖。

pom.xml文件略

新建一个HelloController,通过/hello请求来打印服务相关内容

@RestController
public class HelloController
{
private <
c064
span class="hljs-keyword">final Logger logger = Logger.getLogger(getClass());

@Autowired
private DiscoveryClient client;

@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String index()
{
ServiceInstance instance = client.getLocalServiceInstance();
logger.info("/hello,host:"+instance.getHost()+"service_id:"+instance.getServiceId());
return "hello world";
}
}


在主类中加入注解@EnableEurekaClient,激活Eureka中的DiscoveryClient实现

@EnableEurekaClient
@SpringBootApplication
public class HelloApplication {

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


最后修改application.properties配置文件

spring.application.name=hello-service
eureka.client.service-url.defaultZone = http://peer1:1111/eureka/[/code] 
如果是将服务注册到Eureka集群中,配置文件如下:

spring.application.name=hello-service
eureka.client.service-url.defaultZone = http://peer1:1111/eureka/,http://peer2:1111/eureka/[/code] 

服务的发现与消费

这里借助ribbon,之后记录。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息