SpringCloud — (2) 服务治理Eureka集群
1.新建一个普通的mavent项目,该项目也为Eureka注册中心
new maven-project→springcloud-2.0-eureka-8200(jar)
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.dengfeng</groupId> <artifactId>springcloud-2.0-eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <!-- 管理依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.M7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!--SpringCloud eureka-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.0.1</version> </dependency> </dependencies> <!-- 注意: 这里必须要添加, 否者各种依赖有问题 --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
在srm/main/resource下新建application.yml
###服务端口号 server: port: 8200 ###eureka 基本信息配置 spring: application: name: eureka-server eureka: instance: ###注册到eurekaip地址 hostname: 127.0.0.1 client: serviceUrl: defaultZone: http://127.0.0.1:8100/eureka/ ###因为自己是为注册中心,不需要自己注册自己 register-with-eureka: true ###因为自己是为注册中心,不需要检索服务 fetch-registry: true
新建启动类AppEureka
package com.euraka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer /*@EnableEurekaServer作用:开启eurekaServer*/ @SpringBootApplication public class AppEureka { public static void main(String[] args) { SpringApplication.run(AppEureka.class, args); } }
修改springcloud-2.0-eureka-server工程的application.yml
###服务端口号 server: port: 8100 ###eureka 基本信息配置 spring: application: name: eureka-server eureka: instance: ###注册到eurekaip地址 hostname: 127.0.0.1 client: serviceUrl: defaultZone: http://127.0.0.1:8200/eureka/ ###因为自己是为注册中心,不需要自己注册自己 register-with-eureka: true ###因为自己是为注册中心,不需要检索服务 fetch-registry: true
修改服务提供者工程springcloud-2.0-member的application.yml
###服务端口号 server: port: 8100 ###eureka 基本信息配置 spring: application: name: eureka-server eureka: instance: ###注册到eurekaip地址 hostname: 127.0.0.1 client: serviceUrl: defaultZone: http://127.0.0.1:8200/eureka/ ###因为自己是为注册中心,不需要自己注册自己 register-with-eureka: true ###因为自己是为注册中心,不需要检索服务 fetch-registry: true
修改消费者工程springcloud-2.0-order的application.yml
###服务启动端口号 server: port: 8001 ###服务名称(服务注册到eureka名称) spring: application: name: app-dengfeng360-order ###服务注册到eureka地址 #eureka: # client: # service-url: # defaultZone: http://localhost:8100/eureka ###集群地址 eureka: client: service-url: defaultZone: http://localhost:8100/eureka,http://localhost:8200/eureka ###因为该应用为注册中心,不会注册自己 register-with-eureka: true ###是否需要从eureka上获取注册信息,不需要去注册中心上检索服务 fetch-registry: true
分别启动2个注册中心,和提供者和消费者项目
注册中心管理页面
正常调用