idea中,SpringBoot整合SpringCloud

it2023-06-02  70

一:搭建eureka服务注册中心

1.依次点击File>New>Project后,选择Spring Initializr(如图),点击Next:

2. 填写Project Metadata后点击Next:

 3.选择Spring Cloud Discovery后,选择Eureka Server,再点击Next:

4.输入Project Name,点击Finish等待项目生成:

 5.编辑application.properties文件:

spring.application.name=eureka-server# eureka实例的主机名 server.port=8761 eureka.client.register-with-eureka=false#不把自己注册到eureka上 eureka.client.fetch-registry=false#不从eureka上来获取服务的注册信息

6. 编辑项目的启动类,一般命名都为***Application,在该类最上方增加启用eureka server的注解:

@EnableEurekaServer

7.启动项目,浏览器访问eureka服务注册中心(本机ip+端口)访问,这时候只启动了注册中心的服务,所以红色方框处什么都没有:

至此,服务注册中心搭建完成。

二:搭建服务提供者(provider):

1.依次点击File>New>Project后,选择Spring Initializr(如图),点击Next:

2. 填写Project Metadata后点击Next:

 3.选择Spring Cloud Discovery后,选择Eureka Discovery Client,再点击Next:

4.输入Project Name,点击Finish等待项目生成:

 5.编辑application.properties文件:

spring.application.name=provider server.port=8001 eureka.instance.prefer-ip-address=true# 注册服务的时候使用服务的ip地址

6. 编辑项目的启动类,一般命名都为***Application,在该类最上方增加启用eureka client的注解:

@EnableEurekaClient

7.编写服务提供者的测试Controller类:

package com.example.provider; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @RequestMapping(value = "/test/get",method = RequestMethod.GET) public String TestMethod(){ return "success1"; } }

8.启动项目,浏览器访问eureka服务注册中心,这时候启动了注册中心的服务,也启动了一个服务提供者,所以红色方框处有了一个服务提供者:

至此,服务提供者已经搭建完毕。

为了比较明显的在服务消费者端感受负载均衡机制,可以按照第二步再创建一个服务提供者,重新创建的服务提供者application.properties文件中的端口需要重新修改,并且TestController类中返回的字符串也需要修改,可以修改成success2。

这样的话,在后面使用服务消费者调用服务提供者的/test/get接口时,如果返回success1我们就知道是第一个服务提供者返回的数据;如果返回success2,我们就知道是第二个服务提供者返回的数据。

注:第二个服务提供者创建完成后也启动起来,启动后可以在eureka服务注册中心看到2个服务提供者,这里不再提供截图。

三:搭建服务消费者(consumer):

注:创建服务消费者和创建服务提供者的步骤是一模一样的,只不过application.properties文件中的端口和name,需要重新定义。

1.在服务消费者的启动类中加上使用负载均衡机制的代码:

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @EnableDiscoveryClient @SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } @LoadBalanced//使用负载均衡机制 @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } }

2.编写服务消费者的测试Controller:

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class TestController { @Autowired RestTemplate restTemplate; @RequestMapping(value = "/test/get",method = RequestMethod.GET) public String TestMethod(){ //此参数为注册在Eureka中的服务 String ticketName = restTemplate.getForObject("http://provider//test/get", String.class); return ticketName; } }

3.启动服务消费者,浏览器访问eureka服务注册中心,这时候会看到2个服务提供者和1个服务消费者

4.调用服务消费者对外暴露的接口,不断刷新调用发现一会返回第一个服务提供者返回的success1,一会返回第二个服务提供者返回的success2:

最新回复(0)