springcloud全家桶个人博客系统(三)用户消费组件ConsumerFeign

一、简介

本组件提供消费者端的负载均衡。

二、pom文件

    
        
            com.sustly
            blog_api
            ${project.version}
        
        
            net.java.dev.jna
            jna
            4.1.0
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
        
            org.springframework.cloud
            spring-cloud-starter-ribbon
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework
            springloaded
        
        
            org.springframework.boot
            spring-boot-devtools
        
        
            org.springframework.cloud
            spring-cloud-starter-feign
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            repackage
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                
            
        
    

三、主配置类

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.sustly"})
public class BlogConsumerFeign {
    public static void main(String[] args) {
        SpringApplication.run(BlogConsumerFeign.class, args);
    }
}

四、application.properties

server.port=9001

spring.application.name=blog-consumer-feign

eureka.client.register-with-eureka=true
eureka.client.service-url.defaultZone=http://localhost:7001/eureka/
feign.hystrix.enabled=true

eureka.instance.instance-id=blog-consumer-feign
eureka.instance.prefer-ip-address=true

info.app.name=blog-consumer-feign
info.company.name=www.sustly.xyz
info.build.artifactId=${project.artifactId}
info.build.version=${project.version}

五、FeignConfig

@Configuration
public class FeignConfig {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

    @Bean
    public IRule myRule(){
        //循环算法 默认
        return new RoundRobinRule();
    }
}

注意:该配置类必须配置,负载均衡才能起效果。 

五、controller

@RestController
public class ConsumerAdminController {
    private final UserClientService userService;

    @Autowired
    public ConsumerAdminController(UserClientService userService) {
        this.userService = userService;
    }


    @PostMapping("/register")
    public ResponseMsg register(@RequestBody User user){
        return userService.register(user);
    }

    @PostMapping("/login")
    public ResponseMsg login(@RequestBody User user){
        return userService.login(user);
    }
}

本controller中注入的service是上一篇文章中的。 

六、我的GitHub地址

https://github.com/sustly/blog_vue_server

注意:master分支才是springcloud版本

你可能感兴趣的:(springcloud)