查看: 1676|回复: 0

[Java代码] spring-cloud入门之eureka-server(服务发现)

发表于 2018-3-3 12:06:48

前言

Eureka是一个服务发现和注册框架,细的来说,我们可以分为eureka-server(服务发现)和eureka-client(服务注册)两个,本次我们对eureka-server(服务发现)做一个项目搭建,作为spring-cloud的开篇。

开源地址:https://github.com/bigbeef

项目结构

maven结构大家应该都清楚(不清楚的需要补一补,百度关于maven的文章不计其数),下面我们来看一看这些关键文件的配置

代码编写

cppba-spring-cloud > pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.cppba</groupId>
  5. <artifactId>cppba-spring-cloud</artifactId>
  6. <version>1.0.0</version>
  7. <packaging>pom</packaging>
  8. <name>${project.artifactId}</name>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>1.5.5.RELEASE</version>
  13. </parent>
  14. <properties>
  15. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  16. <java.version>1.8</java.version>
  17. <spring-cloud.version>Dalston.SR2</spring-cloud.version>
  18. </properties>
  19. <dependencyManagement>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.cloud</groupId>
  23. <artifactId>spring-cloud-dependencies</artifactId>
  24. <version>${spring-cloud.version}</version>
  25. <type>pom</type>
  26. <scope>import</scope>
  27. </dependency>
  28. </dependencies>
  29. </dependencyManagement>
  30. <modules>
  31. <module>cppba-spring-cloud-eureka-server</module>
  32. </modules>
  33. </project>
复制代码

cppba-spring-cloud-eureka-server > pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <artifactId>cppba-spring-cloud-eureka-server</artifactId>
  6. <packaging>jar</packaging>
  7. <name>${project.artifactId}</name>
  8. <parent>
  9. <groupId>com.cppba</groupId>
  10. <artifactId>cppba-spring-cloud</artifactId>
  11. <version>1.0.0</version>
  12. </parent>
  13. <dependencies>
  14. <dependency>
  15. <groupId>org.springframework.cloud</groupId>
  16. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  17. </dependency>
  18. </dependencies>
  19. <build>
  20. <finalName>${project.name}</finalName>
  21. <plugins>
  22. <!--打包可执行的jar-->
  23. <plugin>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-maven-plugin</artifactId>
  26. </plugin>
  27. </plugins>
  28. </build>
  29. </project>
复制代码

SpringCloudEurekaServerApplication.java

  1. package com.cppba;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @EnableEurekaServer
  6. @SpringBootApplication
  7. public class SpringCloudEurekaServerApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(SpringCloudEurekaServerApplication.class, args);
  10. }
  11. }
复制代码

application.properties

  1. server.port=8761
  2. eureka.instance.hostname=eureka-server
  3. eureka.client.registerWithEureka=false
  4. eureka.client.fetchRegistry=false
  5. eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
复制代码

到此项目搭建完成

启动项目

我们启动SpringCloudEurekaServerApplication中的main方法,访问http://127.0.0.1:8761

到此,eureka-server(服务发现)项目搭建成功

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报