Spring Colud学习(2) -- 搭建一个普通多节点Spring Colud框架

    xiaoxiao2025-05-21  42

    一、搭建多节点注册中心Eureka Server

    spring cloud已经帮我实现了服务注册中心,我们只需要很简单的几个步骤就可以完成。

    1、pom中添加依赖

    <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>

     2、添加启动代码中添加@EnableEurekaServer注解

    @SpringBootApplication @EnableEurekaServer public class WebEurekaApplication { public static void main(String[] args) { SpringApplication.run(WebEurekaApplication .class, args); } }

    3、配置文件

    在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,在application.properties添加以下配置:

    spring.application.name=spring-cloud-eureka server.port=8001 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/,http://localhost:8001/eureka/ spring.application.name=spring-cloud-eureka server.port=8000 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/,http://localhost:8001/eureka/ eureka.client.register-with-eureka :表示是否将自己注册到Eureka Server,默认为true。eureka.client.fetch-registry :表示是否从Eureka Server获取注册信息,默认为true。eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。

    创建成功后访问http://localhost:8000/eureka/,http://localhost:8001/eureka/可以看到注册中心页面,此为双节点,搭建集群的思路是一样的,唯一区别是发布时取不同的.properties文件

    二、创建服务提供者

    1、pom包配置

    创建一个springboot项目,pom.xml中添加如下配置:

    <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>

    2、配置文件

    application.properties配置如下:

    spring.application.name=spring-cloud-producer server.port=8002 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/,http://localhost:8001/eureka/ spring.application.name=spring-cloud-producer server.port=8003 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/,http://localhost:8001/eureka/

    3、配置启动类

    @SpringBootApplication @EnableDiscoveryClient public class ProducerApplication { public static void main(String[] args) { SpringApplication.run(ProducerApplication.class, args); } }

    4、提供Controller服务

    @RestController public class HelloController { @RequestMapping("/hello") public String index() { return "hello ,this is first messge"; } }

    添加@EnableDiscoveryClient注解后,项目就具有了服务注册的功能。启动工程后,就可以在注册中心的页面看到SPRING-CLOUD-PRODUCER服务

     

    三、 创建服务调用方,消费者

    1. pom.xml创建,跟服务端一样

    2. 创建配置文件

    spring.application.name=spring-cloud-consumer server.port=8004 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/,http://localhost:8001/eureka/

    3. 配置启动类 启动类添加@EnableDiscoveryClient@EnableFeignClients注解。

    @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } } @EnableDiscoveryClient :启用服务注册与发现@EnableFeignClients:启用feign进行远程调用

    4. feign实现调用

    @FeignClient(name= "spring-cloud-producer") public interface HelloRemote { @RequestMapping(value = "hello") public String hello(); } name:远程服务名,及spring.application.name配置的名称

    此类中的方法和远程服务中contoller中的方法名和参数需保持一致。

     

    如果碰到相关jar无法下载的情况,请参考:

    解决本地Maven仓库无法下载Jar或下载失败的情况

     

     

    源码地址:https://download.csdn.net/download/u012547633/11206049

     

    上一篇:Spring Colud学习(1) -- 什么是Spring Cloud

     

     

    最新回复(0)