Spring Cloud学习笔记:Eureka简介,Eureka简单样例

这是本人学习的总结,主要学习资料如下
- 马士兵教育

1、Eureka

1.1、架构

EurekaSpringCloud Nexflix的核心子模块,其中包含ServerClient

Server提供服务注册,存储所有可用服务节点。

Client用于简化和Server的通讯复杂度。

下面是Eureka的简单架构图

在这里插入图片描述

每一个服务节点需要在Eureka Server中注册,如果需要其他节点的服务,则需要远程调用Service ProviderProvider会访问Server,由Server找到一个合适的节点提供服务给cumsumer



1.2、核心特性

  1. 服务注册:这是最核心的功能,其余的特性都是对这个功能的加强。
  2. 服务续约:Client每隔30s就会向Server发送一次心跳来续约,超过90s没有续约就会被Server删除这个服务节点。
  3. 服务下线:Client可以主动向Server发送cancel命令优雅下线。
  4. 缓存注册列表:Client会缓存从Server获取的注册列表,并且每30s更新一次。

2、建立Spring Cloud项目

2.1、项目结构和父项目依赖

接下来就是代码展示如何配置启动serverclient,以及client之间获取信息。

这是项目结构,两个子module,分别是serverorder-clientuser-clientserver提供注册服务,另外两个作为client则是到server注册然后互相调用对方的服务。

在这里插入图片描述

这是根目录的dependency

<properties>
    <java.version>1.8</java.version>
    <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <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>

2.2、启动Server

2.2.1、dependency

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

2.2.2、配置文件

resources/application.yml

spring:
  application:
    name: msb-eureka-server
server:
  port: 8761

eureka:
  instance:
    #注册实例名称
    hostname: localhost
    #是否将自己的ip注册到eureka中,默认false 注册 主机名
    prefer-ip-address: true
    # Eureka客户端需要多长时间发送心跳给Eureka,表明他仍然或者,默认是30
    # 通过下面方式我们可以设置,默认单位是秒
    lease-renewal-interval-in-seconds: 10
    # Eurkea服务器在接受到实例最后一次发送的心跳后,需要等待多久可以将次实例删除
    # 默认值是90
    # 通过下面方式我们可以设置,默认单位是秒
    lease-expiration-duration-in-seconds: 30
  client:
    #是否注册到eureka服务中
    register-with-eureka: false
    #是否拉取其他服务
    fetch-registry: false
    

2.2.3、Server端启动代码

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

启动以后打开网页检查。localhost:8761
请添加图片描述


2.3、启动Client

2.3.1、dependency

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

2.3.2、配置文件

order-client和```user-client````都一样,因为是单机模拟所以监听的端口号不同。

# 节点在server中注册的名字
spring:
  application:
  	# user-client则用user-client
    name: order-client
server:
# order-client 监听9002, user-client监听9003
  port: 9002

  
eureka:
  client:
  # 这个一定要配对,server地址后面默认要加一个上下文eureka
    service-url:
      defaultZone: http://localhost:8761/eureka


management:
  endpoints:
    web:
      exposure:
        include: shutdown #暴露shutdown端点
  endpoint:
    shutdown:
      enabled: true #再次确认暴露shutdown端点

feign:
  tokenId: 11111111111111111111

2.3.3、Client端启动代码

注意有两个注解可以将其标注为Client,分别是@EnableDiscoveryClient@EnableEurekaClient

这里推荐使用@EnableDiscoveryClient,因为后者是netfliex提供的,如果使用后者,后期要更换其它注册中心就需要更换注解,比较麻烦。

这是order-client的代码

@EnableDiscoveryClient // 这是官方提供的  ,我们以后可能切换其他的注册中心比如说nacos,那我们就直接切换就行了
//@EnableEurekaClient  // 是netflix提供的,如果用这个注解就只能服务于eureka
@SpringBootApplication
public class EurekaOrderClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaOrderClientApplication.class);
    }
}

这时user-client的代码

@EnableDiscoveryClient // 这是官方提供的  ,我们以后可能切换其他的注册中心比如说nacos,那我们就直接切换就行了
//@EnableEurekaClient  // 是netflix提供的,如果用这个注解就只能服务于eureka
@SpringBootApplication
public class EurekaUserClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaUserClientApplication.class);
    }
}

到Server的页面查看,两个Client都注册成功。
在这里插入图片描述

2.3.4、提供RPC服务

因为RPC是基于HTTP实现的协议,所以我们提供RPC服务时就像写一个controller的服务一样。

这里设定order模块会调用user提供的服务。

@Slf4j
@RestController
public class UserController {
    @RequestMapping("/getUserInfo")
    public String getUser(String userId) {
        log.info(userId);	
        return "userInfo: {userId: "+ userId +"}";;
    }
}

接下来就看order模块如何通过Eureka调用user提供的服务。

2.4、服务之间获取信息

引入LoadBalancerClient,从这个bean中可以获得其他注册的client元数据,比如地址,端口号等。

获取到这些信息后就可以组成请求地址,然后获取数据。

下面这个例子展示了如何获取其他client的元信息并且调用其它client的服务。

@Service
public class OrderService {
	@Autowired
    private LoadBalancerClient eurekaClient;
    @Autowired
    private RestTemplate restTemplate;
	
	public void getUser() {
		ServiceInstance instance = eurekaClient.choose("msb-user");
		String hostname = instance.getHost();
        int port = instance.getPort();
        String uri = "/getUserInfo?userId=" + userId;
        String url = "http://" + hostname + ":" + port + uri;
        return restTemplate.getForObject(url, String.class);
	}
	
}

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-04-15 07:36:01       17 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-15 07:36:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-15 07:36:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-15 07:36:01       18 阅读

热门阅读

  1. excel表导入导出

    2024-04-15 07:36:01       17 阅读
  2. Linux kernel 墙上时间

    2024-04-15 07:36:01       29 阅读
  3. Spark开窗函数之ROW

    2024-04-15 07:36:01       14 阅读
  4. C#去掉字符串中所有匹配的字符String.Replace方法

    2024-04-15 07:36:01       15 阅读
  5. 最短路计数

    2024-04-15 07:36:01       48 阅读