一、spring cloud简介
spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。另外说明spring cloud是基于springboot的,所以需要开发中对springboot有一定的了解,如果不了解的话可以看这篇文章:2小时学会springboot。另外对于“微服务架构” 不了解的话,可以通过搜索引擎搜索“微服务架构”了解下。
二、创建服务注册中心和服务提供者
在这里,我们需要用的的组件上Spring Cloud Netflix的Eureka ,eureka是一个服务注册和发现模块。
2.1 首先创建一个maven主工程。
2.2 然后创建个model工程作为服务注册中心,即Eureka Server
2.2.1 创建服务注册中心时候选择依赖为cloud discovery->eureka server
2.2.2 创建完后的工程的pom.xml文件如下:
<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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.2.3 yml文件
eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。
#1.eureka.client.registerWithEureka=true #是否将自身注册
#2.eureka.client.fetchRegistry=false #如果为true,启动时报警.
代码段 小部件#通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.
server:
port: 8086
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2.2.4 ServerApplication
@EnableEurekaServer
@SpringBootApplication
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
2.2.5 启动 http://localhost:8086
2.3 然后创建另一个model作为Eureka Client
当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。
2.3.1 引入eureka server和 web,pom依赖如下
<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2.3.2 通过注解@EnableEurekaClient 表明自己是一个eurekaclient.
@SpringBootApplication
@EnableEurekaClient
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
2.3.3 application.yml配置文件如下:
注意:需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8762
spring:
application:
name: service-hi
2.3.4 controller
@Controller
@RequestMapping("/cloud")
public class MyController {
@Value("${server.port}")
String port;
@RequestMapping("/hi")
@ResponseBody
public String home(@RequestParam String name) {
return "hi " + name + ",i am from port:" + port;
}
}
三:定义: 服务消费者
- 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。在这一篇文章首先讲解下基于ribbon+rest。
- ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。Feign默认集成了ribbon。
3.1 ribbon+restTemplate
3.1.1 准备:
- 启动之前的服务注册者和两次服务提供者service-hi(相当于一个小集群)
3.1.2 构建一个modle,依赖eureka,ribbon,web
<dependencies>
<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>
<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>
3.1.3 SpringBootApplication
- @EnableDiscoveryClient向服务中心注册;
- 并且向程序的ioc注入一个bean: restTemplate;
- 并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能
@SpringBootApplication
@EnableDiscoveryClient
public class RestRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RestRibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
3.1.4 applicatin.yml
eureka:
client:
service-url:
defaultZone: http://localhost:8086/eureka/
spring:
application:
name: service-robbin
server:
port: 8088
3.1.5 helloService.java
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hiService(String name) {
return restTemplate.getForObject("http://SERVICE-HI/cloud/hi?name="+name,String.class);
}
}
3.1.6 Controller.java
@Controller
@RequestMapping("/cloud")
public class MyController {
@Autowired
HelloService helloService;
@RequestMapping("/hi")
@ResponseBody
public String hiService(String name) {
return helloService.hiService(name);
}
}
3.2 Fegin
- Fegin集成了ribbon和Hystrix断路器
- Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。
3.2.1 准备:
- 启动之前的服务注册者和两次服务提供者service-hi(相当于一个小集群)
3.2.2 创建一个model,依赖eureka,web,fegin
<dependencies>
<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-openfeign</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>
3.2.3 application.yml
server:
port: 8090
eureka:
client:
service-url:
defaultZone: http://localhost:8086/eureka/
spring:
application:
name: service-feign
3.2.4 FeignConsumerApplication
- @EnableFeignClients注解开启Feign的功能
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(FeignConsumerApplication.class, args);
}
}
3.2.5 定义一个feign接口
- 通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了service-hi服务的“/hi”接口
@FeignClient(value = "service-hi")
public interface FeignSayHiService {
@RequestMapping("/cloud/hi")
public String sayHi(@RequestParam(name = "name") String name);
}
3.2.6 controller
@Controller
@RequestMapping("/cloud")
public class FeignController {
@Autowired
FeignSayHiService feignSayHiService;
@RequestMapping("hi")
@ResponseBody
public String sayHello(String name) {
return feignSayHiService.sayHi(name);
}
}