SpringCloud(Finchley版)1 - Eureka 註冊中心

一, 簡介

spring cloud Finchley 官方文檔:  http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.htmlhtml

 spring cloud 微服務的 重要環節就是 服務的註冊於發現 ,  而 服務註冊中心組件就是 :   Eurekajava

 二, 建立 spring cloud 項目

1, 新建 Maven 項目 cloud, 做爲 父包使用 :

    pom.xml : git

<?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>com.gy.cloud</groupId>
    <artifactId>cloud</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>

    <modules>
        <module>cloud-a</module>
        <module>cloud-b</module>
        <module>cloud-c</module>
        <module>cloud-d</module>
        <module>cloud-e</module>
        <module>cloud-f</module>
        <module>cloud-g</module>
        <module>cloud-h</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <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>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

</project>

2, 新建模塊Module  Spring Boot 項目 cloud-a, 做爲服務的註冊中心

只需引用Eureka服務包便可:web

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

1, cloud-a pom.xml:spring

<?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>
    <parent>
        <groupId>com.gy.cloud</groupId>
        <artifactId>cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>cloud-a</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>cloud-a</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>

2, 註冊中心配置: application.yml : apache

# http://localhost:8761/ 可訪問 eureka server 界面
server:
  port: 8761

eureka:
  instance:
    hostname: localhost   # 服務IP
  client:
    registerWithEureka: false
    fetchRegistry: false
    # 註冊地址
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
# 服務名稱
spring:
  application:
    name: eurka-server

3, 註冊中心啓動類 CloudAApplication :app

package com.gy.cloud.clouda;

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

@EnableEurekaServer
@SpringBootApplication
public class CloudAApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudAApplication.class, args);
        System.out.println("=== 啓動服務註冊中心成功 ===");
    }
}

5, 註冊中心啓動成功後訪問:  http://localhost:8761/maven

簡單的註冊中心就搭建完成;spring-boot

3, 新建 客戶端服務 cloud-b , 向整個項目提供服務 :

 cloud-b pom.xml :微服務

<?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>
    <parent>
        <groupId>com.gy.cloud</groupId>
        <artifactId>cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>cloud-b</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>cloud-b</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

cloud-b application.yml : 

server:
  port: 8762

# 服務名稱
spring:
  application:
    name: service-b

# 服務註冊地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

cloud-b 啓動類 CloudBApplication :

package com.gy.cloud.cloudb;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableEurekaClient
@SpringBootApplication
public class CloudBApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudBApplication.class, args);
        System.out.println("=== 服務B啓動成功 === ");
    }

    @Value("${server.port}")
    private String port;

    @GetMapping("/hi")
    public String home(String name) {
        name = name == null ? "SERVICE-B" : name;
        return "Hi " + name + " , I am from port: " + port;
    }

}

服務B配置完成, 啓動成功後,查看: http://localhost:8761/

SERVICE-B 就會註冊到 Eureka 中 ;

學習文檔

方誌朋的博客 :   https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f1-eureka/

項目源碼:  https://gitee.com/ge.yang/spring-demo/tree/master/cloud

相關文章
相關標籤/搜索