I. Overview
In MicroServices, the consumers of our services need to invoke the services provided by the provider, since we have registered the services using Eureka, we can now use the Ribbon technology to
The service is called, and this component can help us implement the load balancing component of the client.
Two. Construction of the environment
We first configure the provision of our services.
[1] Adding dependencies
We need to add the following dependencies.
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId> spring-cloud-starter-eureka</artifactid> </dependency> <dependency> <groupid >org.springframework.cloud</groupId> <artifactid>spring-cloud-starter-config</artifactid > </dependency> <dependency> <groupid>org.springframework.cloud</groupid > <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency>
[Modification of the 2]yml file
Eureka: client: service-URL: #defaultZone: http://eureka1.com:10086/ eureka/,http://eureka2.com: 10087/eureka/,http://eureka3.com: 10088/eureka/ defaultzone:http://eureka1.com:10086/eureka/ instance: Instance-id:consumer prefertrue
[3] Configuring our Resttemplate
@Configuration Public class resttemplateconfig { @Bean @LoadBalanced public resttemplate Resttemplate () { returnnew resttemplate (); }}
[4] Our main startup class
@SpringBootApplication@EnableEurekaClient " Cloud-provider " )publicclass consumerspringbootstarter { publicstatic void main (string[] args) { springapplication.run (consumerspringbootstarter. class , args);} }
006 Ribbon Components