Spring-cloud:eureka: Ribbon Load Balancer Custom configuration (ii)
With the default configuration is basically the polling interface, now we switch to custom configuration, while supporting: polling, random interface read
Preparatory work:
1.eureka Service
2. Two User services: Project name: Spring-cloud-user interface: 7900/7901
3. Two User services: Project name: Spring-cloud-user2 interface: 8800/8801
4.movie service, read/call user information interface
Eureka start file Add @enableeurekaserver annotations
@EnableEurekaServer @springbootapplicationpublic class Eurekaapplication {public static void main (string[] args) { Springapplication.run (Eurekaapplication.class, args);}}
User Service:
Spring-cloud-user configuration file:
#portserver. port=7901/7900#dataspring.jpa.generate-ddl=falsespring.jpa.show-sql= Truespring.jpa.hibernate.ddl-auto=nonespring.datasource.platform=h2spring.datasource.schema=classpath: schema.sqlspring.datasource.data=classpath:data.sqlspring.application.name=spring-cloud-user# Loglogging.level.root=infologging.level.org.org.hibernate= Infologging.level.org.hibernate.type.descriptor.sql.basicbinder= tracelogging.level.org.hibernate.type.descriptor.sql.basicextractor=tracelogging.level.com.muyang=debug# Eurekaeureka.client.serviceurl.defaultzone=http://localhost:8761/eurekaeureka.instance.preferipaddress=true
Spring-cloud-user start file Add @enableeurekaclient annotations
@EnableEurekaClient @springbootapplicationpublic class Bootuserapplication {public static void main (string[] args) { Springapplication.run (Bootuserapplication.class, args);}}
Spring-cloud-user's Interface file code
@RestControllerpublic class Usercontroller {@Autowiredprivate userrepository userrepository; @GetMapping ("/simple/{ ID} ") public User FindByID (@PathVariable Long ID) {return userrepository.findone (ID);}}
Spring-cloud-user2 configuration As above, please note the corresponding port number modification.
Movie Service:
Create a new Testconfiguration.java configuration file with the startup file directory sibling
Then create a new @interface excludefromcomponentscan annotation file as the Componetscan annotation for Testconfiguration.java
Excludefromcomponentscan.java
Public @interface Excludefromcomponentscan {}
Testconfiguration.java configuration file
Introduction of Componetscan, and random interface reads
@Configuration @excludefromcomponentscanpublic class Testconfiguration {//@Autowired//iclientconfig config;// Random @beanpublic IRule Ribbonirule () {return new Randomrule ();}}
Application.java Startup file
Then start the file join, @EnableEurekaClient annotations,
Also set Spring-cloud-user interface to random read: @RibbonClient (name = "Spring-cloud-user", configuration = Testconfiguration.class)
Exclude inappropriate component types: excludefilters: Specifies a type that is not suitable for component scanning
@ComponentScan (excludefilters = {@ComponentScan. Filter (type = filtertype.annotation, value = Excludefromcomponentscan.class)})
@EnableEurekaClient @springbootapplication@ribbonclient (name = "Spring-boot-user", configuration = Testconfiguration.class) @ComponentScan (excludefilters = {@ComponentScan. Filter (type = filtertype.annotation, value = Excludefromcomponentscan.class)}) public class application {@Bean @loadbalancedpublic resttemplate resttemplate () { return new Resttemplate ();} public static void Main (string[] args) {Springapplication.run (application.class, args);}}
Moviecontroller.java Documentation
Take/test as an example, Spring-cloud-user is a random read interface, SPRING-CLOUD-USER2 bit polling read interface
@RestControllerpublic class Moviecontroller {@Autowiredprivate resttemplate resttemplate;@ Autowiredloadbalancerclient loadbalancerclient; @GetMapping ("/movie/{id}") Public User FindByID (@PathVariable Long ID ) {//http://localhost:7900/simple/return resttemplate.getforobject ("http://spring-boot-user/simple/" + ID, User.class);} @GetMapping ("/test") public String TestUser () {serviceinstance serviceinstance = This.loadBalancerClient.choose (" Spring-boot-user "); System.out.println ("111:" +serviceinstance.getserviceid () + ":" + serviceinstance.gethost () + ":" + Serviceinstance.getport ()); Serviceinstance ServiceInstance2 = This.loadBalancerClient.choose ("Spring-boot-user2"); System.out.println ("222:" + serviceinstance2.getserviceid () + ":" + serviceinstance2.gethost () + ":" + Serviceinstance2.getport ()); return "1";}}
Spring-cloud:eureka: Ribbon Load Balancer Custom configuration (ii)