服务注册中心
1.创建maven项目
2.new module
3.选择cloud-discovery ----eureka-server
创建完毕,pom结构
<?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.example.server</groupId>
<artifactId>server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.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>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-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>${spring-cloud.version}</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>
</project>
4.配置文件application.propertires中添加如下参数
#端口
server.port=8000
#访问地址
eureka.instance.hostname=localhost
#是否注册为服务
eureka.client.register-with-eureka=false
#是否从注册中心获取注册信息
eureka.client.fetch-registry=false
#与注册中心交互地址
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
5.在启动类中增加注解
@EnableEurekaServer
6.启动服务,访问http://localhost:8000
服务发现
1.创建新的module,与server步骤相同
2.启动类中添加注解
@EnableEurekaClient
3.配置文件application.properties
#端口
server.port=8100
#服务名
spring.application.name=hello-client
#注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
4.启动服务
访问注册中心,该服务被注册中心发现,并注册
启动多服务
去掉single instance only ,添加参数-Dserver:port=8200
应用,并启动服务,访问服务中心