一、简介
用户服务主要是提供用户注册、登陆等功能的接口。本组件基于hystrix的容灾处理。
文章服务组件主要提供文章增删改查功能接口。
本文以用户服务组件为例,文章服务组件与此组成大致类似,不在赘述。
二、pom文件
<dependencies>
<dependency>
<groupId>com.sustly</groupId>
<artifactId>blog_api</artifactId>
<version>${project.version}</version>
</dependency>
<!-- actuator监控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 将微服务provider注册进eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</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-test</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
三、主配置类
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@MapperScan(basePackages = "com.systly.dao")
public class BlogAdminProviderHystrix {
public static void main(String[] args) {
SpringApplication.run(BlogAdminProviderHystrix.class, args);
}
}
注意:要开启eureka客户端和hystrix
四、application.properties
server.port=8001
spring.application.name=blog-admin-provider-hystrix
mybatis.configuration.map-underscore-to-camel-case=true
spring.aop.auto=true
spring.aop.proxy-target-class=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/blog?useSSL=false
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
eureka.client.service-url.defaultZone=http://localhost:7001/eureka/
#服务名称
eureka.instance.instance-id=blog-admin-provider-hystrix
#显示地址
eureka.instance.prefer-ip-address=true
#在eureka中显示的名称
info.app.name=blog-admin-provider-hystrix
info.company.name=www.sustly.xyz
info.build.artifactId=${project.artifactId}
info.build.version=${project.version}
五、feign和hystrix容灾处理
5.1 feign
@FeignClient(value = "BLOG-ADMIN-PROVIDER-HYSTRIX", fallbackFactory=UserServiceClientFallBackFactory.class)
public interface UserClientService {
@PostMapping("/register")
ResponseMsg register(@RequestBody User user);
@PostMapping("/login")
ResponseMsg login(@RequestBody User user);
}
6.2 hystrix容灾
@Component
public class UserServiceClientFallBackFactory implements FallbackFactory<UserClientService> {
@Override
public UserClientService create(Throwable throwable) {
return new UserClientService() {
@Override
public ResponseMsg register(User user) {
return ResponseMsg.onFail("该用户名已被占用!");
}
@Override
public ResponseMsg login(User user) {
return ResponseMsg.onFail("用户不存在!");
}
};
}
}
六、本系统GitHub地址
https://github.com/sustly/blog_vue_server
注意:master分支才是springcloud版本