1、启动【服务中心】集群,即 Eureka Server
2、启动【服务提供者】集群,即 Eureka Client
3、创建【服务消费者】,即 Eureka Discovery Client
3.1、新建 Spring Boot 工程,工程名称 springcloud-eureka-feign
3.2、工程pom.xml文件添加如下依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
3.3、在工程启动类中,添加注解 @EnableDiscoveryClient,@EnableFeignClients
package com.miniooc.eurekafeign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* EurekaFeignApplication
* 应用程序启动类,程序入口
*
* @author 宋陆
* @version 1.0.0
*/
@EnableDiscoveryClient // 启用 Eureka 服务发现
@EnableFeignClients // 启用 Feign
@SpringBootApplication
public class EurekaFeignApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaFeignApplication.class, args);
}
}
3.4、创建【服务消费者】服务类 EurekaFeignService
package com.miniooc.eurekafeign.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* EurekaFeignService
* 服务消费者,调用服务提供者提供的服务,实现业务
*
* @author 宋陆
* @version 1.0.0
*/
@FeignClient(value = "eureka-client") // 调用的服务的名称
public interface EurekaFeignService {
@RequestMapping(value = "/info")
String getInfo();
}
3.5、创建【服务消费者】控制器类 EurekaFeignController
package com.miniooc.eurekafeign.controller;
import com.miniooc.eurekafeign.service.EurekaFeignService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* EurekaFeignController
* 服务消费控制器类,对用户提供服务
*
* @author 宋陆
* @version 1.0.0
*/
@RestController
public class EurekaFeignController {
@Resource
private EurekaFeignService eurekaFeignService;
@RequestMapping("/feignInfo")
public String feignInfo() {
String message = eurekaFeignService.getInfo();
return "获取到的信息:" + message;
}
}
3.6、创建工程配置文件application.yml,添加如下配置:
server: port: 52620 spring: application: name: eureka-discovery-feign eureka: instance: hostname: localhost # 表示eureka client间隔多久去拉取服务注册信息,默认为30秒,如果要迅速获取服务注册状态,可以缩小该值 lease-renewal-interval-in-seconds: 5 # 表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance。 # 默认为90秒 # 如果该值太大,则很可能将流量转发过去的时候,该instance已经不存活了。 # 如果该值设置太小了,则instance则很可能因为临时的网络抖动而被摘除掉。 # 该值至少应该大于 leaseRenewalIntervalInSeconds lease-expiration-duration-in-seconds: 10 client: serviceUrl: defaultZone: http://localhost:9527/eureka/,http://localhost:9528/eureka/,http://localhost:9529/eureka/
3.7、启动【服务消费者】工程
3.8、打开浏览器,访问【服务中心】前台界面 http://localhost:9527,http://localhost:9528,http://localhost:9529
红框处,可以看到服务消费实例已经注册到了服务注册中心,服务名 EUREKA-DISCOVERY-FEIGN
3.9、开浏览器窗口,访问 http://localhost:52620/feignInfo,多次刷新该地址
红框处,可以看到【服务消费者】成功调用了【服务提供者】提供的服务,并实现了负载均衡,轮询请求不同的【服务提供者】。
至此,一个简单的单点【服务消费者】搭建完成。