7. feign

7. feign

 

 

 7. feign

 

 

 7. feign

 

 

 在项目中整合Feign

1.0 在如下两个模块的pom文件中加入如下依赖(feign项目是之前consumer项目的翻版)

7. feign

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

2.0 在api模块中添加service层

@FeignClient(value="SPRINGCLOUD-PROVIDER-DEPT")
@Service
public interface DeptClientService {
    @GetMapping("/dept/get/{id}")
    public Dept queryById(@PathVariable("id") Long id);
    @GetMapping("/dept/list")
    public List<Dept> queryAll();
    @PostMapping("/dept/add")
    public Boolean addDept(Dept dept);
}

3.0 给出feign模块的完整结构

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>com.wu</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-consumer-dept-feign</artifactId>
    <!-- 实体类+web-->
    <dependencies>
        <dependency>
            <groupId>com.wu</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <!--ribbon-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!--eureka-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
    </dependencies>
</project>

application.yml

#80配置之后localhost可以直接访问
server:
  port: 80
#eureka配置
eureka:
  client:
    register-with-eureka: false #不向eureka注册自己
    service-url: #需求是他随机进这三个注册中心注册
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/

ConfigBean.java

@Configuration//相当于spring中的applicationContext
public class ConfigBean {
    //注册RestTemplate模板
    @Bean
    @LoadBalanced//负载均衡实现了
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

}

Controller

@RestController
public class DeptConsumerController {

    @Autowired
    private DeptClientService service=null;
    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id")Long id){
        return this.service.queryById(id);
    }
    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept){
        return this.service.addDept(dept);
    }
    @RequestMapping("/consumer/dept/list")
    public List<Dept> list(){
        return this.service.queryAll();
    }

}

主启动类

//Ribbon和Eureka整合以后,客户端可以直接调用,不用关心IP地址和端口号
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.wu.springcloud"})
@ComponentScan("com.wu.springcloud")
public class DeptConsumer_feign {
    public static void main(String[] args) {
        SpringApplication.run(DeptConsumer_feign.class,args);
    }
}

 

上一篇:写给Java软件工程师的3条建议,附答案


下一篇:一文搞懂JVM架构:java常用数据结构和基本算法