1. 程式人生 > >spring cloud 學習之服務註冊及發現(eureka)(一)

spring cloud 學習之服務註冊及發現(eureka)(一)

首先,spring cloud使用的基礎是spring boot ,建立在能熟悉spring boot的基礎之上。 開發工具使用IDEA ,jdk選用8。

一、搭建eureka註冊中心 1、建立spring boot工程,eureka-server,pom.xml中引入下面的依賴:

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.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> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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.SR1</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>

2、在啟動類上,註解@EnableEurekaServer啟動一個服務註冊中心

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

3、配置application.yml檔案

spring:
  application:
    name: eureka-server
server:
  port: 8000
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl.defaultZone: http://127.0.0.1:8000/eureka/ 

在預設設定下,該服務註冊中心也會將自己作為客戶端來嘗試註冊它自己,所以我們需要禁用它的客戶端註冊行為。 忽略了配置eureka.client.service-url.defaultZone會導致異常


com.sun.jersey.api.client.ClientHandlerException: org.apache.http.conn.ConnectTimeoutException: 
Connect to localhost:8761 timed out

這樣就是一個eureka服務註冊中心。

二、搭建服務提供方 1、建立spring boot工程 eureka-client,pom.xml配置

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.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>
	</properties>

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

2、啟動類上,使用註解@EnableDiscoveryClient能啟用Eureka中的DiscoveryClient實現,這樣才能實現Controller中對服務資訊的輸出。

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {

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

3、配置application.yml檔案

server:
  port: 9000
spring:
  application:
    name: eureka-client
eureka:
  client:
    serviceUrl.defaultZone: http://127.0.0.1:8000/eureka/

4、編寫測試的controller

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private DiscoveryClient discoveryClient;
    @RequestMapping("/findService")
    public String findService(){
        String services = "Register Services:" + discoveryClient.getServices();
        System.out.println(services);
        return services;
    }
}

在這裡插入圖片描述

Register Services:[eureka-client]