1. 程式人生 > >第一篇: 服務的註冊與發現Eureka

第一篇: 服務的註冊與發現Eureka

一、建立服務註冊中心

1-1、建立maven主工程,主要引入spring-boot&spring-cloud通用依賴,之後的專案模組都會引入此專案依賴。

pom.xml 檔案如下:

<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.leopard</groupId>
	<artifactId>leopard-parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
  
	<description>project for Spring Boot</description>
  
  	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    
    <!-- spring boot test-->
    <dependencies>
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
	        <artifactId>spring-boot-starter-test</artifactId>
    		<scope>test</scope>
    	</dependency>
    </dependencies>
    
</project>

1-2、建立一個簡單maven工程作為服務註冊中心,即Eureka Server,並依賴主工程

編輯pom.xml,加入服務中心依賴

<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.leopard</groupId>
	<artifactId>leopard-eureka</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<parent>
		<groupId>com.leopard</groupId>
		<artifactId>leopard-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

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

</project>

編輯服務中心啟動類,在主程式加入@EnableEurekaServer,並啟動

package com.leopard.eureka;

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


@EnableEurekaServer
@SpringBootApplication
public class EurekaServiceApplication {
	public static void main(String[] args) {
        SpringApplication.run(EurekaServiceApplication.class, args);
    }
}

1-3、eureka是一個高可用的元件,它沒有後端快取,每一個例項註冊之後需要向註冊中心傳送心跳(因此可以在記憶體中完成),在預設情況下erureka server也是一個eureka client ,必須要指定一個 server。eureka server的配置檔案application.yml

spring:
  application:
    name: eurka-server

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  server:
    enableSelfPreservation: true
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

registerWithEureka表示是否註冊自身到eureka伺服器

fetchRegistry表示是否從eureka伺服器獲取註冊資訊

通過registerWithEureka:false 和 fetchRegistry:false來表明自己是一個eureka server.

1-4、執行主程式,訪問介面檢視

二、建立一個服務提供者 (eureka client)

2-1、當client向server註冊時,它會提供一些元資料,例如主機和埠,URL,主頁等。Eureka server 從每個client例項接收心跳訊息。 如果心跳超時,則通常將該例項從註冊server中刪除。

建立過程同server類似,建立完pom.xml如下:

<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.leopard</groupId>
		<artifactId>leopard-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.leopard</groupId>
	<artifactId>leopard-service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>leopard-service</name>
	<url>http://maven.apache.org</url>


	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

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

		<!-- 上邊引入 parent,因此 下邊無需指定版本 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

	</dependencies>

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

</project>

2-2、編輯啟動類,並加入@EnableEurekaClient,宣告為服務client 。

package com.leopard.service;

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

@EnableEurekaClient
@SpringBootApplication
public class ServiceApplication {

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

}

僅僅@EnableEurekaClient是不夠的,還需要在配置檔案中註明自己的服務註冊中心的地址,application.yml配置檔案如下:

spring:
  application:
    name: leopard-service

server:
  port: 8081

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

需要指明spring.application.name,這個很重要,這在以後的服務與服務之間相互呼叫一般都是根據這個name 。 
2-3、啟動工程,開啟http://localhost:8761 ,即eureka server 的網址:

你會發現一個服務已經註冊在服務中了,服務名為LEOPARD-SERVICE ,埠為8081

2-4、建立一個測試類

package com.leopard.service.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 測試類
 * @author leopard
 *
 */
@RestController
@RequestMapping(value="/test")
public class TestController {

	@Value("${server.port}")
	private String port;
	
	/**
	 * 獲取資訊
	 * @return
	 */
	@RequestMapping(value="/getObject" , method = RequestMethod.GET)
	public String getObject(){
		return "hi leopard,this port  is :" + port;
	}
	
	/**
	 * 提交資訊
	 * @return
	 */
	@RequestMapping(value="/postObject" , method = RequestMethod.POST)
	public void postObject(){
		//
	}
	
	/**
	 * 更新資訊
	 * @return
	 */
	@RequestMapping(value="/putObject" , method = RequestMethod.PUT)
	public void putObject(){
		//
	}
	
	/**
	 * 刪除資訊
	 * @return
	 */
	@RequestMapping(value="/deleteObject" , method = RequestMethod.DELETE)
	public void deleteObject(){
		//
	}
	
	
}

重新啟動服務訪問連結 : http://localhost:8081/test/getObject

輸出如下:

hi leopard,this port is :8081

PS:這個client工程,後續文章會copy和迭代功能二次引用

文章轉載:https://blog.csdn.net/forezp/article/details/81040925