springcloud基本使用

本文详细介绍SpringCloud微服务框架的搭建与使用,包括Eureka服务注册中心的创建、微服务间调用、断路器Hystrix的配置、Feign客户端的集成及Zuul网关的设置,为开发者提供全面的微服务解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Springcloud总结文档

  1. springcloud框架是基于springboot的微服务框架,如果系统较大建议使用springcloud框架进行分布式开发。
  2. 微服务要使用的话,要创建管理各个微服务的eureka(服务注册中心),把微服务注册到服务注册中,各个微服务就可以进行相互调用。

2.1创建服务注册中心

需要添加的依赖:

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

        启动类添加注解:

@EnableEurekaServer

        Application.yml文件需要添加配置:

eureka:
     instance:
              hostname: localhost
     client: #not a client,don't register with yourself
         registerWithEureka: false
              fetch-Registry: false

   2.2创建一个微服务并且注册到服务注册中心

       需要添加的依赖:

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

       需要添加的注解:

@EnableDiscoveryClient

       Application.yml配置文件需要的配置:

eureka:
     client:
         serviceUrl:
             defaultZone: http://localhost:1527/eureka/
        fetch-registry: true
        register-with-eureka: true

 2.3通过ip端口号方式微服务之间进行相互调用:

  1. 在启动类先注册一个bean

@Bean
@LoadBalanced
public RestTemplate restTemplate(){
        return new RestTemplate();
}

  1. 在需要调用微服务的类中通过依赖注入的restTemlate对象调用方法进行访问,并通@LoadBalanced注解进行负载均衡:

@Autowired
private RestTemplate restTemplate;

private String serverDatePath="http://localhost:1528/dept/"//服务提供者的接口

public Dept getClientDept(@PathVariable Integer id) {
     System.out.println(serverDatePath + id);
     return this.restTemplate.getForObject(serverDatePath + id, Dept.class);

     // return this.restTemplate.postForEntity(serverDatePath+id,Dept.class);
}

  1. 上面的这种通过ip端口直接访问的方法不太妥,如果微服务服务器ip端口号经常变更项目维护比较麻烦。下面这个实例解决ip端口经常变更

     每当开发一个微服务现在配置文件里面添加一个spring.application.name:服务名,调用服务时直接通过http://服务名/接口名,进行微服务调用。

3.断路器(Hystrix):

 


由上图可看出,hystrix模式是服务消费者调用服务提供者时,服务提供者出现了问题,那么hystrix进行操作,调用hystrix指定的方法进行处理。

使用hystrix需要添加的依赖:

<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>

     在启动类添加注解:

@EnableCircuitBreaker

     在类中使用方法:

@HystrixCommand(fallbackMethod = "fallbackGetDeptById",commandProperties = {
                 @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")
          })
public Dept getClientDept(@PathVariable Integer id) {
          System.out.println(serverDatePath + id);
          return this.restTemplate.getForObject(serverDatePath + id, Dept.class);
}
public Dept fallbackGetDeptById(Integer id){
     Dept dept=new Dept();
     dept.setDeptno(0);
     return dept;
}

4.feign访问服务配合hystrix一起使用。

需要引入的依赖:
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
启动类需要添加的注解:

@EnableFeignClients

所用方法:

  1. 创建一个java接口

2.在接口上面添加注解@FeignClient

    3.在接口中添加@RequestMapping注解(目前也只支持该注解)

1 实例:

         @FeignClient(name="需要访问的服务名",configuration= FooConfiguration.class)
public interface DeptFeign {

     @RequestMapping(value = "/test",method = RequestMethod.GET)
     public String test();
   
     @RequestMapping(value = "/dept/{id}",method = RequestMethod.GET)
     public java.util.Optional<Dept> getDeptById(@PathVariable("id") Integer id);

@Configuration
public class FooConfiguration {
     @Bean
     Logger.Level feignLoggerLevel() {
         return Logger.Level.FULL;
     }
}

Feign配合hystrix一起使用

方式一:

@FeignClient(name="userserverclient",fallback = HystrixClientFallback.class)
public interface DeptFeign {
     @RequestMapping(value = "/test",method = RequestMethod.GET)
     public String test();
     @RequestMapping(value = "/dept/{id}",method = RequestMethod.GET)
     public java.util.Optional<Dept> getDeptById(@PathVariable("id") Integer id);
}

@Component
public class HystrixClientFallback implements  DeptFeign {
     @Override
     public String test() {
         return "服务提供者异常:^^";
     }

     @Override
     public Optional<Dept> getDeptById(Integer id) {
         Dept dept =new Dept();
         dept.setDeptno(-1);
         Optional<Dept> depts= Optional.ofNullable(dept);
         return depts;
     }
}

方式二:

   @FeignClient(name="userserverclient",fallbackFactory =   HystrixClientFallbackFactory.class)
public interface DeptFeign {
     @RequestMapping(value = "/test",method = RequestMethod.GET)
     public String test();
     @RequestMapping(value = "/dept/{id}",method = RequestMethod.GET)
     public java.util.Optional<Dept> getDeptById(@PathVariable("id") Integer id);
}

@Component
public class HystrixClientFallbackFactory implements FallbackFactory<DeptFeign> {
     @Override
     public DeptFeign create(Throwable cause) {
         return new DeptFeign() {
             @Override
             public String test() {
                 return "服务提供者挂了^^";
             }

             @Override
             public Optional<Dept> getDeptById(Integer id) {
                 Dept dept=new Dept();
                 dept.setDeptno(-1);
                 Optional<Dept> depts= Optional.ofNullable(dept);
                 return depts;
             }
         };
     }

5.Zuul是Netflix的基于JVM的路由器和服务器端负载均衡器。

    1. 需要添加得依赖:

<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>

启动类需要添加得注解:

@EnableZuulProxy

Application.yml文件需要添加得配置:

zuul:
   #ignoredServices: '*'
   #routes:
     #userserverclient: /deptserver/**
     routes:
           userserverclient:
               path: /deptserver/**
               serviceId: userserverclient
#实现服务器端负载均衡
ribbon:
     eureka:
         enabled: false
userserverclient:
     ribbon:
         listOfServers: http://localhost:1529

更多使用方法请访问spring官网:   http://cloud.spring.io/spring-cloud-static/Finchley.SR2/single/spring-cloud.html

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值