0%

SpringCloud学习——Eureka

Eureka是做什么的?

Eureka作为SpringCloud中的一个关键组件,其主要负责完成服务治理的功能。 服务治理就是用来实现各个微服务实例的注册和发现。 软件设计中的一个常用解耦手段就是引入一个第三方“角色”来进行解耦,例如spring里通过引入容器来进行IOC对“创建对象”这一动作进行了解耦,通过动态代理对象完成了对“使用方”和“被代理对象”之间进行解耦,引入消息队列对服务消费方和服务提供方进行解耦等等… 在微服务中,通过引入注册中心,就能够满足:消费方只需要关心调用哪一个服务而不用关心这个服务由谁提供,而服务方也只提供服务而不用关心这个服务会被谁调用,其他不需要被关心的这层“依赖关系”就交给注册中心去做.

Eureka的几种“角色”

1. 作为Servver:服务注册中心

提供注册与发现的功能,注册中心的集群由多个Eureka互相注册构成

a. 主要需要导入的依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

<!-- 其实这里只要在创建Spring Initializer的时候,勾上spring cloud dependencies就自动生成了 -->
<!-- 切记!组件的版本号,别自己瞎搞!!!版本兼容问题能让你吐血-->
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
</properties>

<dependencies>
...
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
...
</dependencies>

...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

b. 配置配置文件

1
2
3
4
5
6
7
8
9
# 设置端口
server.port=9999
eureka.instance.hostname=localhost
# 为false则代表不向注册中心注册自己(在这里,自己就已经是注册中心了)
# eureka.client.register-with-eureka=false
# 是否从注册中心拉取注册的服务信息(在这里,自己已经是注册中心了)
# eureka.client.fetch-registry=false
# 如果没有特别指定的话,默认将自己注册到哪个zone, 服务中心节点互相注册,就形成了Eureka集群,可以注册到多个,使用,隔开。。。http:// 这几个字符一个都不能少
eureka.client.service-url.defaultZone=http://ip1:port1/eureka/,http://ip2:port2/eureka/

c. 在启动类上增加@EnableEurekaServer注解

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.example.eurek_server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

可以访问 ip:port 来看注册信息.

2. 作为Client:

pom文件需要导入依赖:

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

作为Client的Eureka有着两个角色:

a. 服务提供者:

提供服务的应用,一般是SpringBoot应用(也可以是其他技术平台,但要遵守Eureka通信机制).

  1. 配置properties:

    1
    2
    3
    4
    5
    server.port=9999
    # 设置服务名字
    spring.application.name=hello-service
    # 设置默认注册到的zone的地址信息
    eureka.client.service-url.defaultZone=http://localhost:8080/eureka
  2. 在启动类上加@EnableDiscoveryClient,表示以客户端的身份被发现. @EnableDiscoveryClient是springcloud中的注解,可以继承大部分的注册中心(例如zookeeper),而EnableEurekaClient则是netflix中的注册中心… 所以建议使用@EnableDiscoveryClient

  3. 然后被该springboot项目的rest方法就能作为微服务被consumer调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.example.eureka_service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@EnableDiscoveryClient
@RestController
@SpringBootApplication
public class EurekaServiceApplication {

@RequestMapping("/hello")
public String hello(){
return "hello..";
}
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class, args);
}
}

b. 服务消费者:

  1. 配置properties:

    1
    2
    3
    4
    5
    server.port=1111
    # 设置服务名字
    spring.application.name=hello-consumer
    # 设置默认注册到的zone的地址信息
    eureka.client.service-url.defaultZone=http://localhost:8080/eureka
  2. 在启动类上加@EnableDiscoveryClient,表示以客户端的身份被发现.

  3. 远程调用微服务模块

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    package com.example.eureka_consumer;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.ServiceInstance;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
    import org.springframework.context.annotation.Import;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;

    import java.sql.Struct;

    @EnableDiscoveryClient
    @SpringBootApplication
    @RestController
    @Import({RestTemplate.class})
    public class EurekaConsumerApplication {

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

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient eurekaClient;

    @RequestMapping("hello_consumer")
    public String hi(){
    ServiceInstance serviceInstance = eurekaClient.choose("hello-service");
    String host = serviceInstance.getHost();
    int port = serviceInstance.getPort();
    String uri = "/hello";
    String url = "http://" + host + ":" + port + uri;
    return restTemplate.getForEntity(url,String.class).getBody();
    }
    }