服务注册中心 :eureka-server
作用:服务注册中心提供服务注册功能
服务提供方:eureka-client
作用:注册服务到服务注册中心
服务注册中心 :eureka-server
- 创建 一个Spring-Boot 项目(在springboot项目中添加 jar包 依赖)
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 6 <groupId>cn.wh</groupId> 7 <artifactId>firsttest</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>firsttest</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.0.4.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 <spring-cloud.version>Finchley.SR1</spring-cloud.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.cloud</groupId> 31 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 32 </dependency> 33 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-test</artifactId> 37 <scope>test</scope> 38 </dependency> 39 </dependencies> 40 41 <dependencyManagement> 42 <dependencies> 43 <dependency> 44 <groupId>org.springframework.cloud</groupId> 45 <artifactId>spring-cloud-dependencies</artifactId> 46 <version>${spring-cloud.version}</version> 47 <type>pom</type> 48 <scope>import</scope> 49 </dependency> 50 </dependencies> 51 </dependencyManagement> 52 53 <build> 54 <plugins> 55 <plugin> 56 <groupId>org.springframework.boot</groupId> 57 <artifactId>spring-boot-maven-plugin</artifactId> 58 </plugin> 59 </plugins> 60 </build> 61 62 63 </project>
2. 在springboot 的核心类上 加上 @EnableEurekaServer
配置核心配置文件
1 #端口号 2 server.port= 8082 3 eureka.instance.hostname= localhost 4 ##false来表明自己是一个eureka server 5 eureka.client.reg ister-with-eureka=false 6 ##false来表明自己是一个eureka server 7 eureka.client.fetch-registry=false 8 eureka.client.service-url.defaultZone= http://${eureka.instance.hostname}:${server.port}/eureka/ 9 #关闭springboot自带的ioc 10 spring.mvc.favicon.enabled = false
启动我们的项目访问我们的端口号
这个页面就是我们的注册中心
增加一个客户端 对注册中心进行注册
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 6 <groupId>cn.wh</groupId> 7 <artifactId>firsttest</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>firsttest</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.0.4.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 <spring-cloud.version>Finchley.SR1</spring-cloud.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.cloud</groupId> 31 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 32 </dependency> 33 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-test</artifactId> 37 <scope>test</scope> 38 </dependency> 39 </dependencies> 40 41 <dependencyManagement> 42 <dependencies> 43 <dependency> 44 <groupId>org.springframework.cloud</groupId> 45 <artifactId>spring-cloud-dependencies</artifactId> 46 <version>${spring-cloud.version}</version> 47 <type>pom</type> 48 <scope>import</scope> 49 </dependency> 50 </dependencies> 51 </dependencyManagement> 52 53 <build> 54 <plugins> 55 <plugin> 56 <groupId>org.springframework.boot</groupId> 57 <artifactId>spring-boot-maven-plugin</artifactId> 58 </plugin> 59 </plugins> 60 </build> 61 62 63 </project>
客户端的核心配置
1 eureka.client.service-url.defaultZone= http://localhost:8082/eureka/ 2 server.port= 8084 3 spring.application.name= service-hi
在客户端中 使用注解标明这是一个客户端 为下面的负载均衡 做铺垫
客户端和 注册中心同时启动
在注册中心 可以看到 自己配置的客户端已经 成功注册