Start several projects, there are
8761 Eureka server, is the server
microservice-provider-user starts 7900, 7901 and is a service provider
microservice-provider-user2 starts 7902, 7903 and is a service provider
microservice-customer-movie-ribbon starts 8010 and is a service consumer
16.2. Customizing the Ribbon Client
In the project microservice-customer-movie-ribbon
TestConfiguration. Note that it cannot be placed under the package that @ComponentScan of the startup class can scan. ( The startup class can be scanned into the same directory or annotations available under subpackages )
package com.ldgx.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RandomRule; /** * The TestConfiguration has to be @Configuration but take care that it is not in a @ComponentScan for the main application context, * otherwise it will be shared by all the @RibbonClients. If you use @ComponentScan (or @SpringBootApplication) * you need to take steps to avoid it being included (for instance put it in a separate, non-overlapping package, or specify the packages to scan explicitly in the @ComponentScan). * @author Administrator * */ @Configuration public class TestConfiguration { //@Autowired //private IClientConfig config; @Bean public IRule ribbonRule(IClientConfig config) { return new RandomRule();//Random rule } }
Startup class com/ldgx/eshop/MicroserviceCustomerMovie1Application.java added
@RibbonClient(name="microservice-provider-user",configuration = TestConfiguration.class)
Controller
package com.ldgx.eshop.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class UseDemoController { @Autowired private RestTemplate restTemplate; @Autowired private LoadBalancerClient loadBalanceClient; @RequestMapping("/demo") public String useDemo () { return restTemplate.getForObject("http://microservice-provider-user/demo/dd", String.class);//Parameter 1 makes eureka's service-id, which is a virtual host name, you can use eureka.instance.secure. virtual-host-name definition, if not https, use eureka.instance.virtual-host-name } @RequestMapping("/test") public String test() { ServiceInstance instance = this.loadBalanceClient.choose("microservice-provider-user"); System.out.println(instance.getHost() + "," + instance.getPort() +","+instance.getServiceId()); ServiceInstance instance2 = this.loadBalanceClient.choose("microservice-provider-user2"); System.out.println(instance2.getHost() + "," + instance2.getPort() +","+instance2.getServiceId()); return "1"; } }
When accessing the address http://localhost:8010/test on the browser
The project microservice-customer-movie-ribbon prints the result:
IDXZC2FNKSQOYM6,7900,microservice-provider-user IDXZC2FNKSQOYM6,7903,microservice-provider-user2 IDXZC2FNKSQOYM6,7901,microservice-provider-user IDXZC2FNKSQOYM6,7902,microservice-provider-user2 IDXZC2FNKSQOYM6,7901,microservice-provider-user IDXZC2FNKSQOYM6,7903,microservice-provider-user2 IDXZC2FNKSQOYM6,7900,microservice-provider-user IDXZC2FNKSQOYM6,7902,microservice-provider-user2 IDXZC2FNKSQOYM6,7900,microservice-provider-user IDXZC2FNKSQOYM6,7903,microservice-provider-user2 IDXZC2FNKSQOYM6,7901,microservice-provider-user IDXZC2FNKSQOYM6,7902,microservice-provider-user2 IDXZC2FNKSQOYM6,7901,microservice-provider-user IDXZC2FNKSQOYM6,7903,microservice-provider-user2 IDXZC2FNKSQOYM6,7900,microservice-provider-user IDXZC2FNKSQOYM6,7902,microservice-provider-user2 IDXZC2FNKSQOYM6,7901,microservice-provider-user IDXZC2FNKSQOYM6,7903,microservice-provider-user2 IDXZC2FNKSQOYM6,7900,microservice-provider-user IDXZC2FNKSQOYM6,7902,microservice-provider-user2Get the result: microservice-provider-user is random, microservice-provider-user2 is the default rule (polling)
Question: What if you want to move Configuration to another package?
define tags
package com.ldgx.eshop; public @interface ExcludeFromComponent { }
TestConfiguration class added
@ExcludeFromComponentStartup class added
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type=FilterType.ANNOTATION, value=ExcludeFromComponent.class)})//Do not scan the class of the annotation ExcludeFromComponent annotation
16.4 Customizing the Ribbon Client using properties
application.yml, focus
NFLoadBalancerRuleClassName: load balancing algorithm
microservice-provider-user: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
The running results are as above, and it is concluded that microservice-provider-user adopts the random principle, and microservice-provider-user2 adopts the polling principle.
16.5 Using Ribbon with Eureka
in spring.application.name=microservice-provider-userde metadata
eureka.instance.metadata-map.zone=ABC #Metadata that eureka can understand eureka.instance.metadata-map.lilishow=BBC #will not affect the client's metadata
Enter the address in the browser http://localhost:8761/eureka/apps/microservice-provider-user
get
16.6 Example: How to Use Ribbon Without Eureka
ribbon: eureka: enabled: false #If there is eureka under the classpath, how to disable (Disable Eureka use in Ribbon) microservice-provider-user: ribbon: listOfServers: localhost:7900operation result
localhost,7900,microservice-provider-user localhost,7900,microservice-provider-user localhost,7900,microservice-provider-user localhost,7900,microservice-provider-user localhost,7900,microservice-provider-user localhost,7900,microservice-provider-user localhost,7900,microservice-provider-userAs a result, the ribbon load balancing is disabled, and only port 7900 is called