5分钟搭建SpringCloud Eureka服务注册中心的实现

创建父级项目 只需保留pom.xml文件

这里只需搭建一个微服务 其他操作并无




4.0.0

  com.tyy.springcloud
  cloudstudy
  pom
  1.0-SNAPSHOT
  
  
  	
   	cloud-provider-8001
   	
   	cloud-eureka-server9001
  
   
   
  
  	
		UTF-8
    1.8
    1.8
	
	 
  
    
      
      
        org.springframework.boot
        spring-boot-dependencies
        2.2.2.RELEASE
        pom
        import
      
      
      
        org.springframework.cloud
        spring-cloud-dependencies
        Hoxton.SR1
        pom
        import
      
   
    

搭建注册中心 cloud-eureka-server9001

首先搭建项目基本就是 写pom,写配置…

pom文件



  
    cloudstudy
    com.tyy.springcloud
    1.0-SNAPSHOT
  
  4.0.0

  cloud-eureka-server9001

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


aplication.yml

server:
 port: 9001
eureka:
 instance:
  hostname: eureka9001.com #eureka服务端的实例名称
 client:
  # false 表示不向注册中心注册自己
  register-with-eureka: false
  # false 表示自己就是注册中心我的职责就是维护服务实例,并不需去检查服务
  fetch-registry: false
  service-url:
   # 集群就是指向其他eureka 单机就是指向自己	
   #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
   defaultZone: http://eureka9001.com:9001/eureka/
  server:
   #关闭自我保护机制,保证不可用服务被及时踢除
   enable-self-preservation: false
   eviction-interval-timer-in-ms: 2000

3.启动类

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

去电脑C:\Windows\System32\drivers\etc 里在hosts 文件

5分钟搭建SpringCloud Eureka服务注册中心的实现_第1张图片

如果找不到,把隐藏文件显示出来就行了

5分钟搭建SpringCloud Eureka服务注册中心的实现_第2张图片

搭建客户端 cloud-provider-8001 注册到9001

依旧先写入pom文件

1.pom.xml



  
    cloudstudy
    com.tyy.springcloud
    1.0-SNAPSHOT
  
  4.0.0

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

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

2.application.yml

server:
 port: 8001

spring:
 application:
  name: cloud-dept-service
 datasource:
  username: root
  password: root
  url: jdbc:mysql://localhost:3306/db2020?useUnicode=true&characterEncoding-utr-8&useSSL=false
  driver-class-name: com.mysql.jdbc.Driver

eureka:
 client:
  # 表示是否将自己注册到EurekaServer 默认true
  register-with-eureka: true
  service-url:
    defaultZone: http://eureka9001.com:9001/eureka/
 instance:
  instance-id: 8001
  prefer-ip-address: true    #访问路径显示ip地址


mybatis-plus:
 configuration:
  log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

3.启动类

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

测试

是不是很简单呢 启动时 要先启动注册中心 再启动客户端

5分钟搭建SpringCloud Eureka服务注册中心的实现_第3张图片

这样就算搭建好啦~!

到此这篇关于5分钟搭建SpringCloud Eureka服务注册中心的实现的文章就介绍到这了,更多相关SpringCloud Eureka服务注册中心内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(5分钟搭建SpringCloud Eureka服务注册中心的实现)