spring-cloud入门(一):eureka-server(服务发现)
标签:
Spring Cloud
前言
Eureka是一个服务发现和注册框架,细的来说,我们可以分为eureka-server(服务发现)和eureka-client(服务注册)两个,本次我们对eureka-server(服务发现)做一个项目搭建,作为spring-cloud的开篇。
项目结构
maven结构大家应该都清楚(不清楚的需要补一补,百度关于maven的文章不计其数),下面我们来看一看这些关键文件的配置
代码编写
cppba-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.cppba</groupId> <artifactId>cppba-spring-cloud</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <name>${project.artifactId}</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.5.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <spring-cloud.version>Dalston.SR2</spring-cloud.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <modules> <module>cppba-spring-cloud-eureka-server</module> </modules></project>
cppba-spring-cloud-eureka-server > pom.xml
<?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> <artifactId>cppba-spring-cloud-eureka-server</artifactId> <packaging>jar</packaging> <name>${project.artifactId}</name> <parent> <groupId>com.cppba</groupId> <artifactId>cppba-spring-cloud</artifactId> <version>1.0.0</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <build> <finalName>${project.name}</finalName> <plugins> <!--打包可执行的jar--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
SpringCloudEurekaServerApplication.java
package com.cppba;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublic class SpringCloudEurekaServerApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudEurekaServerApplication.class, args); } }
application.properties
server.port=8761 eureka.instance.hostname=eureka-server eureka.client.registerWithEureka=falseeureka.client.fetchRegistry=falseeureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
到此项目搭建完成
启动项目
我们启动SpringCloudEurekaServerApplication中的main方法,访问http://127.0.0.1:8761
到此,eureka-server(服务发现)项目搭建成功
作者:大黄蜂coder
链接:https://www.jianshu.com/p/c71ce827b261
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦