springcloud eureka注册中心分布式配置
最近在学习springcloud,做下笔记以及记下遇到的坑。
1.建立maven工程,结构很简单,一个启动类和一个配置文件,结构如下图所示
2.启动类代码如下,需要添加注册中心注解:EnableEurekaServer
1 package com.chendb.eureka; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 7 @SpringBootApplication 8 @EnableEurekaServer 9 public class SpringCloudEurekaApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(SpringCloudEurekaApplication.class, args); 13 } 14 }
3.配置文件application.yml如下代码,在这里遇到了一个坑,项目能够成功启动,但是访问配置中心一直报404,经过查询,是因为freemarker的viewResolver没有在spring-cloud-netflix-eureka-server-2.1.0.RELEASE.jar中找到模板,导致访问注册中心报404,故需要在配置文件加上
spring:
freemarker:
prefer-file-system-access: false
这里一共配置了三个配置中心,可通过替换配置分别启动,形成分布式
spring: profiles: active: dev1 # freemarker的viewResolver没有在spring-cloud-netflix-eureka-server-1.3.1.RELEASE.jar中找到模板,导致访问注册中心报404 freemarker: prefer-file-system-access: false --- spring: application: ###当前服务名 name: eureka-peer profiles: dev server: ##端口号 port: 10000 --- spring: profiles: dev1 application: name: eureka-peer2 server: port: 10001 --- spring: profiles: dev2 application: name: eureka-peer3 server: port: 10002 --- eureka: instance: hostname: dev1 client: ##检索服务 fetch-registry: false ##是否将自己注册到eureka register-with-eureka: false ##客户端默认去这个地址找注册中心 service-url: defaultZone: http://localhost:10000/eureka/,http://localhost:10001/eureka/,http://localhost:10002/eureka/ server: ### 当eureka启动时,不能从集群节点中获取到instance注册信息,应该等多久 wait-time-in-ms-when-sync-empty: 0 enable-self-preservation: true ###eureka多长时间更新一次数据 peer-eureka-nodes-update-interval-ms: 100000
3.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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>spring-cloud-eureka-server</groupId> <artifactId>spring-cloud-eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-cloud-eureka-server</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories> </project>
4.运行启动类,访问http://localhost:10001即可,如下图
5.配置客户端,客户端代码结构如下
6.客户端启动类的配置文件如下
1 package com.chendb.eureka; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 7 @SpringBootApplication 8 @EnableEurekaClient 9 public class SpringCloudEurekaClient { 10 public static void main(String[] args) { 11 SpringApplication.run(SpringCloudEurekaClient.class, args); 12 } 13 }
1 spring: 2 application: 3 name: eureka-client 4 # freemarker的viewResolver没有在spring-cloud-netflix-eureka-server-1.3.1.RELEASE.jar中找到模板,导致访问注册中心报404 5 freemarker: 6 prefer-file-system-access: false 7 8 server: 9 port: 8001 10 11 eureka: 12 client: 13 service-url: 14 defaultZone: http://localhost:10000/eureka/,http://localhost:10001/eureka/,http://localhost:10002/eureka/
7.启动后可以看到该客户端已经被注册到服务端