SpringCloud OpenFeign Demo

项目结构如下

其中API中主要放置了需要暴露的接口,service则通过RequestMapping的方式提供了一些Controller。在client中,通过feign的方式来调用这些controller。

主POM



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.5.RELEASE
        
    
    com.bxoon
    demo-rpc
    pom
    1.0-SNAPSHOT
    
        demo-rpc-api
        demo-rpc-service
        demo-rpc-client
    

    
        8
        8
        Hoxton.SR8
        2.2.3.RELEASE
        1.2.75
    

    
        
        
            org.projectlombok
            lombok
        
        
        
            com.alibaba
            fastjson
            ${fastjson.version}
        
    

    
        
            
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
            
            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                ${spring-cloud-alibaba.version}
                pom
                import
            
        
    



api 代码

pom



    
        demo-rpc
        com.bxoon
        1.0-SNAPSHOT
    
    4.0.0

    demo-rpc-api
    jar
    
        8
        8
    

    
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
    



一个接口

@FeignClient(value = "demo-rpc-service")
@Component
public interface SayHello {
    @GetMapping(value = "/sayHello")
    String sayHello();
}

service 代码



    
        demo-rpc
        com.bxoon
        1.0-SNAPSHOT
    
    4.0.0

    demo-rpc-service

    jar

    
        8
        8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            com.bxoon
            demo-rpc-api
            1.0-SNAPSHOT
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


application.yml

server:
  port: 12080
eureka:
  client:
    serviceUrl:
      defaultZone: http://${EUREKA_HOST:192.168.7.135}:${EUREKA_PORT:8761}/eureka/
spring:
  application:
    name: demo-rpc-service

Controller

@RestController
public class SayHelloController {
    @GetMapping(value = "/sayHello")
    public String sayHello() {
        return "hello";
    }
}

Client代码

pom



    
        demo-rpc
        com.bxoon
        1.0-SNAPSHOT
    
    4.0.0

    demo-rpc-client

    jar

    
        8
        8
    

    
        
            com.bxoon
            demo-rpc-api
            1.0-SNAPSHOT
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
    


application.yml

server:
  port: 11080
eureka:
  client:
    serviceUrl:
      defaultZone: http://${EUREKA_HOST:192.168.7.135}:${EUREKA_PORT:8761}/eureka/
spring:
  application:
    name: demo-rpc-client

启动类需要加@EnableFeignClients注解

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

Controller

@RestController
@RequiredArgsConstructor
public class SayHelloController {

    private final SayHello sayHello;

    @RequestMapping("sayHello")
    String testSayHello(){
        return sayHello.sayHello();
    }
}

测试

访问如下地址

localhost:11080/sayHello

页面输出hello说明成功

其他

项目省略了eureka的部署过程,这个过程请自行百度。

你可能感兴趣的:(SpringCloud OpenFeign Demo)