This chapter describes the service consumers in Springcloud
There are two implementations of the Springcloud service invocation method:
1,resttemplate+ribbon,
2,feign
Originally wanted an article to finish, found the length is a bit long, so this chapter first speak Resttemplate+ribbon,
The Ribbon is a load balancer client that can control some of the behavior of HTT and TCP very well.
The feign is integrated with the Ribbon by default.
A new Springboot project with a name of Eureka-consumer-ribbon
The Pom.xml configuration is as follows
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <PR Oject.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8</ java.version> </properties> <dependencies> <dependency> <groupid>org.sp Ringframework.cloud</groupid> <artifactId>spring-cloud-starter-eureka</artifactId> < ;/dependency> <dependency> <groupId>org.springframework.boot</groupId> & Lt;artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-actuator</ar tifactid> </dependency> <dependency> <groupid>org.springframework.cloud</ Groupid> <artifactid>spring-cloud-starter-ribbon</artifactid> </dependency> </dependencies> <depen dencymanagement> <dependencies> <dependency> <groupid>org.springfram Ework.cloud</groupid> <artifactId>spring-cloud-dependencies</artifactId> & Lt;version>dalston.sr1</version> <type>pom</type> <scope>Import</scope> </dependency> </dependencies> </dependencyManagement>
Add @enablediscoveryclient annotations on the Project startup class application (the difference between @enablediscoveryclient and @enableeurekaserver mentioned in the previous chapter) register the service with the service center,
and using @bean to inject a Resttemplate object into the spring container, @LoadBalanced annotations indicate that the restremplate turns on the load balancing function.
@EnableDiscoveryClient @springbootapplication Public class application {@Bean @LoadBa lanced public Resttemplate resttemplate () { return new Resttemplate (); public static void main (string[] args) { new Springapplicationbuilder (Application. Class ). Web (true
specified in Application.properties, service name: Eureka-consumer-ribbon, Port 9101, service registry address: http://localhost:9001/eureka/
spring.application.name=eureka-consumer-ribbonserver.port=9101 Eureka.client.serviceUrl.defaultZone=http://localhost:9001/eureka/
Create a new Ribboncontroller class to consume Eureka-client services
@RestController
public class Ribboncontroller {
@Autowired
Resttemplate resttemplate;
@GetMapping ("/consumer")
Public String getmsg () {
Return Resttemplate.getforobject ("Http://eureka-client/client", String.class);
}
}
The request address here uses http://server-name instead of Http://ip:port, because in the ribbon it chooses a specific service instance based on the service name, and depending on the service instance, the service name is replaced with a specific URL at the time of the request. The request here is eureka-client/client.
Use the projects already created in the previous chapter eureka-server,eureka-client these two projects
First of all:
1, start Eureka-server
2, the port that starts the Eureka-client configuration file defaults to 9002
3, modify the configuration file port to 9003, and then start a eureka-client
4, at startup Eureka-consumer-ribbon
By visiting http://localhost:9001/, you will find that there are already eureka-clent services registered, and there are two instances, 9002 and 9003
By visiting Http://localhost:9101/consumer, you will find that you have already consumed the services provided by the Eureka-client project, and the Ribbon has already implemented load balancing by default
This article code:Code Cloud:GitHub:
Resources:
Thanks for sharing:
Suphing: http://blog.didispace.com/spring-cloud-starter-dalston-1/
Fang Zhipeng: Http://blog.csdn.net/forezp/article/details/70148833#t0
Welcome to pay attention to my public number, sweep code find more exciting content
|
This article is the original article, welcome reprint, reprint please indicate the author |
Springcloud's service consumer (ribbon)