spring cloud微服务搭建(一)

mac2022-06-30  28

martin fowler大神提出微服务的概念后,各种微服务的技术满天飞,现在用的比较多的是spring cloud和阿里的dubbo,由于dubbo

在16年10月份就停止更新了,不过好像前些天又更新了下,这里我们讲解spring cloud技术

dubbo源码地址:https://github.com/alibaba/dubbo

spring cloud源码地址:https://github.com/spring-cloud

组件有:注册中心+普通服务+断路器+服务网关+分布式追踪+性能监控+消息总线+配置中心

运用的java框架:Intellij idea + spring boot + mybatis + mysql + maven

官网文档:http://spring.io/docs/reference,主要用到了里面的spring boot,spring cloud netflix,spring cloud sleuth,spring cloud stream

spring cloud系列文章目标:记录自己的技术方便自己和其它人学习,不过推荐入门后多去看官方文档,技术最好也有淘汰的一天,锻炼自己的学习能力才是最重要的

一、注册中心

首先新建一个spring boot项目,访问https://start.spring.io/,命名目录名称和项目名称

点击下载,解压压缩包,打开项目,将修改maven的本地仓库地址,maven的远程仓库地址修改成阿里的maven地址,参考地址http://www.cnblogs.com/waterlufei/p/6498526.html

不改也可以主要是慢,有时候慢挺能锻炼人的耐心的哈

访问spring cloud的官网:http://projects.spring.io/spring-cloud/,将最新的版本控制copy下来

在项目的pom.xml中添加

<dependency>     <groupId>org.springframework.cloud</groupId>     <artifactId>spring-cloud-starter-eureka-server</artifactId></dependency>

<dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-eureka</artifactId></dependency>

<dependencyManagement>    <dependencies>       <dependency>           <groupId>org.springframework.cloud</groupId>     <artifactId>spring-cloud-dependencies</artifactId>     <version>Dalston.RELEASE</version>     <type>pom</type>     <scope>import</scope>     </dependency>   </dependencies></dependencyManagement>然后在src/main/resources/application.properties中加入配置文件

spring.application.name=eureka-serverserver.port=1111#设置hostnameeureka.instance.hostname=localhost

#防止注册中心自我注册eureka.client.register-with-eureka=falseeureka.client.fetch-registry=false#注册地址eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:1111/eureka/

然后在src/main/java/com/waterlufei/EurekaServerApplication中加入@EnableEurekaServer,作为注册中心的服务端启动

然后我们启动项目,访问localhost:1111

这样我们的注册中心就启动起来了

二、服务的注册与发现

跟刚刚一样我们创建一个名叫Service-A的spring boot项目,在pom.xml加入:

<dependency>   <groupId>org.springframework.cloud</groupId>   <artifactId>spring-cloud-starter-eureka</artifactId></dependency>

<dependencyManagement>   <dependencies>     <dependency>       <groupId>org.springframework.cloud</groupId>       <artifactId>spring-cloud-dependencies</artifactId>       <version>Dalston.RELEASE</version>       <type>pom</type>       <scope>import</scope>     </dependency>   </dependencies></dependencyManagement>

在配置文件src/main/resources/application.properties中加入

spring.application.name=service-Aserver.port=2222eureka.instance.hostname=localhosteureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:1111/eureka/

在主函数src/main/java/com/waterlufei/EurekaServerApplication

上面注解@EnableDiscoveryClient,作为注册中心的客户端启动并能发现其它服务,自己也能被别的服务发现

tomcat已经集成到dependency中了,启动项目,访问localhost:1111,发现服务A已经注册到注册中心了

如果有红色警告不用管它,这是提示eureka开启了安全模式,保护注册的实例,就是说如果服务A挂了,还是会在注册中心显示的

eureka默认开启安全模式可以关闭,在注册中心服务EurekaServer的配置文件中加入

eureka.server.enable-self-preservation=false关闭安全模式

现在我们访问localhost:2222

发现没有主页面,我们在resources文件夹下新建public文件夹,然后在public下面新建index.html,写入内容,重启服务A

访问localhost:2222

所以我们开发的时候,前端的页面可以放在resources/public文件夹下面

 开源中国源码地址:https://git.oschina.net/wustxiabin/spring-cloud

 github源码地址:    https://github.com/waterlufei/spring-cloud

转载于:https://www.cnblogs.com/waterlufei/p/6826720.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)