Eureka--springClould笔记一:

守給你的承諾、 提交于 2020-08-10 09:26:38

单机版注册中心:

1、Eureka服务端
pom
<!--eureka-server-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

yml:
    server:
      port: 7001

    eureka:
      instance:
        hostname: localhost #eureka服务端的实例名字
      client:
        allow-redirects: false # 是否向注册中心注册本服务:true为是。注册中心不需要将自己注册进去
        fetch-registry: false # false表示自己是注册中心,职责是维护服务实例,并不需要去检索服务
        service-url:
          #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址,多个时用逗号隔开
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

主启动类 :
 @EnableEurekaServer
 @SpringBootApplication
 @EnableEurekaServer //服务端:注册中心
 public class EurekaApplication7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication7001.class,args);
    }
}

 

2、EurekaClient端的--服务提供者端:
<!--eureka-client-->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>

server:
  port: 8001
spring:
  application:
    name: cloud-payment-service # 服务名称
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456789
eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

主启动类添加注解:@EnableEurekaClient

 

3、EurekaClient端的--服务调用者端:
80Controller层
@Resource
public RestTemplate restTemplate;
public static final String PAY_URL="http://localhost:8001";
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
    return restTemplate.getForObject(PAY_URL+"/payment/get/"+id,CommonResult.class);
}

集群版注册中心:

搭建Eureka注册中心集群,实现负载均衡+故障容错

 

为了模拟实际情况,所以修改下hosts文件
C:\Windows\System32\drivers\etc下找到hosts文件添加以下内容
# test-eureka
127.0.0.1   eureka7001.com
127.0.0.1   eureka7002.com
1、Eureka服务端
server:
  port: 7001
eureka:
   instance:
        hostname: eureka7001.com    #eureka服务端的实例名字
   client:
        register-with-eureka: false    #不向注册中心注册自己
        fetch-registry: false   #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
        service-url:
          defaultZone: http://eureka7002.com:7002/eureka/    #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
如上是7001注册到7002上
同样7002 注册到7001上
server:
  port: 7002
eureka:
  instance:
    hostname: eureka7002.com
  client:
    #不注册自己
    register-with-eureka: false
    #自己是注册中心,不需要去检索服务
    fetch-registry: false
    service-url:
      #设置与Eureka Server 交互的地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://eureka7001.com:7001/eureka/
2、EurekaClient端的--服务提供者Service端:
server:
  port: 8001
 
spring:
  application:
    name: cloud-payment-service # 服务名称
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password:*********
 
eureka:
  client:
    #注册自己
    register-with-eureka: true
    #需要去检索服务
    fetch-registry: true
    service-url:
      #设置与Eureka Server 交互的地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/
  instance:
    instance-id: payment8001  # 把默认的localhost:name:端口名,修改为payment8001
    prefer-ip-address: true   #访问路径下鼠标悬停显示IP地址
 
 
mybatis:
  type-aliases-package: com.atguigu.springcloud.entities
  mapper-locations: classpath:mapper/*.xml
如图:现有两个服务端:8001;8002 ,服务名字都是CLOUD_PAYMENT_SERVICE
3、EurekaClient端的--服务调用者端:
 
server:
  port: 80
 
spring:
  application:
    name: cloud-consumer-order # 服务名称
 
eureka:
  client:
    #注册自己
    register-with-eureka: true
    #需要去检索服务
    fetch-registry: true
    service-url:
      #设置与Eureka Server 交互的地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/
80Controller层
// 直接服务提供方的服务的名字
public static final String PAY_URL="http://CLOUD-PAYMENT-SERVICE";
@Resource
public RestTemplate restTemplate;
 
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
    return restTemplate.getForObject(PAY_URL+"/payment/get/"+id,CommonResult.class);
}
RestTemplate: 是有一种优雅的HTTP请求方式---参考二
自我 保护机制:
某时刻某一个微服务不可用了,Eureka不会立刻清理,依旧会对该微服务的信息进行保存
属于CAP里面的AP分支
2.禁止自我保护:(一般生产环境中不会禁止自我保护)
 
7001修改:
1)出厂默认,自我保护机制是开启的
    eureka.server.enable-self-preservation = true
 
2)使用eureka.server.enable-self-preservation = false可以禁用自我保护模式
 
    server:
      enable-self-preservation: false # 禁用自我保护模式
      eviction-interval-timer-in-ms: 2000 # 2秒
 
8001修改:
1)添加 eureka.instance.lease-renewal-interval-in-seconds和lease-expiration-duration-in-seconds
 
   # eureka客户端想服务端发动心跳的时间间隔,单位为秒(默认是30秒)。开发的时候可以设置小一些,以保证服务关闭后注册中心及时剔除服务
    lease-renewal-interval-in-seconds: 1
    # eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒)。开发时候设置小一些
    lease-expiration-duration-in-seconds: 2
 
 
 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!