SpringCloud--03、Eureka注册中心

妖狐艹你老母 2022-04-12 13:48 399阅读 0赞

1、概述

  1. ureka就好比是滴滴,负责管理、记录服务提供者的信息。服务调用者无需自己寻找服务,
  2. 而是把自己的需求告诉Eureka,然后Eureka会把符合你需求的服务告诉你。
  3. 同时,服务提供方与Eureka之间通过“心跳”机制进行监控,当某个服务提供方出现问题,Eureka自然会把它从服务列表中剔除。

最终实现:服务的自动注册、发现、状态监控

2、原理图

" class="reference-link">watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3Nzd3F6eA_size_16_color_FFFFFF_t_70

  1. - Eureka:就是服务注册中心(可以是一个集群),对外暴露自己的地址
  2. - 服务提供者:启动后向Eureka注册自己信息(地址,提供什么服务)
  3. - 消费者:向Eureka订阅服务,Eureka会将对应服务的所有提供者地址列表发送给消费者,并且定期更新
  4. - 心跳(续约):提供者定期通过http方式向Eureka刷新自己的状态

#

3、入门案例

创建工程、

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3Nzd3F6eA_size_16_color_FFFFFF_t_70 1

导入依赖、pom.xml

  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
  5. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6. <parent>
  7. <artifactId>cloud-demo</artifactId>
  8. <groupId>com.baidus.demo</groupId>
  9. <version>1.0-SNAPSHOT</version>
  10. </parent>
  11. <modelVersion>4.0.0</modelVersion>
  12. <groupId>com.baidus.demo</groupId>
  13. <artifactId>eureka-server</artifactId>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.cloud</groupId>
  17. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  18. </dependency>
  19. </dependencies>
  20. </project>

启动类、EurekaDemoApp.java

  1. package com.baidus.eureka.domain;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. /**
  6. * @ Author :ShaoWei Sun.
  7. * @ Date :Created in 17:42 2018/12/1
  8. */
  9. @SpringBootApplication
  10. @EnableEurekaServer
  11. public class EurekaDemoApp {
  12. public static void main(String[] args) {
  13. SpringApplication.run(EurekaDemoApp.class,args);
  14. }
  15. }

全局配置 applicaton.yml

  1. server:
  2. port: 10086 # 端口
  3. spring:
  4. application:
  5. name: eureka-server # 应用名称,会在Eureka中显示
  6. eureka:
  7. client:
  8. register-with-eureka: false # 是否注册自己的信息到EurekaServer,默认是true
  9. fetch-registry: false # 是否拉取其它服务的信息,默认是true
  10. service-url: # EurekaServer的地址,现在是自己的地址,如果是集群,需要加上其它Server的地址。
  11. defaultZone: http://127.0.0.1:${server.port}/eureka

4、将user-service注册到Eureka

  1. 注册服务,就是在服务上添加Eureka的客户端依赖,客户端代码会自动把服务注册到EurekaServer中。

在user-service中添加依赖

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

在user-service启动类添加@EnableDiscoveryClient 、开启Eureka客户端功能

  1. package com.baidus.user;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import tk.mybatis.spring.annotation.MapperScan;
  6. /**
  7. * @ Author :ShaoWei Sun.
  8. * @ Date :Created in 16:08 2018/12/1
  9. */
  10. @SpringBootApplication
  11. @MapperScan("com.baidus.user.mapper")
  12. @EnableDiscoveryClient //开启EurekaClient功能
  13. public class UserApp {
  14. public static void main(String[] args) {
  15. SpringApplication.run(UserApp.class,args);
  16. }
  17. }

application.yml配置文件

  1. server:
  2. port: 8081
  3. spring:
  4. datasource:
  5. url: jdbc:mysql://localhost:3306/mybatis
  6. username: root
  7. password: sswqzx
  8. application:
  9. name: user-service # 应用名称
  10. mybatis:
  11. type-aliases-package: com.baidus.user.pojo
  12. eureka:
  13. client:
  14. service-url: # EurekaServer地址
  15. defaultZone: http://127.0.0.1:10087/eureka
  16. instance:
  17. prefer-ip-address: true # 当调用getHostname获取实例的hostname时,返回ip而不是host名称
  18. ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找
  19. lease-expiration-duration-in-seconds: 10 #服务续约(renew)的间隔,默认为30秒
  20. lease-renewal-interval-in-seconds: 5 #服务失效时间,默认值90秒

重启:访问:http://localhost:10087/

5、消费者从Eureka获取服务

修改consumer-demo

添加依赖

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

启动类

  1. @SpringBootApplication
  2. @EnableDiscoveryClient // 开启Eureka客户端
  3. public class UserConsumerDemoApplication {
  4. @Bean
  5. public RestTemplate restTemplate() {
  6. return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
  7. }
  8. public static void main(String[] args) {
  9. SpringApplication.run(UserConsumerDemoApplication.class, args);
  10. }
  11. }

配置文件application.yml

  1. server:
  2. port: 8080
  3. spring:
  4. application:
  5. name: consumer # 应用名称
  6. eureka:
  7. client:
  8. service-url: # EurekaServer地址
  9. defaultZone: http://127.0.0.1:10087/eureka
  10. instance:
  11. prefer-ip-address: true # 当其它服务获取地址时提供ip而不是hostname
  12. ip-address: 127.0.0.1 # 指定自己的ip信息,不指定的话会自己寻找

service.java代码

  1. @Service
  2. public class UserService {
  3. @Autowired
  4. private RestTemplate restTemplate;
  5. @Autowired
  6. private DiscoveryClient discoveryClient;// Eureka客户端,可以获取到服务实例信息
  7. //如果用了robbin负载就有了负载均衡算法、轮询和随机
  8. public List<User> queryUserByIds(List<Long> ids) {
  9. List<User> users = new ArrayList<>();
  10. // String baseUrl = "http://localhost:8081/user/";
  11. // 根据服务名称,获取服务实例
  12. List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
  13. // 因为只有一个UserService,因此我们直接get(0)获取
  14. ServiceInstance instance = instances.get(0);
  15. // 获取ip和端口信息
  16. String baseUrl = "http://"+instance.getHost() + ":" + instance.getPort()+"/user/";
  17. ids.forEach(id -> {
  18. // 我们测试多次查询,
  19. users.add(this.restTemplate.getForObject(baseUrl + id, User.class));
  20. // 每次间隔500毫秒
  21. try {
  22. Thread.sleep(500);
  23. } catch (InterruptedException e) {
  24. e.printStackTrace();
  25. }
  26. });
  27. return users;
  28. }
  29. }

重启访问:http://localhost/8081/consume?ids=1,2,3

发表评论

表情:
评论列表 (有 0 条评论,399人围观)

还没有评论,来说两句吧...

相关阅读