1、在intellij ideaIDE里,File -> New -> Project -> Maven -> Next


设置组名:

    GroupId : service-server-2

    ArtifactId : service-server-2

Next


设置工程名称

   Project Nameserviceserver2

Finish


总体结构:


2、创建入口类 EurekaServiceRegisterApplication.java

标上

@EnableEurekaServer

@SpringBootApplication

特性


package hello;

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


@EnableEurekaServer 
@SpringBootApplication
public class EurekaServiceRegisterApplication {

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


3、修改 pom.xml

更新 spring-boot,spring-cloud的类库,需要点时间,别着急; 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>service-server-2</groupId>
    <artifactId>service-server-2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <!-- spring boot的maven打包插件 -->
    <build>

        <finalName>service-center-1</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

        <!-- spring boot test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>



4、增加 application.properties


默认端口:8761


#注册服务的端口号
server.port=8761

#是否需要注册到注册中心,因为该项目本身作为服务注册中心,所以为false
eureka.client.register-with-eureka=false
#是否需要从注册中心获取服务列表,原因同上,为false
eureka.client.fetch-registry=false
#注册服务器的地址:服务提供者和服务消费者都要依赖这个地址
eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka

logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF




5、跑起来

菜单:Run -> EurekaServiceRegisterApplication






6、发布jar


1).点击菜单:File -> Project Stucture,进入工程配置界面; 


2).在配置界面的左侧:

*选中“Artifacts ,点 +”,  

*选择“JAR”,二级选择 From Modules With Dependencies”,

*选择“Main Class 入口对应的类;

*选择其它类库的导出方式:复制; 

*最后 点击“OK”,配置完成; 


上述默认生成的jar,会运行不成功,

1、相关依赖的jar签过名,采用嵌入模式生成的jar没签名执行时会提示签名有问题; 

2、采用复制模式时,MANIFEST.MF文件始终不能打包进入jar,总提示找不到入口类; 


经过一天的苦研,最终采用分离式(采用两个art),一个为了生成本工程的jar文件,另一个是为了复制出相关的依赖文件; 

生成本工程的jar文件,采用手分增加文件,用默认的编辑果结死活不行。


过程如下:

*设置独立serviceserver.jar,并加入 

/hello/EurekaServiceRegisterApplication.class

/META-INF/MANIFEST.MF



*相关依赖文件采用复制模式:

   输出文件包括(serviceserver-2,type:other):

       相关依赖的.jar(n个)

       serviceserver.jar

       application.properties






3).点击菜单:Build -> Make Project生成目标文件


4).查看jar文件:工程根目录/out/artifacts/serviceserver_2下;


5).运行: 

cd 工程根目录/out/artifacts/serviceserver_2

java -jar serviceserver.jar;


源代码 :https://github.com/ronmy/EurekaAndDotnetCore2.0




 


Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐