SpringCloud 服务治理:Eureka
- Eureka 介绍
- Eureka 入门案例- 父工程
- Eureka Server- pom.xml
- 启动类
- application.yml
- Eureka 控制台
 
- 服务提供者- pom.xml
 
- 启动类
- application.yml
- domain
- controller
- service
- dao
 
- 服务调用者- pom.xml
- 启动类
- application.yml
- RestTemplateConfig
- domain
- controller
 
 
- Eureka 相关配置及特性- instance 相关属性
- server 相关属性
 
- Eureka 高可用- Eureka Server 搭建- eureka-server-1
- eureka-server-2
 
- Eureka Client 配置
 
- Eureka Server 搭建
Eureka 介绍
Eureka 是 Netflix 公司开源的一个服务注册与发现的组件。
? Eureka 和其他 Netflix 公司的服务组件(例如负载均衡、熔断器、网关等) 一起,被 SpringCloud 社区整合为 Spring-Cloud-Netflix 模块。
? Eureka 包含两个组件:Eureka Server(注册中心)和 Eureka Client(服务提供者、服务消费者)。
Eureka 入门案例
搭建步骤:
- 搭建 Provider 和 Consumer 服务。
- 使用 RestTemplate 完成远程调用。
- 搭建 Eureka Server 服务。
- 改造 Provider 和 Consumer,成为 Eureka Client。
- Consumer 服务通过从 Eureka Server 中抓取 Provider 地址,完成远程调用。
父工程
<?xml version="1.0" encoding="UTF-8"?>
    4.0.0 
    org.example 
    eureka-parent 
    pom 
    1.0-SNAPSHOT 
    
        eureka-provider 
        eureka-consumer 
        eureka-server 
     
    
        
        org.springframework.boot 
        spring-boot-starter-parent 
        2.1.0.RELEASE 
     
    
        
        
            org.projectlombok 
            lombok 
            true 
         
     
    
    
        
            
                org.springframework.cloud 
                spring-cloud-dependencies 
                ${spring-cloud.version} 
                pom 
                import 
             
         
     
    
        UTF-8 
        UTF-8 
        1.8 
        
        Greenwich.RELEASE 
     
 
Eureka Server
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
    
        eureka-parent 
        org.example 
        1.0-SNAPSHOT 
     
    4.0.0 
    eureka-server 
    
        
            org.springframework.boot 
            spring-boot-starter-web 
         
        
        
            org.springframework.cloud 
            spring-cloud-starter-netflix-eureka-server 
         
     
    
        8 
        8 
     
 
启动类
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer  // 启用 EurekaServer(不声明也是默认启动)
public class EurekaApp {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApp.class, args);
    }
}
application.yml
server:
  port: 8761
# eureka 配置:共分为四部分配置
# 1. dashboard: eureka 的 web 控制台配置
# 2. server:    eureka 的服务端配置
# 3. client:    eureka 的客户端配置
# 4. instance:  eureka 的实例配置
eureka:
  instance:
    hostname: localhost # 主机名
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka  # eureka 服务端地址,将来客户端使用该地址和 eureka 进行通信
    register-with-eureka: false  # 是否将自己的路径注册到 eureka 上(eureka server 需要;eureka provider client 不需要)。默认:true
    fetch-registry: false  # 是否需要从 eureka 中抓取路径(eureka server 不需要;eureka consumer client 需要)。默认:true
Eureka 控制台
运行启动类后,访问 localhost:8761 即可进入Eureka 控制台:
- System status:系统状态信息
- DS Replicas:集群信息
- tance currently registered with Eureka:实例注册信息
- General Info:通用信息
- Instance Info:实例信息
服务提供者
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
    
        eureka-parent 
        org.example 
        1.0-SNAPSHOT 
     
    4.0.0 
    eureka-provider 
    
        
        
            org.springframework.boot 
            spring-boot-starter-web 
         
        
        
            org.springframework.cloud 
            spring-cloud-starter-netflix-eureka-client 
         
     
 
启动类
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
 * 启动类
 */
@EnableEurekaClient  // 该注解在SpringCloud新版本中可以省略
@SpringBootApplication
public class ProviderApp {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApp.class, args);
    }
}
application.yml
server:
  port: 8000
eureka:
  instance:
    hostname: localhost  # 主机名
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka  # eureka 服务端地址,将来客户端使用该地址和 eureka 进行通信
spring:
  application:
    name: eureka-provider  # 设置当前应用的名称。这会在 eureka 控制台中的 Application 显示,且需要使用该名称来获取路径
domain
package com.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Goods {
    private int id;
    private String name;
    private double price;
    private int skuNum;
    @Override
    public String toString() {
        return "Goods{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", skuNum=" + skuNum +
                '}';
    }
}
controller
package com.controller;
import com.domain.Goods;
import com.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @GetMapping("findOne/{id}")
    public Goods findGoods(@PathVariable("id") int id){
        return goodsService.findOne(id);
    }
}
service
package com.service;
import com.dao.GoodsDao;
import com.domain.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class GoodsService {
    @Autowired
    private GoodsDao goodsDao;
    public Goods findOne(int id){
        return goodsDao.findOne(id);
    }
}
dao
package com.dao;
import com.domain.Goods;
import org.springframework.stereotype.Repository;
@Repository
public class GoodsDao {
    public static Goods findOne(int id){
        Goods phone = new Goods(1, "华为P10", 6999.00, 20);
        return phone;
    }
}
服务调用者
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
    
        eureka-parent 
        org.example 
        1.0-SNAPSHOT 
     
    4.0.0 
    eureka-consumer 
    
        
        
            org.springframework.boot 
            spring-boot-starter-web 
         
        
        
            org.springframework.cloud 
            spring-cloud-starter-netflix-eureka-client 
         
     
 
启动类
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableDiscoveryClient  // 激活 DiscoveryClient
@EnableEurekaClient
@SpringBootApplication
public class ConsumerApp {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApp.class, args);
    }
}
application.yml
server:
  port: 8001
eureka:
  instance:
    hostname: localhost  # 主机名
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka  # eureka服务端地址,将来客户端使用该地址和eureka进行通信
spring:
  application:
    name: eureka-consumer  # 设置当前应用的名称。这会在eureka中Application显示,且需要使用该名称来获取路径
RestTemplateConfig
- RestTemplateConfig 是 Spring 提供的一种简单便捷的模板类,用于在 java 代码里访问 restful 服务。
 ? 其功能与 HttpClient 类似,但是 RestTemplate 实现更优雅,使用更方便。
package com.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
domain
package com.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Goods {
    private int id;
    private String name;
    private double price;
    private int skuNum;
    @Override
    public String toString() {
        return "Goods{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", skuNum=" + skuNum +
                '}';
    }
}
controller
package com.controller;
import com.domain.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
 * 服务调用方
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    @GetMapping("/goods/{id}")
    public Goods findOrderByGoodsId(@PathVariable("id") int id) {
        /*
            动态从 Eureka Server 中获取 provider 的 ip 和端口
             1. 注入 DiscoveryClient 对象.激活
             2. 调用方法
         */
        // 演示 discoveryClient 使用
        List instances = discoveryClient.getInstances("EUREKA-PROVIDER");  // 服务提供者所配置的实例名称(不区分大小写)
        // 判断集合是否有数据
        if(instances == null || instances.size() == 0){
            // 集合没有数据
            return null;
        }
        ServiceInstance instance = instances.get(0);
        String host = instance.getHost();  // 获取ip
        int port = instance.getPort();  // 获取端口
        /*
            远程调用 Goods 服务中的 findOne 接口:使用 RestTemplate
            1. 定义Bean  restTemplate
            2. 注入Bean
            3. 调用方法
         */
        String url = String.format("http://%s:%d/goods/findOne/%d", host, port, id);
        Goods goods = restTemplate.getForObject(url, Goods.class);
        return goods;
    }
}
 运行效果:
访问 http://localhost:8001/order/goods/1 ,返回:{"id":1,"name":"华为P10","price":6999.0,"skuNum":20}
Eureka 相关配置及特性
instance 相关属性
- 
Eureka Instance 的配置信息全部保存在org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean 配置类里,实际上它是 com.netflix.appinfo.EurekaInstanceConfig 的实现类,替代了 netflix 的 com.netflix.appinfo.CloudInstanceConfig 的默认实现。 
- 
Eureka Instance 的配置信息全部以 eureka.instance.xxx的格式配置。
常用配置:
server 相关属性
- 
Eureka Server 注册中心端的配置是对注册中心的特性配置。Eureka Server 的配置全部在 org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean 里,实际上它是 com.netflix.eureka.EurekaServerConfig 的实现类,替代了 netflix 的默认实现。 
- 
Eureka Server 的配置全部以 eureka.server.xxx的格式进行配置。
常用配置:
注意:上述配置一般在生产环境保持默认即可(生产环境配置原则是尽量减少环境变化),在开发或测试则可以为了方便而修改配置。
Eureka 高可用
搭建示例:
- 搭建两个 Eureka Server
- 分别进行配置:相互注册
- Eureka Client 分别注册到这两个 Eureka Server 中
Eureka Server 搭建
修改本地 host 文件:
eureka-server-1
application.yml:
server:
  port: 8761
eureka:
  instance:
    hostname: eureka-server1  # 主机名
  client:
    service-url:
      defaultZone: http://eureka-server2:8762/eureka
    register-with-eureka: true  # 是否将自己的路径注册到 eureka 上
    fetch-registry: true  # 是否需要从 eureka 中抓取路径
spring:
  application:
    name: eureka-server-ha
eureka-server-2
application.yml:
server:
  port: 8762
eureka:
  instance:
    hostname: eureka-server2  # 主机名
  client:
    service-url:
      defaultZone: http://eureka-server1:8761/eureka
    register-with-eureka: true  # 是否将自己的路径 注册到 eureka 上
    fetch-registry: true  # 是否需要从 eureka 中抓取路径
spring:
  application:
    name: eureka-server-ha
Eureka Client 配置
分别修改服务提供者和服务消费者配置文件中的注册服务地址:
...
eureka:
  client:
    service-url:
      defaultZone: http://eureka-server1:8761/eureka,http://eureka-server2:8762/eureka  # eureka 服务端地址
...
干掉其中一台 server,验证 client 仍能正常访问。