美文网首页
springcloud

springcloud

作者: Jaypc | 来源:发表于2020-10-28 10:50 被阅读0次

一.介绍

Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring Cloud并没有重复制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包

二.开始搭建

1.新建一个maven工程,起名cloud
2.在工程下新建一个module,选择springboot,勾上Eureka Server。以便自动导入包,所对应的pom文件中的依赖为:

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

3.在application中加入注解@EnableEurekaServer,申明此处为服务注册中心。


image.png

4.application.properties

server.port=8761
spring.application.name=serve

eureka.instance.hostname=eureka-serve1
eureka.instance.prefer-ip-address=false
eureka.server.enable-self-preservation=false
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://eureka-serve2:8762/eureka

这里为了演示多个注册中心集群,这里新建一个
同样操作新建一个module,端口号改为8762,
eureka.client.service-url.defaultZone=http://eureka-serve2:8761/eureka
两个serve互相注册。假如其中一个服务挂掉,则我们的服务还可用,因为还有第二个节点;在实际应用过程中,不同的注册中心启在不同的服务器上,多个微服务server部署在不同的服务器上,都统一注册中心中管理;
这里在为了方便用域名代替eureka服务的主机,将eureka-serve1和eureka-serve2
对应127.0.0.1,对应在windows下host文件修改
启动服务
5.启动服务,浏览器输入localhost:8761

image.png

目前还没有其他服务注册进来,只有两个注册中心,这里可以看到

三.创建生产者

1.新建一个module,为server-provider

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

2.启动类加@EnableEurekaClient申明为是一个服务生产者


image.png

3.配置文件

server.port=8086
spring.application.name=service-provider

eureka.client.service-url.defaultZone=http://localhost:8761/eureka,http://localhost:8762/eureka

http://localhost:8761/eureka,http://localhost:8762/eureka 代表将当前服务注册到刚才启动的两个注册中心中
4.编写一个测试controller接口

@RestController
@RequestMapping("/person")
public class ProviderController {
    @RequestMapping(value = "/say/{name}",method = RequestMethod.GET)
    public String findName(@PathVariable("name") String name){
        return name;
    }
}

4.启动服务,打开localhost:8761 可以看到,生产者已经注册进来了

image.png
5.浏览器输入http://localhost:8086/person/say/hello
image.png
这是刚才的测试接口

四.创建消费者

1.新建一个module,为service-invoker

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

2.启动类增加@EnableDiscoveryClient代表是消费者
3.配置文件

server.port=9000
spring.application.name=service-invoker

eureka.client.service-url.defaultZone=http://localhost:8761/eureka,http://localhost:8762/eureka

4.引入RestTemplate,编写测试接口
RestTemplate是对http请求调用的一种rest方式

@Autowired
 private RestTemplate restTemplate;
@RequestMapping(value = "/router/{name}",method = RequestMethod.GET)
 public String router(@PathVariable("name") String name){
        String result = restTemplate.getForObject("http://service-provider/person/say/"+name,String.class);
        return result;
  }
@Configuration
public class RestTemplateConfig {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

service-provider就是刚才的提供者的服务
5.启动服务,浏览器输入http://localhost:9000/router/hello
可以看到调用成功,说明通过注册中心调用服务成功

五.引入openFeign进行服务名映射,更加方便

1.引入依赖

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

2.启动类加入@EnableFeignClients(basePackages = "com.example.invoker.service")

@EnableFeignClients(basePackages = "com.example.invoker.service")
@EnableDiscoveryClient
@SpringBootApplication
public class InvokerApplication {

    public static void main(String[] args) {
        SpringApplication.run(InvokerApplication.class, args);
    }

}

3.新建service

@Component
@FeignClient(value = "service-provider",fallback = InvokeServiceFallBackImpl.class)
public interface InvokeService {
    /**
     * say
     * @param name
     * @return
     */
    @GetMapping("/person/say/{name}")
    String say(@PathVariable("name") String name);
}

@FeignClient(value = "service-provider",fallback = InvokeServiceFallBackImpl.class)
value值就是生产者的服务名,fallback指定的类就是服务熔断降级的方法
当然也可以同Hystrix,方法类同;

新建一个类InvokeServiceFallBackImpl.class

@Component
public class InvokeServiceFallBackImpl implements InvokeService {
    @Override
    public String say(String name) {
        return name+":熔断默认方法";
    }
}

实现刚才的service接口,对应的方法就是服务降级的方法,等会可以演示一下
4.在controller中新写一个方法

@RequestMapping("/say/{name}")
    public String say(@PathVariable("name") String name){
        return invokeService.say(name);
    }

启动服务,浏览器输入http://localhost:9000/say/hello
可以看到调用服务成功

image.png
5.现在我们将service-provider这个服务停掉
继续调用浏览器输入http://localhost:9000/say/hello
image.png
看到没!这就是所谓的服务熔断和降级
在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。为了解决这个问题,业界提出了断路器模型。
Netflix开源了Hystrix组件,实现了断路器模式
springcloud还有许多优秀的功能框架整合,比如zuul网关,ribbon负载均衡,config统一配置,bus消息总线等等;

相关文章

网友评论

      本文标题:springcloud

      本文链接:https://www.haomeiwen.com/subject/hxjqvktx.html