springcloud全家桶个人博客系统(二)用户服务组件AdminProviderHystrix和文章服务组件ArticleProviderHystrix

一、简介

用户服务主要是提供用户注册、登陆等功能的接口。本组件基于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版本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值