1. 程式人生 > >SpringCloud之搭建Eureka

SpringCloud之搭建Eureka

Eureka是基於 REST 的服務,來實現服務的發現與註冊。不多說,直接提供程式碼。

1.maven依賴

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

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

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.6.RELEASE</version>
		<relativePath/>
	</parent>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Camden.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>

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

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

2.application.yml配置

spring:
  application:
    name: xichuan-eureka

server:
  port: 2001

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    registryFetchIntervalSeconds: 5
    serviceUrl:
    #eureka服務的地址
      defaultZone: http://localhost:${server.port}/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 10

#不使用Https
management:
  security:
    enabled: false

3.在啟動類上加上註釋

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

@SpringBootApplication
@EnableEurekaServer     //新增此註解,表示是eureka服務
public class Application {

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

eureka的搭建非常簡單,主要還是看你是如何使用了.如果直接訪問:http://localhost:2001/ ,就可以看到在eureka註冊的服務有哪些。

可以看下大佬的部落格,大佬寫spring cloud的部落格很詳細:https://blog.csdn.net/forezp/article/details/70148833