1.springcloud简介
SpringCloud是基于SpringBoot基础之上开发的微服务框架,是一套完整的微服务解决方案,其内容包括服务治理,注册中心,配置管理,断路器,智能路由,微代理,控制总线,全局锁,分布式回话等。
SpringCloud包括众多的子项目
SpringCloud config 分布式配置中心
SpringCloud Netflix 核心组件
1、Eureka:服务治理 注册中心
2、Hystrix:服务保护框架
3、Ribbon: 客户端负载均衡器
4、Feign:基于ribbon和hystrix的声明式服务调用组件
5、zuul:网关组件,提供智能路由,访问过滤等功能。
2.SpringCloud服务注册与发现原理Eureka
依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
@SpringBootApplication
@EnableEurekaServer
再启动Eureka服务器时,服务器会把自己当做一个客户端,去注册Eureka服务器,并且会到Eureka服务器抓取注册信息,它自己本身就是一个服务器,而不是服务的提供者,因此需要修改application.xml文件。
#表示是否将自己注册到Eureka Server上,默认为true
eureka.client.registerWithEureka=false
#表示是否从Eureka Server上获取注册信息,默认为true
eureka.client.fetchRegistry=false
2.编写 服务提供者
新建项目:first-service-provicer
编写控制器:
@RestController
public class FirstController {
@RequestMapping(value = "/person/{pid}",method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public Person findPerson(@PathVariable("pid") Integer pid){
Person person=new Person();
person.setPid(pid);
person.setPname("朱海涛");
return person;
}
}
#将应用配置为first-service-provider,该服务将会被注册到8888的Eureka服务器上
spring.application.name=first-service-provider
#配置服务实例的主机名称
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
@SpringBootApplication
@EnableEurekaClient
然后先启动Eureka服务器,再启动Eureka客户端,就可以发现通过 localhost:8888 可以发现注册中心的服务列表了
3.编写服务调用者
当服务被注册,发布到Eureka服务器后,需要有程序去发现他,并且进行调用。此处所说的调用者,是指同样注册到Eureka的客户端(也即是说Eureka客户端其实也被当做一个服务注册到服务中心了),简单来说就是Eureka的内部调用。
server.port=9000
spring.application.name=first-service-invoker
eureka.instance.hostname=localhost
eureka.client.service-url.defaultZone=http://localhost:8888/eureka/
驱动类
@SpringBootApplication
@EnableDiscoveryClient
编写控制器:
@RestController
@Configuration
public class InvokerController {
@Bean
@LoadBalanced//让这个实例具有分布式的功能
public RestTemplate getRestTemplate (){
return new RestTemplate();
}
@RequestMapping(value = "/router",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
public String router(){
RestTemplate restTemplate=getRestTemplate();
String json=restTemplate.getForObject("http://first-service-provider/person/1",String.class);
return json;
}
}
然后我们依次启动Eureka服务器端,客户端(服务提供端),服务调用者,然后通过服务调用者的端口和控制器方法
输入http://localhost:9000/router来调用服提供端的接口 。
Eureka集群搭建
如果只有一台电脑的话,我们想要构建集群,需要修改hosts文件,为其添加主机名的映射。
修改C:\Windows\System32\drivers\etc\hosts文件
添加如下内容:127.0.0.1 slave1 slave2
对于集群搭建我们可以针对服务器新建两个项目
1.服务器端
启动类:
@EnableEurekaServer
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://slave1:8888/eureka/
# instance:
# hostname: slave2
server:
port: 8889
spring:
application:
name: first-cloud-server
# profiles: slave2
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://slave1:8889/eureka/
# instance:
# hostname: slave2
server:
port: 8888
spring:
application:
name: first-cloud-server
# profiles: slave2
2.服务提供者
spring:
application:
name: first-cloud-provider
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:8888/eureka/,http://localhost:8889/eureka/
#将服务提供者注册到两个服务器中
@SpringBootApplication
@EnableEurekaClient
public class FirstEkServiceProviderApplication {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String port=scanner.nextLine();
new SpringApplicationBuilder(FirstEkServiceProviderApplication.class).
properties("server.port="+port).run(args);
}
}
分步启动输入 8080 8081 端口 防止端口占用。
3.服务调用者
添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
server:
port: 9000
spring:
application:
name: first-cloud-invoker
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:8888/eureka/,http://localhost:8889/eureka/
@RestController
@Configuration
public class InvokerController {
@Bean
@LoadBalanced//让这个实例具有分布式的功能 负载均衡的能力
public RestTemplate getRestTemplate (){
return new RestTemplate();
}
@RequestMapping(value = "/router",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
public String router(){
RestTemplate restTemplate=getRestTemplate();
String json=restTemplate.getForObject("http://first-cloud-provider/person/1",String.class);
return json;
}
}
启动类:
@SpringBootApplication
@EnableDiscoveryClient
public class FirstEkServiceInvokerApplication {
public static void main(String[] args) {
SpringApplication.run(FirstEkServiceInvokerApplication.class, args);
}
}
访问步骤 :
1.先启动两个不同的Eureka服务器。
2.分步启动Eureka服务提供端 输入 8080 8081 端口 防止端口占用。
3.启动Eureka客户端,通过客户端端口
输入 http://localhost:9000/router 即可访问到json数据值
什么环境下开启Eureka的自我保护机制
测试的时候关闭Eureka的自我保护机制,来确保重启服务过程中,注册中心能够及时的发现该服务不可用
生产环境中应该开启保护机制,使得在网络延迟的时候,能够使得存在在服务注册中心的服务任然可用。
在注册中心配置 :false关闭
eureka: server: enable-self-preservation: false eviction-interval-timer-in-ms: 2000
在服务提供端配置:
eureka:
instance:
#Eureka客户端向服务注册中心发送心跳的时间间隔
lease-renewal-interval-in-seconds: 1
#Eureka服务器端在收到最后一次心跳之后的等待时间的时间上限,单位为秒,超过时间就会去掉该服务
lease-expiration-duration-in-seconds: 2