SpringCloud介绍及入门一
本文内容纲要:
-springcloud是什么
-idea如何创建多个模块。
-1.创建EurekaServer
-2.创建生产者和消者共用的接口
-3.创建服务提供者
-现在的项目架构
-开始配置
springcloud是什么
基于springboot实现的服务治理工具包,管理和协微服务
把别人的东西拿来组合在一起,形成各种组件
微服务协调者【serviceregisttry注册中心Eureka】--统一的网关【apigateway-zuul】
负载均衡【ribbon】--分布式配置【config】-断路器【hystrix】
服务者--消费者都向Eureka注册中心去注册,一个负责生产,一个负责消费
idea如何创建多个模块。
和eclipse有点区别,创建maven项目
看下项目结构
和eclipse中的workspace一样。
1.创建EurekaServer
带个端口明确一下。
2.创建生产者和消者共用的接口
3.创建服务提供者
4.创建服务消费者
现在的项目架构
开始配置
想让项目中的四个模块都拥有springboot
让四个模块继承一个父亲。在总项目的pom.xml中添加springboot,这个下面的四个模块都会继承
总项目的pom.xml
${spring-cloud.version}是一个变量,要定义在properties中。
以下代码是引入springcloud.
一定要注意版本是否正确。我在这里就入了一个大坑。
先开始定义的spring-cloud.version写成了spring-cloud.verison,导致cloud怎么也导入不进来
费了很大功夫才发现。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java-version>1.8</java-version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
springboot与springcoud的版本选择
这里要注意springboot与springcoud的版本的匹配,可以参考:
https://spring.io/projects/spring-cloud
springboot的最新版,springcloud不一定就支持,要看对应关系。
这里有一位同学写的踩坑旅程,有兴趣的可以参考一下。少走一些弯路
https://blog.51cto.com/honly/2156583?source=dra
以下图中可以看到。srpingboot2.0开头的版本,要用springcloud的Finchley来适配
Finchley又分为Release,SR1-SR4.选择自己需要的就可以。比如FinchleyRelease或者FinchleySR1
以下网址是springcloudmaven中面仓库的地址
https://repo1.maven.org/maven2/org/springframework/cloud/spring-cloud-dependencies/
可以看到Finchley.Release是Finchley最新发布的一个版本,之后才有sr1-4
然后是Greenwich.Releash版本,这个是针对springboot2.1.x版本做的适配
每个版本都有针对的说明:比如
finchley.Release的官方说明:https://spring.io/blog/2018/06/19/spring-cloud-finchley-release-is-available
中文说明参考:https://www.oschina.net/news/97226/spring-cloud-finchley-released
显著变化:
- 与SpringBoot2.0.x兼容
- 不支持SpringBoot1.5.x
- 最低要求Java8
- 新增SpringCloudFunction和SpringCloudGateway
以下代码是表示所有的模块都继承springboot
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/><!--lookupparentfromrepository-->
</parent>
如果表示模块继承了,在模块的pom.xml中
//<artifactId>microservice_paent</artifactId>表示继承了父类
<parent>
<artifactId>microservice_paent</artifactId>
<groupId>com.project</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
Eureka注册中心的pom.xml
添加springboot,spring.cloud的依赖。
<dependencies>
<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-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Eureka注册的配置
在src->main->resources中添加application.yml.指定服务器端口号,同时配置Eureka的配置
server:
port:7001
eureka:
instance:
hostname:127.0.0.1
client:
register-with-eureka:false
fetch-registry:false
service-url:
defaultZone:http://${eureka.instance.hostname}:${server.port}/eureka/
在src->main->java中添加新类。
这些类似于单项目springboot项目的创建。
这里如果@EnableEurekaServer不出现智能提示,或者明显有错误,需要去检查springcloud是否导入进来
importorg.springframework.boot.autoconfigure.SpringBootApplication;
importorg.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
publicclassEurekaServiceApplication{
publicstaticvoidmain(String[]args){
SpringBootApplication.run(EurekaServiceApplication.class,args);
}
}
也可以去检查依赖的包里是否有cloud的字样,比如下图,如果没有,检查配置pom.xml确保springcloud导入进来再进行操作。
下面就会有区别了。请等下一篇,今天有几个坑入的比较深
本文内容总结:springcloud是什么,idea如何创建多个模块。,1.创建EurekaServer,2.创建生产者和消者共用的接口,3.创建服务提供者,现在的项目架构,开始配置,
原文链接:https://www.cnblogs.com/hsapphire/p/11037503.html