springcloud~Eureka实例搭建
服务端
build.gradle配置
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
bootstrap.yml相关配置
server.port: 8761
management.port: 8762
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/
启动代码
package lind.sprindemo.eurekaServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
客户端
向我们配置中心的客户端同时也是eureka的一个客户端,例如一个订单服务,它的配置存储在配置中心,它如果希望公开自己,就需要是eureka的一个客户端。
例子
高可用的eureka集群
主要是在eureka里注册另一个eureka,两个eureka实例相互注册,实现一个最高用的集群。
同一时刻,只有一个eureka里有服务,而当这个eureka挂了之后,会把服务自动同步到另一个节点上,这就是高可用。
- 配置代码
server:
port: ${PORT:8761}
management:
port: ${BG_PORT:8762}
application:
name: ${NAME:eurekaserver}
spring:
profiles:
active: dev
---
eureka:
profile: dev
instance:
hostname: ${application.name}
perferIpAddress: true #基于IP地址注册
client:
registerWithEureka: false #false表示不向注册中心注册自己。
fetchRegistry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
serviceUrl:
defaultZone: ${URL:http://${eureka.instance.hostname}:${server.port}/eureka/}
- Dockerfile内容
可以从远程下载你的jar包,如果你的网速足够好的话
FROM fabric8/java-jboss-openjdk8-jdk:1.4
ENV JAVA_APP_JAR="app.jar"
ENV JAVA_MAX_MEM_RATIO=40
ENV AB_OFF ""
ADD --chown=jboss:jboss ./jar/eurekaServer-0.0.2.jar /deployments/app.jar
- docker-compose实现eureka集群
version: "3.3"
services:
eurekaserver1:
build: ./eureka-server
restart: on-failure
ports:
- "6761:6761"
- "6762:6762"
networks:
- dev
environment:
- PORT=6761
- BG_PORT=6762
- NAME=eureka1
- URL=http://eureka2:6761/eureka #集群地址配置
eurekaserver2:
build: ./eureka-server
restart: on-failure
ports:
- "5761:5761"
- "5762:5762"
networks:
- dev
environment:
- PORT=5761
- BG_PORT=5762
- NAME=eureka2
- URL=http://eureka1:6761/eureka #集群地址配置
运行docker-compose up -d 就会先构建eureka的镜像,然后运行你的两个eureka实例了。
合集:
springcloud
分类:
Java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
2017-06-20 docker~Dockerfile优化程序的部署
2016-06-20 插件~使用ECharts动态在地图上标识点
2013-06-20 EF架构~引入规约(Specification)模式,让程序扩展性更强
2012-06-20 面向对象的故事~数据底层操作告诉了我们接口,抽象类,继承与多态性的使用