Spring Cloud 2-Eureka服务发现注册(一)


Spring Cloud Eureka 

 

  • 1.服务端配置
    • pom.xml
    • application.yml
    • Application.java
  • 2.客户端配置
    • pom.xml
    • application.yml
    • 新建HelloController.java
  • 3.优化pom引用
    • 父项目:spring-cloud-base
    • 子项目:eureka-server修改如下
    • 子项目:eureka-client修改如下

 

 


服务架构图

 

http://localhost:8761/

 


 

 

http://localhost:8080/hello

Hello World!

3.优化pom引用

每个pom中都要引用一大堆相同的东西,可以被一个父项目代替掉.

  1. 在父项目引用所有资源
  2. 子项目以父项目为基础进行构建

父项目:spring-cloud-base

<?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.0modelVersion>

    <groupId>com.lyfgroupId>
    <artifactId>spring-cloud-baseartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>pompackaging>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.2.RELEASEversion>
        <relativePath/> 
    parent>


    <repositories>
        <repository>
            <id>spring-snapshotsid>
            <name>Spring Snapshotsname>
            <url>https://repo.spring.io/libs-snapshoturl>
            <snapshots>
                <enabled>trueenabled>
            snapshots>
        repository>
        <repository>
            <id>spring-milestonesid>
            <name>Spring Milestonesname>
            <url>https://repo.spring.io/libs-milestoneurl>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        repository>
        <repository>
            <id>spring-releasesid>
            <name>Spring Releasesname>
            <url>https://repo.spring.io/libs-releaseurl>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        repository>
    repositories>


    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Finchley.SR2version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

子项目:eureka-server修改如下

<?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.0modelVersion>
   
   <groupId>com.lyfgroupId>
   <artifactId>eureka-serverartifactId>
   <version>0.0.1-SNAPSHOTversion>
   <name>eureka-servername>
   <description>Demo project for Spring Bootdescription>

   <properties>
      <java.version>1.8java.version>
   properties>

   <parent>
      <groupId>com.lyfgroupId>
      <artifactId>spring-cloud-baseartifactId>
      <version>1.0-SNAPSHOTversion>
   parent>
   
   <dependencies>
      <dependency>
         <groupId>org.springframework.cloudgroupId>
         <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
      dependency>
   dependencies>

project>

子项目:eureka-client修改如下

<?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.0modelVersion>

   <groupId>com.lyfgroupId>
   <artifactId>eureka-clientartifactId>
   <version>0.0.1-SNAPSHOTversion>
   <name>eureka-clientname>
   <description>Demo project for Spring Bootdescription>

   <properties>
      <java.version>1.8java.version>
   properties>

   <parent>
      <groupId>com.lyfgroupId>
      <artifactId>spring-cloud-baseartifactId>
      <version>1.0-SNAPSHOTversion>
   parent>
   
   <dependencies>
      
      <dependency>
         <groupId>org.springframework.cloudgroupId>
         <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
      dependency>
      
      <dependency>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-starter-webartifactId>
      dependency>
   dependencies>

project>