1. 程式人生 > >(1-1)SpringCloud-Eureka:服務的註冊與發現

(1-1)SpringCloud-Eureka:服務的註冊與發現

SpringCloud Eureka是SpringCloud Netflix服務套件中的一部分,它基於Netflix Eureka做了二次封裝,主要負責完成微服務架構中的服務治理功能。下面來做一個示例:

一、搭建服務註冊中心:

  1.構建一個maven專案

  2.新增maven依賴

複製程式碼

<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>org.hope</groupId>
  <artifactId>eureka-server</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>eureka-server</name>

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

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

  <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>Dalston.RC1</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>

  <repositories>
    <repository>
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>https://repo.spring.io/milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
</project>

複製程式碼

  3.寫啟動類

複製程式碼

package org.hope;

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

複製程式碼

  4.springboot的配置檔案application.yml

複製程式碼

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    #由於該應用為註冊中心,所以設定為false,代表不向註冊中心註冊自己
    registerWithEureka: false
    #由於註冊中心的職責就是維護服務例項,它並不需要去檢索服務,所以也設定為false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

複製程式碼

  5.執行EurekaServerApplication啟動服務

  在瀏覽器中輸入http://localhost:8761,就可以看到Eureka的管理介面

  

二、服務註冊與服務發現---註冊服務提供者

  1.新建maven專案

  2.新增pom依賴

複製程式碼

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

複製程式碼

  3.寫啟動類ServiceApplication

@EnableEurekaClient註解是基於spring-cloud-netflix依賴,只能eureka使用

@EnableDiscoveryClient註解是基於spring-cloud-commons依賴,並且在classpath中實現

它們的作用是一樣的

複製程式碼

package org.hope;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
//@EnableDiscoveryClient
@RestController
public class ServiceApplication {

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

}

複製程式碼

   4.寫一個Controller測試用

複製程式碼

package org.hope;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    private final Logger logger = Logger.getLogger(getClass());

    @Autowired
    private DiscoveryClient client;

    @RequestMapping(value = "hello", method = RequestMethod.GET)
    public String index() {
        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("/hello, host:" + instance.getHost() + ", service_id:" + instance.getServiceId());
        return "Hello World";
    }
}

複製程式碼

   5.配置檔案application.yml

複製程式碼

eureka:
  client:
    serviceUrl:
      #註冊中心的地址
      defaultZone: http://localhost:8761/eureka/
server:
  #當前服務埠號
  port: 8762
spring:
  application:
    #當前應用名稱
    name: service-hi

複製程式碼

  6.執行應用

我們會在Eureka的控制檯上看到應用名,證明服務提供者已經成功註冊在Eureka上了

7.在瀏覽器中輸入http://localhost:8762/hello

 

 https://gitee.com/huayicompany/springcloud-learn/tree/master/lesson1

參考:

[1] 部落格,http://blog.csdn.net/forezp/article/details/69696915

[2] 部落格,http://www.cnblogs.com/skyblog/p/5133752.html

[3] 《SpringCloud微服務實戰》,電子工業出版社,翟永超