Eureka
1 简介
Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers.
Eureka是一个基于REST的服务,主要用于AWS云中的服务定位,以实现负载均衡和中间层服务的故障转移。
Eureka提供了完整的Service Registry和Service Discovery实现,并且也经受住了Netflix自己的生产环境考验,相对使用起来会比较省心(同时Spring Cloud还有一套非常完善的开源代码来整合Eureka,所以使用起来非常方便)。
-
Eureka Server:提供服务注册和发现,多个Eureka Server之间会同步数据,做到状态一致(最终一致性)
-
Service Provider:服务提供方,将自身服务注册到Eureka,从而使服务消费方能够找到
-
Service Consumer:服务消费方,从Eureka获取注册服务列表,从而能够消费服务
2 server-注册中心 standalone-mode
提供服务注册和发现
2.1 IDEA使用Spring Initializr创建项目
2.2 启动类添加注解
@SpringBootApplication
@EnableEurekaServer //开启Eureka注册中心
public class EurekaRegistryApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaRegistryApplication.class, args);
}
}
2.3 配置文件配置
server:
port: 8761 #指定端口号
eureka:
instance:
hostname: localhost #指定示例主机名称
client:
fetch-registry: false #默认true,表示是否将自己注册到Eureka Server
register-with-eureka: false #默认true,表示是否从Eureka Server获取注册的服务信息
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2.4 启动服务
访问注册中心
看到上图,配置成功。
3 client-客户端
将自身服务注册到Eureka,服务消费者、服务提供者均为client
3.1 IDEA使用Spring Initializr创建项目
3.2 配置文件配置
server:
port: 8000
spring:
application:
name: service-client
eureka:
instance:
hostname: localhost
client:
fetch-registry: true
register-with-eureka: true
serviceUrl:
defaultZone: http://root:root@${eureka.instance.hostname}:8761/eureka/ #配置注册中心地址
3.3 启动服务
上图即注册成功
4 eureka集群配置
创建两个eureka服务端,与第2节类似。区别在使用不同配置
配置文件-server1:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:8762/eureka/
spring:
application:
name: eureka-registry
config:
activate:
on-profile: registry1
配置文件-server2:
server:
port: 8762
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
spring:
application:
name: eureka-registry
config:
activate:
on-profile: registry2
启动服务
上图即完成集群搭建
5 集成feign调用服务
5.1 服务提供者
pojo
import lombok.Data;
@Data
public class User {
private Integer id;
private String name;
private Integer age;
}
controller
import com.cuixk.microservice.eureka.pojo.User;
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("/user")
public class UserController {
@GetMapping("/{id}")
public User getUserInfo(@PathVariable Integer id) {
User user = new User();
user.setId(id);
user.setName("provider");
user.setAge(25);
return user;
}
}
配置文件
server:
port: 8000
spring:
application:
name: service-client
eureka:
instance:
hostname: localhost
client:
fetch-registry: true
register-with-eureka: true
serviceUrl:
defaultZone: http://root:root@${eureka.instance.hostname}:8761/eureka/
5.2 服务消费者
引入open-feign依赖
<!--服务调用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
编写服务调用 UserFeign
import com.cuixk.microservice.eureka.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient("service-client") //注册中心application
public interface UserFeign {
@GetMapping("/user/{id}")
User getUserInfo(@PathVariable Integer id);
}
编写controller
import com.cuixk.microservice.eureka.entity.User;
import com.cuixk.microservice.eureka.feign.UserFeign;
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 javax.annotation.Resource;
@RestController
@RequestMapping("/get")
public class OrderController {
@Resource
private UserFeign userFeign;
@GetMapping("/{id}")
public String getUserInfo(@PathVariable Integer id) {
User userInfo = userFeign.getUserInfo(id);
return userInfo.toString();
}
}
配置文件
server:
port: 8001
spring:
application:
name: service-server
eureka:
instance:
hostname: localhost
client:
fetch-registry: true
register-with-eureka: true
serviceUrl:
defaultZone: http://root:root@${eureka.instance.hostname}:8761/eureka/
5.3 测试
启动服务注册中心、服务提供者、服务消费者;
调用服务消费者OrderController;
服务调通有返回,则完成配置
6 参考链接
https://docs.spring.io/spring-cloud-netflix/docs/2.2.6.RELEASE/reference/html/
https://zhuanlan.zhihu.com/p/88385121