从零搭建springcloud

小灰灰 2023-06-20 09:05 165阅读 0赞

开发工具:IDEA

JDK1.8

sprngcloud微服务的架构基础 :生产者(client),消费者(client),注册中心/配置中心(server)

首先我们创建server,打开idea,选择左侧spring initializr,点击next

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xldHRlcnNz_size_16_color_FFFFFF_t_70

springcloud是以eureka作为注册中心 ,这里要勾选Eureka Server

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xldHRlcnNz_size_16_color_FFFFFF_t_70 1

一直next创建

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xldHRlcnNz_size_16_color_FFFFFF_t_70 2

pom.xml如下,注意要修改springboot与springcloud版本,否则springboot版本太高版本不匹配会爆红报错,建议按照以下配置来。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>1.5.9.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>server</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>server</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. </properties>
  21. <!-- 导入eureka依赖 -->
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  26. </dependency>
  27. </dependencies>
  28. <!-- dependency management -->
  29. <dependencyManagement>
  30. <dependencies>
  31. <dependency>
  32. <groupId>org.springframework.cloud</groupId>
  33. <artifactId>spring-cloud-dependencies</artifactId>
  34. <version>Dalston.SR1</version>
  35. <type>pom</type>
  36. <scope>import</scope>
  37. </dependency>
  38. </dependencies>
  39. </dependencyManagement>
  40. <build>
  41. <plugins>
  42. <plugin>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-maven-plugin</artifactId>
  45. </plugin>
  46. </plugins>
  47. </build>
  48. </project>

在application.properties配置文件里配置eureka

  1. server.port=8081
  2. eureka.instance.hostname=localhost
  3. #在默认设置下, 该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为
  4. eureka.client.registerWithEureka=false
  5. eureka.client.fetchRegistry=false
  6. eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

在启动类上添加

  1. @EnableEurekaServer

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xldHRlcnNz_size_16_color_FFFFFF_t_70 3

启动项目,访问http://localhost:8081

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xldHRlcnNz_size_16_color_FFFFFF_t_70 4

二.创建生产者provider1

新建springboot项目与注册中心server创建一样,pom.xml如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>1.5.9.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>provider1</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>provider1</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. </properties>
  21. <!-- 导入eureka依赖 -->
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  26. </dependency>
  27. </dependencies>
  28. <!-- dependency management -->
  29. <dependencyManagement>
  30. <dependencies>
  31. <dependency>
  32. <groupId>org.springframework.cloud</groupId>
  33. <artifactId>spring-cloud-dependencies</artifactId>
  34. <version>Dalston.SR1</version>
  35. <type>pom</type>
  36. <scope>import</scope>
  37. </dependency>
  38. </dependencies>
  39. </dependencyManagement>
  40. <build>
  41. <plugins>
  42. <plugin>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-maven-plugin</artifactId>
  45. </plugin>
  46. </plugins>
  47. </build>
  48. </project>

application.properties,注意端口不能跟server服务端口一样,eureka地址端口要与server服务一样。

  1. server.port=8082
  2. eureka.client.serviceUrl.defaultZone=http://localhost:8081/eureka/
  3. #服务名称
  4. spring.application.name=provider1

启动类上添加

  1. @EnableEurekaClient

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xldHRlcnNz_size_16_color_FFFFFF_t_70 5

创建controller写一个提供方法

  1. import org.springframework.web.bind.annotation.GetMapping;
  2. import org.springframework.web.bind.annotation.RequestParam;
  3. import org.springframework.web.bind.annotation.ResponseBody;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. @RestController
  8. public class controller {
  9. /**
  10. * 假如这个客户端要提供一个getCloud的方法
  11. * @return
  12. */
  13. @GetMapping(value = "/getCloud")
  14. @ResponseBody
  15. public Map<String,Object> getCloud(@RequestParam String name){
  16. Map<String,Object> data = new HashMap<>();
  17. data.put("userName",name);
  18. data.put("from","provider-A");
  19. return data;
  20. }
  21. }

启动项目,可见在注册中心已经注册上了

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xldHRlcnNz_size_16_color_FFFFFF_t_70 6

然后我们访问provider1服务

20191209172947983.png

三.创建消费者(customer)

新建springboot项目与注册中心server创建一样,pom.xml如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>1.5.9.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>customer</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>customer</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. </properties>
  21. <!-- 导入eureka依赖 -->
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  26. </dependency>
  27. </dependencies>
  28. <!-- dependency management -->
  29. <dependencyManagement>
  30. <dependencies>
  31. <dependency>
  32. <groupId>org.springframework.cloud</groupId>
  33. <artifactId>spring-cloud-dependencies</artifactId>
  34. <version>Dalston.SR1</version>
  35. <type>pom</type>
  36. <scope>import</scope>
  37. </dependency>
  38. </dependencies>
  39. </dependencyManagement>
  40. <build>
  41. <plugins>
  42. <plugin>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-maven-plugin</artifactId>
  45. </plugin>
  46. </plugins>
  47. </build>
  48. </project>

application.properties

  1. server.port=8083
  2. eureka.client.serviceUrl.defaultZone=http://localhost:8081/eureka/
  3. spring.application.name=customer1

启动类上添加

  1. @EnableEurekaClient

创建消费方法

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import org.springframework.web.client.RestTemplate;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. @RestController
  12. public class customerController {
  13. @Autowired
  14. RestTemplate restTemplate;
  15. /**
  16. * 实例化RestTemplate
  17. * @return
  18. */
  19. @LoadBalanced
  20. @Bean
  21. public RestTemplate rest() {
  22. return new RestTemplate();
  23. }
  24. /**
  25. * Rest服务端使用RestTemplate发起http请求,然后得到数据返回给前端
  26. * @param name
  27. * @return
  28. */
  29. @GetMapping(value = "/gotoCloud")
  30. @ResponseBody
  31. public Map<String,Object> gotoCloud(@RequestParam String name){
  32. Map<String,Object> data = new HashMap<>();
  33. /**
  34. * 因为他向注册中心注册了服务,服务名称provider1,我们访问provider1即可
  35. */
  36. data = restTemplate.getForObject("http://provider1/getCloud?name="+name,Map.class);
  37. return data;
  38. }
  39. }

启动消费者,可以看到消费者也注册上了

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xldHRlcnNz_size_16_color_FFFFFF_t_70 7

访问http://localhost:8083/gotoCloud?name=wangsheng,可以看到调用了提供者的服务。

20191209174430664.png

那么怎么实现负载均衡呢?

我们把provider1复制一份,修改 application.properties,将端口改为8084,spring.application.name不变

  1. server.port=8084
  2. eureka.client.serviceUrl.defaultZone=http://localhost:8081/eureka/
  3. #服务名称
  4. spring.application.name=provider1

将提供者里面的方法稍微修改一下

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xldHRlcnNz_size_16_color_FFFFFF_t_70 8

然后启动

20191209175210548.png

可以看到provider1有2个实例。

我们再次访问http://localhost:8083/gotoCloud?name=wangsheng

20191209175451334.png

20191209175506747.png

一开始是from A,你刷新一下 变成 from B了,说明这个时候两台提供者在交替工作,从而达到了一个负载均衡的作用。

发表评论

表情:
评论列表 (有 0 条评论,165人围观)

还没有评论,来说两句吧...

相关阅读

    相关 springcloud

    开发工具:IDEA JDK1.8 sprngcloud微服务的架构基础 :生产者(client),消费者(client),注册中心/配置中心(server) 首先我们创建