写点什么

【springcloud】eureka 服务治理入门

  • 2021 年 11 月 11 日
  • 本文字数:2209 字

    阅读完需:约 7 分钟

//表示服务注册中心


@EnableEurekaServer


@SpringBootApplication


public class EurekaApplication {


public static void main(String[] args) {


SpringApplication.run(EurekaApplication.class,args);


}


}


3. yml 配置




server:


port: 7001


spring:


application:


name: cloud-eureka-service


eureka:


instance:


hostname: eureka7001


client:


#false 表示不向注册中心注册自己


register-with-eureka: false


#false 表示自己就是注册中心,我的职责是维护服务实例,并不需要去检索服务


fetch-registry: false


service-url:


#设置与 eureka server 交互的地址查询服务和注册服务都依赖这个地址


defaultZone: http://{server.port}/eureka/


4. 测试





三、Eureka Server 集群部署


======================================================================================


1. pom 依赖引用(和单机版一致)




<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-web</artifactId>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-actuator</artifactId>


</dependency>


2. 配置启动类




//eureka-7001


import org.springframework.boot.SpringApplication;


import org.springframework.boot.autoconfigure.SpringBootApplication;


import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;


//表示服务注册中心


@EnableEurekaServer


@SpringBootApplication


public class EurekaApplication {


public static void main(String[] args) {


SpringApplication.run(EurekaApplication.class,args);


}


}


//eureka-7002


import org.springframework.boot.SpringApplication;


import org.springframework.boot.autoconfigure.SpringBootApplication;


import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;


//表示服务注册中心


@EnableEurekaServer


@SpringBootApplication


public class EurekaApplication {


public static void main(String[] args) {


SpringApplication.run(EurekaApplication.class,args);


}


}


3. yml 配置



7001

server:


port: 7001


spring:


application:


name: cloud-eureka-service


eureka:


instance:


hostname: eureka7001


client:


#false 表示不向注册中心注册自己


register-with-eureka: false


#false 表示自己就是注册中心,我的职责是维护服务实例,并不需要去检索服务


fetch-registry: false


service-url:


#设置与 eureka server 交互的地址查询服务和注册服务都依赖这个地址


defaultZone: http://eureka7002:7002/eureka/

7002

server:


port: 7002


spring:


application:


name: cloud-eureka-service


eureka:


instance:


hostname: eureka7002


client:


#false 表示不向注册中心注册自己


register-with-eureka: false


#false 表示自己就是注册中心,我的职责是维护服务实例,并不需要去检索服务


fetch-registry: false


service-url:


#设置与 eureka server 交互的地址查询服务和注册服务都依赖这个地址


defaultZone: http://eureka7001:7001/eureka/


4. 测试






四、Eureka client 部署(服务消费者和服务提供者)


=================================================================================================


1. pom 依赖引入




<dependency>


<groupId>org.springframework.cloud</groupId>


<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-web</artifactId>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-actuator</artifactId>


</dependency>


2. 配置主启动类




import org.springframework.boot.SpringApplication;


import org.springframework.boot.autoconfigure.SpringBootApplication;


import org.springframework.cloud.netflix.eureka.EnableEurekaClient;


@EnableEurekaClient


@SpringBootApplication


public class PaymentApplication {


public static void main(String[] args) {


SpringApplication.run(PaymentApplication.class,args);


}


}


3. yml 配置




server:


port: 8888


eureka:


cli


【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


ent:


register-with-eureka: true


fetch-registry: true


service-url:


defaultZone: http://eureka7001:7001/eureka,http://eureka7002:7002/eureka


instance:


prefer-ip-address: true


instance-id: payment-8888


4. 测试





五、配置 actuator 信息


=================================================================================


eureka:


client:


register-with-eureka: true


fetch-registry: true


service-url:


defaultZone: http://eureka7001:7001/eureka,http://eureka7002:7002/eureka


instance:


prefer-ip-address: true #显示主机 ip 名称


instance-id: payment-8888 #实例 id


management:


endpoint:


health:


show-details: always #显示详细信息


endpoints:


web:


exposure:


exclude: env #排除 actuator 暴露的接口


include: ["health","info","mappings"] #设置暴露的 actuator 接口

评论

发布
暂无评论
【springcloud】eureka服务治理入门