一、Spring Cloud介绍
如果做开发的现在说还没听过微服务,估计要失业了~。微服务中有很多生态,国内DUBBO框架用的较多,相对来说海外用Spring Cloud较多,不过近来Spring Cloud在国内普及程度越来越高,很多中小互联网公司都开始大量使用Spring Cloud。
Spring Cloud用到许多技术,其实好多技术是已经开发好的,Spring Cloud只不过拿来集成,大体来说包含以下:
1、Eureka注册中心
2、客户端负载均衡Ribbon
3、声明式REST客户端Feign
4、熔断降级Hystrix
5、Api网关Zuul
当然还有其它的东西,主要上面这些,接下来会分篇讲述。
二、Eureka介绍
Eureka准确地说是Netfix Eureka,是Netfix开源的在AWS环境下的服务注册中心,即用来实现服务注册和发现的,实现是基于Http协议,项目地址:
https://github.com/Netflix/eureka
Spring Cloud Eureka是Spring Cloud基于Netfix Eureka封装的服务注册中心,同样他的实现也是基于Http的,并且提供Java客户端组件,可以非常快速方便地完成服务注册和发现功能的开发。
Spring Cloud Eureka由以下几部分组成:
1、Eureka注册中心
提供服务注册和发现功能
2、Eureka 服务提供者
服务提供者将自己注册到上面的注册中心,提供服务供服务消费者调用,服务提供者和服务消费者通过Http协议通信
3、Eureka服务消费者
从Eureka注册中心拉取服务提供者的信息,然后通过Http协议调用服务提供者
概念大体上和Dubbo是一样的。
三、Eureka Demo编写
接下来用Spring Boot编写一个Eureka的Demo:
服务提供者提供一个Hello的服务,即访问http://ip:port/hello/hello的时候输出Hello world。
1、Eureka注册中心
1.1 先引入依赖:
<dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR2</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency>
1.2、配置
在application.properties中配置:
spring.application.name=eureka-serverserver.port=8800//不注册自己eureka.client.register-with-eureka=false//不拉取服务eureka.client.fetch-registry=false
比较重要的配置是eureka.client.register-with-eureka,因为这个是作为注册中心启动的,所以不能注册自己;
同样eureka.client.register-with-eureka也设置为false,因为不用调用服务。
1.3、编写启动类
@EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication{public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}}
即加上@EnableEurekaServer注解就可以了。
2、编写服务提供者
2.1 引入依赖
这个同上,就不贴了
2.2 创建启动类
@EnableDiscoveryClient@SpringBootApplicationPublic class HelloServiceApplication {public static void main(String[] args) {SpringApplication.run(HelloServiceApplication.class, args);}}
这里开启了 @EnableDiscoveryClient 注解表明当前服务是一个Eureka客户端。
2.3配置
在application.properties中配置:
spring.application.name=hello-serviceserver.port=9001eureka.client.serviceUrl.defaultZone=http://localhost:8800/eureka/
比较重要的配置为eureka.client.serviceUrl.defaultZone,这个配置Eureka注册中心的地址,即第1步配置的IP和端口。
spring.application.name也比较重要,后面服务消费者调用提供者就是通过这个加以区分的。
2.4 编写服务
@RequestMapping("/hello")@RestControllerpublic class HelloController {@GetMapping("/hello")public String hello() {return "Hello world";}}
启动服务访问 http://localhost:9001/hello/hello,如果输出Hello world就表示正常启动了。
3、编写服务消费者
3.1 引入依赖
同上
3.2 配置
在application.properties中配置:
spring.application.name=hello-client-serviceserver.port=9002eureka.client.serviceUrl.defaultZone=http://localhost:8800/eureka/
注意配置对 eureka.client.serviceUrl.defaultZone 这个是注册中心的地址.
3.3 配置RestTemplate
前面说过Eureka中服务消费者通过Http协议调用服务提供者的,因此需要一个Http访问工具,Spring Cloud中推荐用RestTemplate,代码如下:
@Configurationpublic class BeanConfiguration {@Bean@LoadBalancedpublic RestTemplate getRestTemplate() {return new RestTemplate();}}
注意这里加了 @LoadBalanced 表示启用负载均衡功能,即Ribbon,这个后面再讲。
3.4 编写访问代码
@RequestMapping("/demo")@RestControllerpublic class DemoController {@Autowiredprivate RestTemplate restTemplate;@GetMapping("/hello")public String Hello() {return restTemplate.getForObject("http://hello-service/hello/hello", String.class);}}
然后我们访问 http://localhost:9002/demo/hello 就可以看到Hello world了。
这里我们访问服务提供者的格式为:
http://{application name}
因为第2步注册的name为hello-service:
spring.application.name=hello-service
所以这里要和上面保持一致,Eureka会在访问时进行拦截,动态地将服务名换成实际IP地址。




