3.Eureka服务注册与发现(2)- 单机Eureka构建

1. IDEA生成eurekaServer端服务注册中心类似物业公司

cloud-eureka-server7001

1. pom



    
        cloud2020
        com.atguigu.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    cloud-eureka-server7001

    
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        

        
            com.atguigu.springcloud
            cloud-api-commons
            ${project.version}
        

        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

        
        
            org.projectlombok
            lombok
        

        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            junit
            junit
        

    

2. yml

server:
  port: 7001

eureka:
  instance:
    hostname: localhost  #eureka服务端的实例名字
  client:
    register-with-eureka: false    #表识不向注册中心注册自己
    fetch-registry: false   #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
     service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/    #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址

3. 主启动

@EnableEurekaServer

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

4. 测试

http://localhost:7001/

image.png

2. EurekaClient端cloud-provider-payment8001将注册进EurekaServer成为服务提供者provider

1.pom

 
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

2.yml

eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

3.主启动

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class PaymentMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class,args);
    }
}

4.测试

  • 先要启动EurekaServer
  • http://localhost:7001/
image.png

自我保护机制

image.png

3. EurekaClient端cloud-consumer-order80将注册进EurekaServer成为服务消费者consumer

1. pom

 
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

2.yml

spring:
  application:
    name: cloud-order-service

eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

3.主启动

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class PaymentMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class,args);
    }
}

4.测试

  • 先要启动EurekaServer,7001服务
  • 再要启动服务提供者provider,8001服务
  • eureka服务器
image.png
  • http://localhost/consumer/payment/get/31

你可能感兴趣的:(3.Eureka服务注册与发现(2)- 单机Eureka构建)