服务消费者
在消费者项目中引入 autumn-base ,并且在启动类扫描相应的包,接口处可以直接 @Autowired 自动注入上面消费者的 UserFeignClient
- 代码流程
- 新建一个 maven 项目,名为 summer ,pom 中引入
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>lindian</groupId>
<artifactId>autumn-base</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>lindian</groupId>
<artifactId>moon</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
- 新建 主类 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 -------------------");
}
}
- 新建 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;
}
}
- 项目配置
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
-
启动步骤
- 编译器直接执行 main 方法
- 浏览器 http://localhost:9082/user/get?id=3 查看各个项目打印的日志
链路监控
链路监控使用的是很容易和 SpringCloud 集成的 zipkin ,在上面的生产者和消费者中,均已经配置了zipkin,我们可以使用 zipkin 清楚的看到一个接口的流转过程,以便于清楚的定位哪里会比较耗时,从而进一步知道如何去优化。
- 启动步骤
- 在网上下载一个 zipkin.jar
- java -jar zipkin.jar
- 浏览器打开 http://localhost:9411
优化
我们公司之前的有些业务逻辑是写在一个公共的 jar 中,封装了一些业务逻辑,在别的消费者项目中使用,为了使项目平滑的从 WebService 过度到 SpringCloud ,所以这一点需要解决。
- 代码流程
- 新建一个 maven 项目,名为 summer ,pom 中引入
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.6.RELEASE</version>
</dependency>
<dependency>
<groupId>lindian</groupId>
<artifactId>autumn-base</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
- 封装一个 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> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
// 通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
- 封装我们的业务逻辑
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);
}
}
- 这样我们在项目中就可以直接使用 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
网友评论