1. Documents officiels
https: // cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.2.2.RELEASE/reference/html/
2. Ajouter la dépendance
< Dépendance > < groupId > org.springframework.boot </ groupId > < artifactId > ressort-boot-démarreur web </ artifactId > </ dépendance > < dépendance > < groupId > org.springframework.cloud </ groupId > < artifactId > ressort nuage-démarreur-netflix-eurêka-client </ artifactId > </ dépendance > < dépendance > <groupId> Org.springframework.cloud </ groupId > < artifactId > ressort nuage-démarreur-openfeign </ artifactId > </ dépendance >
3. Ajouter notes de classe de démarrage
importation org.mybatis.spring.annotation.MapperScan; importation org.springframework.boot.SpringApplication; importation org.springframework.boot.autoconfigure.SpringBootApplication; importation org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication // @MapperScan ( "cn.ytheng.order_service") @EnableFeignClients publique classe OrderServiceApplication { publics statiques vides principaux (String [] args) { SpringApplication.run (OrderServiceApplication. Classe , args); } }
4. Ajouter Feindre Interface
Importation org.springframework.cloud.openfeign.FeignClient; importation org.springframework.web.bind.annotation.GetMapping; importation org.springframework.web.bind.annotation.RequestParam; / ** * * client Marchandises Services * produits de service : appel nom de service, que spring.application.name * * / @FeignClient (nom = "produit-service" ) publique Interface ProductClient { @GetMapping ( "/ API / v1 / produit / la Recherche" ) Chaîne getById (@RequestParam ( " ID « ) int ID); }
5. Ajouter contrôleur
importation cn.theng.order_service.service.ProductClient; importation org.springframework.beans.factory.annotation.Autowired; importation org.springframework.web.bind.annotation.PostMapping; importation org.springframework.web.bind.annotation.RequestMapping; importation org.springframework.web.bind.annotation.RequestParam; importation org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping ( "/ api / v1 / order" ) publique classe ProductOrderController { @Autowired privée ProductClient productClient; @PostMapping ( "/ test2" ) publique test2 (Object @RequestParam ( "product_id") int productId) { produit Chaîne = productClient.getById (productId); retour « succès » ; } }
6. Ajouter application.yml configuration
serveur: Port: 8781 eureka: client: ServiceUrl: defaultZone: http: // localhost: 8761 / eureka / printemps: Application: nom: ordre -service
7. Accès Adresse