eclipse创建spring cloud的eureka客户端和eureka服务端 MAVEN工程

接下来我们进入今天的正题

1、创建maven父工程
File—>new—>other—>Maven—>Maven project

eclipse创建spring cloud的eureka客户端和eureka服务端 MAVEN工程_第1张图片

点击“next”

eclipse创建spring cloud的eureka客户端和eureka服务端 MAVEN工程_第2张图片

eclipse创建spring cloud的eureka客户端和eureka服务端 MAVEN工程_第3张图片

2、创建子项目
打开项目中的pom.xml文件选择overview

eclipse创建spring cloud的eureka客户端和eureka服务端 MAVEN工程_第4张图片

第一种创建方式
点击create直接在父工程中创建

eclipse创建spring cloud的eureka客户端和eureka服务端 MAVEN工程_第5张图片

eclipse创建spring cloud的eureka客户端和eureka服务端 MAVEN工程_第6张图片

eclipse创建spring cloud的eureka客户端和eureka服务端 MAVEN工程_第7张图片

使用同样的方法创建eurekaclient。创建完成的项目结构如图

eclipse创建spring cloud的eureka客户端和eureka服务端 MAVEN工程_第8张图片
3、父工程pom.xml配置
在spring cloud官方文档上面有一段maven父工程pom.xmlde示例配置,可以直接引用过来。


  4.0.0
  com.test
  common
  0.0.1-SNAPSHOT
  pom
 
      eurekaserver
      eurekaclient
 

 
       org.springframework.boot
       spring-boot-starter-parent
       1.3.5.RELEASE
       
   


    
        
            org.springframework.cloud
            spring-cloud-dependencies
            Brixton.RELEASE
            pom
            import
        

    


    
        org.springframework.cloud
        spring-cloud-starter-config
    

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

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


    
           
               org.springframework.boot
               spring-boot-maven-plugin
           

    



我们可以看得到,spring-boot-starter-parent的版本是1.3.5.RELEASE,版本有些低我们将它替换到2.0.2.RELEASE的版本,同时需要替换spring-cloud-dependencies的版本,替换配置如下:


       org.springframework.boot
       spring-boot-starter-parent
       2.0.2.RELEASE
       
   

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.RELEASE
                pom
                import
            

        

    

官方文档中缺少很多引用,需要根据项目进行添加,但首先我们需要先添加spring-boot-starter-web jar包。因为刚开始我没有引用这个jar包,导致客户端无法注册到注册中心中。

4、eurekaserver Pom.xml配置

需要在eurekaserver的pom.xml文件中配置:eurekaserver的jar包。
注意:项目应该是jar,如果你的war ,需要你修改成jar


    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  4.0.0
  com.test
  eurekaserver
  0.0.1-SNAPSHOT
  jar
  
 
    com.test
    common
    0.0.1-SNAPSHOT
 

  
  eurekaserver
  
 
   
      junit
      junit
      3.8.1
      test
   

   
      org.springframework.cloud
      spring-cloud-starter-eureka-server
      1.4.3.RELEASE
   

 

  
 
    eurekaserver
 

package com.test.eurekaServer;

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

@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {
     public static void main(String[] args) {
         new SpringApplicationBuilder(ServerApplication.class).web(true).run(args); 
     }

}

这个需要我们在src/main/resources中创建application.yml
点击new—>File

将代码复制进去

server:
  port: 8761   #端口号

eureka:
  instance:
    hostname: localhost   #主机ip
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:  #注册中心地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
在启动类中运行程序
右键点击启动类—>Run as —>java application
在浏览器中输入http://localhost:8761/进入注册中心


4、eurekaclient 配置
和服务器的配置一样,根据官方文档操作即可。直接看配置
pom.xml


    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  4.0.0
  com.test
  eurekaclient
  0.0.1-SNAPSHOT
  jar
  
 
    com.test
    common
    0.0.1-SNAPSHOT
 

  
  eurekaclient

 
   
      junit
      junit
      3.8.1
      test
   

   
      org.springframework.cloud
      spring-cloud-starter-eureka
      1.4.3.RELEASE
   

 

 
    eurekaclient
 


启动类

package com.test.eurekaClient;

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

@SpringBootApplication
@EnableEurekaClient
public class ClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }

}

application.yml

server:
  port: 8762    #端口号

eureka:
  client:
    serviceUrl:   #注册中心地址
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: bwx-common   #在注册中心显示的名字


客户端完成

eclipse创建spring cloud的eureka客户端和eureka服务端 MAVEN工程_第9张图片

你可能感兴趣的:(maven,git,命令)