6、EurekaServer部署以及8001项目和80项目注册进EurekaServer

image.png

把Eureka Server当做注册中心,ServierConsumer和ServiceProvider对于EurekaServer来说都是Client

1、创建Module名为cloud-eureka-server7001

首先创建一个module名为cloud-eureka-server7001
在cloud-eureka-server7001配置pom.xml


        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
        
        
            com.kari.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
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

2、配置cloud-eureka-server7001的配置信息

编写cloud-eureka-server7001的application.xml

server:
  port: 7001

eureka:
  instance:
    hostname: localhost #eureka服务端的实例名称

  client:
    register-with-eureka: false  #false表示不向注册中心注册自己
    fetch-registry: false  #false自己就是注册中心
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka  #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址

register-with-eureka: false表示不向注册中心注册自己
fetch-registry: false 自己就是注册中心
service-url: defaultZone: 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址

然后在主启动类加入@EnableEurekaServer注解 使Eureka生效

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

3、将8001项目注册进7001server

此时8001项目对于7001来说是一个client
首先在8001项目的pom.xml中加入Eureka Client的依赖



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

然后在8001项目的application中加入注册信息

eureka:
  client:
    register-with-eureka: true  #表示是否将自己注册进EurekaServer默认为True
    fetch-registry: true   #是否愿意从Eureka抓取字的注册信息,默认为true.单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    service-url:
      defaultZone: http://localhost:7001/eureka

表示注册到http://localhost:7001/eureka
并且在主启动类加入注解@EnableEurekaClient

4、将80项目注册进7001server

此时80项目对于7001来说是一个client
首先在80项目的pom.xml中加入Eureka Client的依赖



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

然后在80项目的application中加入注册信息

eureka:
  client:
    register-with-eureka: true  #表示是否将自己注册进EurekaServer默认为True
    fetch-registry: true   #是否愿意从Eureka抓取字的注册信息,默认为true.单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    service-url:
      defaultZone: http://localhost:7001/eureka

并且配置application name

spring:
  application:
    name: cloud-order-service

表示注册到http://localhost:7001/eureka

5、依次启动7001、8001、70项目

依次启动7001、8001、70项目
首先访问http://localhost:7001/访问注册中心
发现服务已经被注册进来

image.png

你可能感兴趣的:(6、EurekaServer部署以及8001项目和80项目注册进EurekaServer)