Spring Cloud学习 服务的注册与发现(Eureka)

Posted hsbt2333

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Cloud学习 服务的注册与发现(Eureka)相关的知识,希望对你有一定的参考价值。

1 创建服务注册中心

在这里,我需要用的组件是Spring Cloud Netflix的Eureka ,eureka是一个服务注册和发现模块。

1.1    创建model工程作为服务注册中心Eureka-server

 项目结构

 技术分享图片

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>

    <groupId>com.seawaterbt.cloud</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.13.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <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>Edgware.SR4</spring-cloud.version>
    </properties>

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

        <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>

 

1.2 启动服务注册中心Eureka-server

springboot启动类

package com.seawaterbt.cloud.eurekaserver;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

 

application.yml

server:
  port: 8761
  context-path: /eureka-server
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka-server/

通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.

启动工程,打开浏览器访问:  http://localhost:8761/eureka-server/ ,界面如下:

技术分享图片

No application available 没有服务被发现 ……^_^   因为没有注册服务当然不可能有服务被发现了。

2 创建服务提供者 Eureka-client

1.1    创建model工程作为服务提供者Eureka-client

  项目结构

  技术分享图片

 

  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>

    <groupId>com.seawatebt.cloud</groupId>
    <artifactId>eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.13.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <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>Edgware.SR4</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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>

1.2 创建一个简单的服务接口

  目录结构

技术分享图片

 

  InfoController.java

package com.seawatebt.cloud.eurekaclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class InfoController {

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

    @GetMapping({"/hi","/hello"})
    public String hi(@RequestParam(value = "name",defaultValue = "seawaterBt")String name){
        return "name="+name+"-----port="+port;
    }
}

 

1.3  启动服务提供者Eureka-client

  配置springboot启动类

    EurekaClientApplication

package com.seawatebt.cloud.eurekaclient;

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

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

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

  application.yml

server:
  port: 8762
  context-path: /eureka-client

#服务名称
spring:
  application:
    name: server-hi
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka-server/eureka/

spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name

启动工程,打开http://localhost:8761/eureka-server/ ,即eureka server 的网址:

 技术分享图片

 

你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI ,端口为8762

访问 http://pc-20180321caba:8762/eureka-client/hi 你会在浏览器上看到 :

技术分享图片

 

3. 参考资料

  https://blog.csdn.net/forezp/article/details/69696915

 

以上是关于Spring Cloud学习 服务的注册与发现(Eureka)的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud笔记03: 服务注册和服务发现的基本概念

Spring Cloud Alibaba —— 服务注册与配置中心

Spring Cloud Alibaba 实战 之 Nacos 服务注册和发现

Spring Cloud(13)——服务注册与发现工具Consul

Spring Cloud Eureka服务注册与发现

Spring Cloud Eureka服务注册与发现