springcloud feign消费者

springcloud feign消费者

1. pom依赖

父pom dependencyManagement

  <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>
                    org.springframework.cloud
                </groupId>
                <artifactId>
                    spring-cloud-dependencies
                </artifactId>
                <version>
                    Greenwich.RELEASE
                </version>
                <type>
                    pom
                </type>
                <scope>
                    import
                </scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

feign客户端 pom

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

2.application.properties 文件

server.port=8091
spring.application.name=cloud4
#注册服务的地址
eureka.client.service-url.defaultZone=http://localhost:8080/eureka/

3.代码部分

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class SpringCloud4Application {

    public static void main(String[] args) {

        SpringApplication.run(SpringCloud4Application.class,args);
    }
}
@Configuration
public class FeignConfig {

    @Bean
    public Retryer feignRetryer() {
        return new Retryer.Default(100, SECONDS.toMillis(1), 5);
    }

}

其中value值为服务提供者项目名, @GetMapping值为服务提供者接口地址

@FeignClient(value = "cloud1", configuration = FeignConfig.class)
public interface FeignService {

    @GetMapping("test/a")
    public String a();
}


@Service
public class TestService {

    @Autowired
    FeignService feignService;

    public String a(){

        return feignService.a();
    }
}
@RestController
@RequestMapping("test")
public class TestController {

    @Autowired
    TestService testService;

    @GetMapping("a")
    public ResponseEntity a(){

        return ResponseEntity.ok(testService.a());
    }
}

发布了43 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43866295/article/details/87947332
今日推荐