Eureka,集群。CAP原则

Eureka服务端

依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    <version>2.1.6.RELEASE</version>
</dependency>
yml配置
server:
  port: 7001

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 # server:
  #  enable-self-preservation: false 关闭eureka自我保护机制,心跳机制,不推荐

启动类
@SpringBootApplication
@EnableEurekaServer//eureka 服务端注解,启动后,localhost:7001访问
public class EurekaServer_7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_7001.class, args);
    }
}

Eureka服务提供者

依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>1.4.6.RELEASE</version>
</dependency>

<!--eureka完善监控信息-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
yml配置
#Eureka配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: springcloud-provider-dept8001 #修改eureka上默认描述信息

#eureka上本服务信息,spring-boot-starter-actuator的作用 
info:
  app.name: springcloud-provider-dept8001
  company.name: com.moral
启动类
@SpringBootApplication
@EnableEurekaClient//eureka客户端注解、
@EnableDiscoveryClient//服务发现
public class DeptProvider_8001 {

    public static void main(String[] args) {
        SpringApplication.run(DeptProvider_8001.class, args);
    }
}
服务发现
import org.springframework.cloud.client.discovery.DiscoveryClient;//注意不要导错包

@Autowired
private DiscoveryClient discoveryClient;

@GetMapping("discovery")
public Object discovery() {
    //获取服务列表清单
    List<String> services = discoveryClient.getServices();
    System.out.println("services-->" + services);

    //获取具体微服务信息,通过服务id,application.name
    List<ServiceInstance> instances = discoveryClient.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
    for (ServiceInstance instance : instances) {
        System.out.println(instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri() + "\t" + instance.getInstanceId());
    }
    return discoveryClient;
}

Eureka集群

服务端

为了便于区分:修改本地hosts文件,eureka7001.com,eureka7002.com, eureka7003.com都映射成127.0.0.1

Eureka服务端7001

server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com
  client:
    register-with-eureka: true #表示是否将自己注册在EurekaServer上,默认为true
    fetch-registry: true 
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

Eureka服务端7002

server:
  port: 7002

eureka:
  instance:
    hostname: eureka7002.com
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/

 # server:
  #  enable-self-preservation: false 关闭eureka自我保护机制,心跳机制,不推荐

Eureka服务端7003

server:
  port: 7003

eureka:
  instance:
    hostname: eureka7003.com
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

 # server:
  #  enable-self-preservation: false 关闭eureka自我保护机制,心跳机制,不推荐
服务提供者
server:
  port: 8001

#mybatis配置
mybatis:
  type-aliases-package: com.moral.pojo
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

#spring配置
spring:
  application:
    name: springcloud-provider-dept

  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/db01?seUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

#Eureka配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: springcloud-provider-dept8001 #修改eureka上默认描述信息

#eureka上本服务信息
info:
  app.name: springcloud-provider-dept8001
  company.name: com.moral

CAP

C:一致性 A:可用性 P:容错性

一个分布式系统最多只能满足两点,由于分区容错P在分布式系统中必须保证,因此只能在a和c之间权衡

  • zookeeper CP
  • eureka AP
posted @ 2021-09-18 14:41  jpy  阅读(24)  评论(0)    收藏  举报