I. About Feigin
Feigin is a templated, declarative HTTP client that feign can simplify HTTP request access by binding annotations to an interface. Of course, we can also customize the custom decoder (XML or JSON format parsing) and error handling when creating feign objects.
Two. Add Springcloud support for feign
Gradle configuration:
Compile (' org.springframework.cloud:spring-cloud-starter-feign ')
View Code
Feigin the most basic methods of use:
1 InterfaceGitHub {2@RequestLine ("Get/repos/{owner}/{repo}/contributors")3List<contributor> Contributors (@Param ("owner") String owner, @Param ("Repo") String repo);4 }5 6 Static classContributor {7 String login;8 intcontributions;9 }Ten One Public Static voidMain (String ... args) { AGitHub GitHub =Feign.builder () -. Decoder (NewGsondecoder ()) -. Target (GitHub.class, "https://api.github.com"); the - //Fetch and print a list of the contributors to this library. -List<contributor> contributors = Github.contributors ("Openfeign", "feign"); - for(contributor contributor:contributors) { +System.out.println (Contributor.login + "(" + contributor.contributions + ")"); - } +}View Code
Feign sends an HTTP request in JSON and XML format:
1 Interfaceloginclient {2 3@RequestLine ("POST/")4@Headers ("Content-type:application/xml")5@Body ("<login \" user_name\ "=\" {user_name}\ "\" password\ "=\" {password}\ "/>")6 voidXML (@Param ("user_name") String user, @Param ("Password") String password);7 8@RequestLine ("POST/")9@Headers ("Content-type:application/json")Ten //JSON curly braces must be escaped! One@Body ("%7b\" user_name\ ": \" {user_name}\ ", \" password\ ": \" {password}\ "%7d") A voidJSON (@Param ("user_name") String user, @Param ("Password") String password); -}View Code
Note the example needs to add support for Gson
Feign send the code for HTTPS to trust all certificates:
1 Finaltrustmanager[] Trustallcerts =Newtrustmanager[]{NewX509trustmanager () {2 @Override3 Public voidcheckclienttrusted (4 java.security.cert.x509certificate[] chain,5 String authtype) {6 }7 8 @Override9 Public voidcheckservertrusted (Ten java.security.cert.x509certificate[] chain, One String authtype) { A } - - @Override the Publicjava.security.cert.x509certificate[] Getacceptedissuers () { - return NULL; - } - }}; + FinalSslcontext Sslcontext = sslcontext.getinstance ("TLSv1"); -Sslcontext.init (NULL, Trustallcerts, + Newjava.security.SecureRandom ()); A //Create an SSL socket factory with our All-trusting manager at FinalSslsocketfactory sslsocketfactory =Sslcontext - . Getsocketfactory (); -Feign.builder (). Client (NewClient.default (Sslsocketfactory, (S, sslsession)true));View Code
Three. Using feign in Springcloud
For example, the registration center has the following services:
1) Configuration of application.yml:
Spring: application: name:demo-Consumereureka: client: service-URL: Defaultzone:http://Localhost:8080/eureka,http://localhost: 8081/ EurekaServer: 8090
View Code
2) Create an interface
1 PackageCom.bdqn.lyrk.consumer.demo.api;2 3 Importorg.springframework.cloud.netflix.feign.FeignClient;4 Importorg.springframework.web.bind.annotation.RequestMapping;5 6@FeignClient ("Demo")7 Public InterfaceDemoconfigservice {8 9@RequestMapping ("/demo.do")Ten String Demoservice (); One}View Code
Note the annotations on the interface: the parameters in the @FeignClient ("demo") annotations are the service names registered in the Eureka
3) Write the Startup class:
PackageCom.bdqn.lyrk.consumer.demo;ImportCom.bdqn.lyrk.consumer.demo.api.DemoConfigService;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.cloud.client.discovery.EnableDiscoveryClient;Importorg.springframework.cloud.netflix.feign.EnableFeignClients;Importorg.springframework.context.configurableapplicationcontext;@ Enablediscoveryclient@enablefeignclients@springbootapplication Public classDemoconsumerprovider { Public Static voidMain (string[] args) {Configurableapplicationcontext ApplicationContext= Springapplication.run (Democonsumerprovider.class, args); Democonfigservice Democonfigservice= Applicationcontext.getbean (Democonfigservice.class); System.out.println (Democonfigservice.demoservice ()); }}View Code
Note Add @enablefeignclients on the startup class to turn on the feign feature
Post-run output:
At this point we can see a lot of code about our access to the service using the feign client
The feign of Springcloud learning