Spring cloud入门教程(1)

川长思鸟来 2022-05-16 07:14 372阅读 0赞
  1. 看到网上关于Spring cloud的入门教程基本是基于Eclipse的,因为我用的是IDEA,所以打算写一份IDEA使用Spring cloud的入门教程。
  2. 废话不多说,直接来干。
  3. Spring cloud是一个分布式架构的服务治理,和Dubbo差不多,但是这个更简单,看教程来搭建一个微服务架构吧。

第一步:先创建一个主Maven工程

  1. ![70][]
  2. pom.xml添加依赖
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <project xmlns="http://maven.apache.org/POM/4.0.0"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  7. <modelVersion>4.0.0</modelVersion>
  8. <groupId>com.xuye.parent</groupId>
  9. <artifactId>parent</artifactId>
  10. <version>1.0-SNAPSHOT</version>
  11. <name>sc-f-chapter1</name>
  12. <description>Demo project for Spring Boot</description>
  13. <parent>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-parent</artifactId>
  16. <version>2.0.3.RELEASE</version>
  17. <relativePath/>
  18. </parent>
  19. <properties>
  20. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  21. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  22. <java.version>1.8</java.version>
  23. <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  24. </properties>
  25. <dependencies>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-test</artifactId>
  29. <scope>test</scope>
  30. </dependency>
  31. </dependencies>
  32. <dependencyManagement>
  33. <dependencies>
  34. <dependency>
  35. <groupId>org.springframework.cloud</groupId>
  36. <artifactId>spring-cloud-dependencies</artifactId>
  37. <version>${spring-cloud.version}</version>
  38. <type>pom</type>
  39. <scope>import</scope>
  40. </dependency>
  41. </dependencies>
  42. </dependencyManagement>
  43. <build>
  44. <plugins>
  45. <plugin>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-maven-plugin</artifactId>
  48. </plugin>
  49. </plugins>
  50. </build>
  51. </project>

第二步:在此项目下,创建一个Module,作为注册中心。

  1. ![70 1][]
  2. 注意该Module是基于spring boot的。然后填好信息一路next,到此步骤,按下图选择,Cloud DiscoveryEureka Server
  3. ![70 2][]
  4. 该模块的pom.xml暂时也没什么需要添加和修改的,因为已经继承了父项目的pom.xml
  5. 我们此时,需要去做的事情是,修改application.yml,如果后缀是properties的记得改后缀,如果看得懂的话,就自己修改成properties的格式。。注册中心的,配置内容如下。
  6. #服务端口号
  7. server:
  8. port: 8901
  9. #服务名称
  10. spring:
  11. application:
  12. name: server
  13. eureka:
  14. instance:
  15. hostname: localhost
  16. client:
  17. #注册中心不需要注册自己,这两处默认值都为true,我们设为false
  18. registerWithEureka: false
  19. #注册中心不需要去发现服务
  20. fetchRegistry: false
  21. #设置服务注册中心的URL,此处则表示为http://localhost:8901/eureka/
  22. serviceUrl:
  23. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  24. 然后去入口,Application中添加注解:@EnableEurekaServer,表示是一个注册中心。

70 3

  1. OK,此时注册中心已经配置完了,直接启动注册中心的服务器。
  2. ![70 4][]
  3. 出现如图所示的样子,则表示注册中心启动成功,如果出现问题的话,请换个端口试试,可能你的端口被使用了。

第三步:在此项目下,新建一个Module,作为服务提供者。

  1. 创建Module的步骤和创建注册中心的Module一模一样,注意名字不要一样就行了。
  2. 创建好了后,我们再来看application.yml 注意,如果后缀是properties的记得改后缀,如果看得懂的话,就自己修改成properties的格式。
  3. server:
  4. port: 8902
  5. #此处需要加上该服务的名字
  6. spring:
  7. application:
  8. name: client
  9. eureka:
  10. client:
  11. #向注册中心注册自己,此处默认为true,可以不用加的,但为了教学解释
  12. registerWithEureka: true
  13. #让注册中心发现自己,默认也为true,可以不用加的,但为了教学解释
  14. fetchRegistry: true
  15. #设置服务注册中心的URL,注册中心已经设置过了,直接去注册中心查看即可。
  16. serviceUrl:
  17. defaultZone: http://localhost:8901/eureka/
  18. 注意上面的registerWithEureka: truefetchRegistry: true是可以不用添加的,因为默认就是true的。
  19. 再去入口处添加注解@EnableEurekaClient,表示是一个服务提供者。我们来测试一下。
  20. ![70 5][]
  21. 启动服务,注册中心和服务提供者都需要启动。
  22. ![70 6][]
  23. 注意看中间的位置,多了一个服务提供者。
  24. 然后输入url:[http://localhost:8902/hello][http_localhost_8902_hello]
  25. ![70 7][]
  26. OK,最简单的案例搭建完成。

发表评论

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

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

相关阅读