前面几篇博客中我们大概讲解了一下spring boot的知识,话说管理这些微服务的架构就构成了spring cloud,我们一样,先不说原理先搭建出来再说;
我们今天主要搭建三个服务
1.注册中心(Netflix Eureka):即所有的微服务都得上注册中心中注册才能进行管理
2.网关(Netflix Zuul):路由配置(拦截过滤)
3.正常服务:我们这儿就先搭建一个
一、注册中心
先搭一套spring boot的框架,具体的流程就不说了,之前讲的很明白了,然后我们给这套框架加配置configuration;
只需要在spring boot的基础上添加依赖包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
后面这个依赖是为了管理spring cloud的所有依赖包方便加的,要是不想加也可以,就在上面依赖包上加一个版本号就可以了。
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
然后我们在启动文件application.java上添加
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args){
new SpringApplicationBuilder(EurekaApplication.class).web(true).run(args);
}
}
注意添加的注解@EnableEurekaServer
还有一个是@EnableEurekaClient
,注意别添加错了。
然后在application.properties中添加
server.port=1111
eureka.server.eviction-interval-timer-in-ms=5000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.server.enable-self-preservation=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
其实只有最后一行是必须的,其它的都是配置参数,视具体的环境而加;
这样,启动工程,然后访问:http://localhost:1111/,我们会看到启动页面:
好了,我们接下来配置gateway
二、GateWay
一样,我们先看依赖
这个依赖ZUUL是gateway的核心部分,主要靠这个包来路由
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
但是我们为了把该服务也注册到注册中心上,我们就需要添加注册的依赖包,看着和注册中心的挺像吧,其实并非一个包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
然后我们在启动文件上添加:
@EnableZuulProxy
@SpringCloudApplication
public class GateWayApplication {
public static void main(String[] args){
new SpringApplicationBuilder(GateWayApplication.class).web(true).run(args);
}
}
该注解@EnableZuulProxy
就是将spring boot服务配置为网关的关键。
然后我们在application.properties中添加
我们看到后面三行就是将该服务注册到注册中心上,
而前面这个是干嘛的呢?
这儿就是路由的核心,我们把三的服务test1的路径配置为/test1/**,既可以用url,也可以用服务中声明的spring.application.name=test1
当做serviceId来映射
#routes to url
zuul.routes.test1.path=/test1/**
zuul.routes.test1.url=http://localhost:3333/
#zuul.routes.test1.serviceId=test1
eureka.instance.leaseRenewalIntervalInSeconds=2
eureka.instance.leaseExpirationDurationInSecondes=6
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
这样我们启动服务,然后看一下注册中心的管理页面,会发现该服务出现在了管理页面上了;
三、正常的spring boot不说了,我们用这个配置
server.port=3333
spring.application.name=test1
eureka.instance.leaseRenewalIntervalInSeconds=2
eureka.instance.leaseExpirationDurationInSecondes=6
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
注意添加注册中心的依赖包哦!!!
最后我们会看到注册中心的管理页面上会出现两个服务;
路由的话
我们的test1服务如果用:http://localhost:3333/getMsg可以访问的话,我们路由之后,url替换为http://localhost:2222/test1/getMsg访问;
之后我会具体的把配置还有断路器的知识讲讲,未完待续。。。