说明

参考 eureka-demo 工程中(SpringCloud_RestTemplate.md里有具体步骤)服务之间远程例子,
SpringCloud_Eureka - 图1
user-service 对外提供服务,需要对外暴露自己的地址。而 consumer-service (调用者)需要记录服务提供者的地址。将来地址出现变更,还需要及时更新。这在服务较少的时候并不觉得有什么,但是在现在日益复杂的互联网环境,一个项目肯定会拆分出十几,甚至数十个微服务。此时如果还人为管理地址,不仅开发困难,将来测试、发布上线都会非常麻烦,这与 DevOps 的思想是背道而驰的。

demo

eureka-demo 工程的基础上,进行 demo 测试,
新建 eureka-service 子工程
添加 eureka启动器依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring-cloud-learn</artifactId>
  7. <groupId>com.it.learn</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>eureka-service</artifactId>
  12. <properties>
  13. <maven.compiler.source>8</maven.compiler.source>
  14. <maven.compiler.target>8</maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <!-- 导入Eureka的启动器 -->
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  21. </dependency>
  22. </dependencies>
  23. </project>

添加启动类,并添加 @EnableEurekaServer 注解,

  1. package com.it.learn;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @SpringBootApplication
  6. @EnableEurekaServer // 声明这个应用是一个EurekaServer
  7. public class EurekaServiceApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(EurekaServiceApplication.class);
  10. }
  11. }

添加 application.yml 配置文件

  1. server:
  2. port: 10086
  3. spring:
  4. application:
  5. name: eureka-service
  6. eureka:
  7. client:
  8. service-url:
  9. defaultZone: http://127.0.0.1:10086/eureka # 设置eureka服务器地址
  10. register-with-eureka: false # 不注册自己
  11. fetch-registry: false # 不拉取服务本eureka服务中心的服务信息

项目结构如下
SpringCloud_Eureka - 图2
启动服务,浏览器访问 http://127.0.0.1:10086
SpringCloud_Eureka - 图3

服务注册

注册服务:就是让服务提供者 user-service 把自己的信息注册到 eureka-service 上,方便服务消费者去拉取。
user-service 中添加 Eureka 客户端依赖

  1. <!-- Eureka客户端 -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>

修改 user-service 配置文件,指定 eureka 地址
这里我们添加了 spring.application.name 属性来指定应用名称,将来会作为服务的id使用。

  1. server:
  2. port: 8081
  3. spring:
  4. datasource:
  5. driver-class-name: com.mysql.jdbc.Driver
  6. url: jdbc:mysql://192.168.206.99:3306/db2?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false
  7. username: root
  8. password: root
  9. application:
  10. name: user-service # 服务的名称不能使用下划线
  11. mybatis:
  12. type-aliases-package: com.it.learn.pojo
  13. configuration:
  14. map-underscore-to-camel-case: true
  15. logging:
  16. level:
  17. com.it.learn: debug
  18. eureka:
  19. client:
  20. service-url: # eureka-service地址
  21. defaultZone: http://127.0.0.1:10086/eureka/

重启 user-service 工程,查看 eureka 客户端
我们发现 user-service 服务已经注册成功了
SpringCloud_Eureka - 图4

服务发现

接下来我们修改 user-consumer,尝试从 eureka-service 获取服务提供者 user-service 的真实 IP 和端口信息。
添加 Eureka 客户端依赖

  1. <!-- Eureka客户端 -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>

修改配置,指定 eureka 地址

  1. server:
  2. port: 8080
  3. spring:
  4. application:
  5. name: consumer-service # 应用名称
  6. eureka:
  7. client:
  8. service-url: # EurekaServer地址
  9. defaultZone: http://127.0.0.1:10086/eureka

服务拉取,修改 UserController,使用 DiscoveryClient 类的方法,根据服务名称,获取服务实例

  1. package com.it.learn.controller;
  2. import com.it.learn.pojo.User;
  3. import org.springframework.cloud.client.discovery.DiscoveryClient;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.cloud.client.ServiceInstance;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import org.springframework.web.client.RestTemplate;
  10. import java.util.List;
  11. @RestController
  12. @RequestMapping("consumer")
  13. public class UserController {
  14. // 发送基于http协议的远程过程调用(2个服务器相互调用)
  15. @Autowired
  16. private RestTemplate restTemplate;
  17. @Autowired
  18. private DiscoveryClient discoveryClient;
  19. @RequestMapping("/{id}")
  20. public User findUserById(@PathVariable("id") Long id){
  21. // 从Eureka服务器中拉取提供服务的实例集合
  22. List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
  23. // 获取服务器提供者信息
  24. ServiceInstance serviceInstance = instances.get(0);
  25. // 从实例中获取 host 和 port,组成 url
  26. String url = String.format("http://%s:%s/user/%s", serviceInstance.getHost(), serviceInstance.getPort(), id);
  27. System.out.println(url); // http://localhost:8081/user/5
  28. // 查询
  29. User user = restTemplate.getForObject(url, User.class);
  30. return user;
  31. }
  32. }

启动测试,http://localhost:8080/consumer/5
SpringCloud_Eureka - 图5

基础架构

Eureka 架构中的3个核心角色

  1. 服务注册中心 —— Eureka 的服务端应用,提供服务注册和发现功能,就是刚刚我们建立的 eureka-service
  2. 服务提供者 —— 提供服务的应用,可以是 SpringBoot 应用,也可以是其它任意技术实现,只要对外提供的是Rest风格服务即可。本例中就是我们实现的 user-service
  3. 服务消费者 —— 消费应用从注册中心获取服务列表,从而得知每个服务方的信息,知道去哪里调用服务方。本例中就是我们实现的 user-consumer

    高可用的 Eureka Server

    服务的注册中心,在刚才的例子中,我们只有一个 eureka-service,事实上,eureka-service 也可以是一个集群,形成高可用的 Eureka 中心
    服务同步
    多个Eureka Server之间也会互相注册为服务,当服务提供者注册到Eureka Server集群中的某个节点时,该节点会把服务的信息同步给集群中的每个节点,从而实现高可用集群。因此,无论客户端访问到Eureka Server集群中的任意一个节点,都可以获取到完整的服务列表信息。
    而作为客户端,需要把信息注册到每个Eureka中:
    SpringCloud_Eureka - 图6
    搭建2条 eureka-service 的集群,端口分别为 10086 和 10087
    修改原来的 eureka-service 配置
    1. server:
    2. port: 10086
    3. spring:
    4. application:
    5. name: eureka-service
    6. eureka:
    7. client:
    8. service-url:
    9. defaultZone: http://127.0.0.1:10086/eureka,http://127.0.0.1:10087/eureka # 设置eureka服务器地址
    10. register-with-eureka: true # 不注册自己
    11. fetch-registry: true # 不拉取服务本eureka服务中心的服务信息
    重启 eureka-service
    再启动一台 eureka 服务
    因为 idea 中一个应用不能启动2次,我们需要重新配置一个启动器
    SpringCloud_Eureka - 图7
    需要保证2个服务端口不同,因此要配置 JVM 参数,覆盖配置文件中的端口
    1. -Dserver.port=10087
    最终,多个服务如下
    SpringCloud_Eureka - 图8
    启动,测试
    SpringCloud_Eureka - 图9
    客户端注册服务到集群
    因为现在 eureka-service 不止1个,因此 eureka 的客户端配置服务端地址的时候,service-url参数需要变化
    user-serviceuser-consumer 2个服务中修改 eureka 服务端地址
    1. eureka:
    2. client:
    3. service-url:
    4. defaultZone: http://127.0.0.1:10086/eureka,http://127.0.0.1:10087/eureka
    5. instance:
    6. ip-address: 127.0.0.1 # 配置服务器ip地址
    7. prefer-ip-address: true # 更倾向于使用ip,而不是host名
    8. instance-id: ${eureka.instance.prefer-ip-address}:${server.port} # 自定义实例的id
    重启 user-serviceuser-consumer 测试
    SpringCloud_Eureka - 图10
    项目demo:
    eureka-demo