SpringCloud 实践(下)

服务消费者

在消费者项目中引入 autumn-base ,并且在启动类扫描相应的包,接口处可以直接 @Autowired 自动注入上面消费者的 UserFeignClient

  • 代码流程
  1. 新建一个 maven 项目,名为 summer ,pom 中引入

    

        
            org.projectlombok
            lombok
            1.16.18
        

        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.cloud
            spring-cloud-starter-hystrix
        

        
            org.springframework.cloud
            spring-cloud-starter-eureka
        

        
            org.springframework.cloud
            spring-cloud-starter-zuul
        

        
            org.springframework.cloud
            spring-cloud-starter-feign
        

        
            org.springframework.cloud
            spring-cloud-starter-sleuth
        

        
            org.springframework.cloud
            spring-cloud-starter-zipkin
        

        
            lindian
            autumn-base
            1.0-SNAPSHOT
        

        
            lindian
            moon
            1.0-SNAPSHOT
        

    

  1. 新建 主类 App.class

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
@EnableCircuitBreaker
@EnableFeignClients(basePackages = "base.autumn.feign")
public class App {

    public static void main(String[] args) {
        System.out.println("------------------- start -------------------");
        SpringApplication.run(App.class, args);
        System.out.println("-------------------  end  -------------------");
    }

}

  1. 新建 Controller
@RequestMapping(value = "/user")
@RestController
public class UserController {

    @Autowired
    private UserFeignClient userFeignClient;

    @RequestMapping(value = "/get", method = {RequestMethod.GET}, produces = {"application/json;charset=UTF-8"})
    public SummerResponse GetUser(@Validated Integer id) {
        AutumnResponse autumnResponse = userFeignClient.findById(id);
        SummerResponse response = new SummerResponse();
        response.setUser(Convert.convertUser(autumnResponse.getUser()));
        System.out.println(response);
        return response;
    }


    private User getUser(String name) {
        UserVO vo = new UserVO();
        vo.setName(name);
        User user = Convert.convertUser(vo);
        return user;
    }

}
  1. 项目配置
app:
    name: summer

server:
    port: 9082

eureka:
  client:
    service-url:
       defaultZone: http://localhost:9080/eureka/,http://localhost:9070/eureka/
  instance:
    prefer-ip-address: true

spring:
  application:
    name: summer
  sleuth:
    sampler:
      percentage: 1.0
  zipkin:
    base-url: http://localhost:9411

feign:
  hystrix:
     enabled: true

  • 启动步骤

    1. 编译器直接执行 main 方法
    2. 浏览器 http://localhost:9082/user/get?id=3 查看各个项目打印的日志

链路监控

链路监控使用的是很容易和 SpringCloud 集成的 zipkin ,在上面的生产者和消费者中,均已经配置了zipkin,我们可以使用 zipkin 清楚的看到一个接口的流转过程,以便于清楚的定位哪里会比较耗时,从而进一步知道如何去优化。

  • 启动步骤
  1. 在网上下载一个 zipkin.jar
  2. java -jar zipkin.jar
  3. 浏览器打开 http://localhost:9411

优化

我们公司之前的有些业务逻辑是写在一个公共的 jar 中,封装了一些业务逻辑,在别的消费者项目中使用,为了使项目平滑的从 WebService 过度到 SpringCloud ,所以这一点需要解决。

  • 代码流程
  1. 新建一个 maven 项目,名为 summer ,pom 中引入



       
           org.projectlombok
           lombok
           1.16.18
       

       
           org.springframework.boot
           spring-boot-starter
           1.5.6.RELEASE
       

       
           lindian
           autumn-base
           1.0-SNAPSHOT
       

   

  1. 封装一个 SpringUtil.class ,可以随意的获取 Spring 的 Bean

public class SpringUtil implements ApplicationContextAware {

   private static ApplicationContext context;

   @Override
   public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
       if(context == null) {
           context = applicationContext;
       }
   }

   // 获取applicationContext
   public static ApplicationContext getApplicationContext() {
       return context;
   }

   // 通过name获取 Bean.
   public static Object getBean(String name) {
       return getApplicationContext().getBean(name);
   }

   // 通过class获取Bean.
   public static  T getBean(Class clazz) {
       return getApplicationContext().getBean(clazz);
   }

   // 通过name,以及Clazz返回指定的Bean
   public static  T getBean(String name, Class clazz) {
       return getApplicationContext().getBean(name, clazz);
   }
}


  1. 封装我们的业务逻辑

public class UserExecutor {

  private static class Holder {
      private static final UserExecutor singleton = new UserExecutor();
  }

  public static UserExecutor getInstance() {
      return UserExecutor.Holder.singleton;
  }

  private UserExecutor() {
      userFeignClient = SpringUtil.getBean(UserFeignClient.class);
  }

  private UserFeignClient userFeignClient;

  public AutumnResponse addUser(User user) {
      return userFeignClient.addUser(user);
  }

}

  1. 这样我们在项目中就可以直接使用 UserExecutor 里封装的业务逻辑

  @RequestMapping(value = "/add", method = {RequestMethod.GET}, produces = {"application/json;charset=UTF-8"})
  public SummerResponse AddUser(@Validated String name) {
      UserExecutor.getInstance().addUser(getUser(name));
      return new SummerResponse();
  }

  private User getUser(String name) {
      UserVO vo = new UserVO();
      vo.setName(name);
      User user = Convert.convertUser(vo);
      return user;
  }

尾记

至此,SpringCloud 的一个小雏形就搭建好了,出于一个可使用的状态。
本文代码 https://github.com/coderCjm/winter

SpringCloud 实践(上)

你可能感兴趣的:(SpringCloud 实践(下))