Next, in the last section, this article focuses on solving another component of Spring Cloud, ribbon, to implement Loading Balance on the client side. There are two main ways for ribbon to implement LB: ribbon restTemplate and feign.
One: ribbon restTemplate
1: The service registry Eureka has been built above, and a service client spring cloud-client has been built to register with eureka. To illustrate LB, the port and instance-id in spring cloud-client's yml file are changed as follows:
In particular, it should be noted that many online textbooks only say that ports need to be changed and started. You will find that although both ports can be accessed, there are no two services in eureka, and LB cannot be implemented. The reason is that if instance-id is the same, Eureka does not think of two different services, the latter covering the previous services.
After booting up, you will find that the inside of eureka becomes as follows:
2. Create a consumer and access client Load balance.
A new spring boot Maven project named spring cloud-ribbon is created. Spring cloud dependency is introduced, and eureka, ribbon and web dependency are also introduced. Its pom file is as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>ribbon</groupId> <artifactId>springcloud-ribbon</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springcloud-ribbon</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <!-- spring-cloud All project dependency packages --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
3. Add application.yml under src/main/resources to specify the eureka before the registry address of the service. The contents are as follows:
server: port: 8100 spring: application: name: springcloud-ribbon #The registry points to start eureka: instance: instance-id: springcloud-ribbon appname: ${spring.application.name} client: service-url: defaultZone: http://lps:YJ$888@127.0.0.1:8888/eureka/
4. Create a new startup class and apply for RestTemplate Bean in it. Use @LoadBalanced to specify that the entity bean enables LB.
@SpringBootApplication @EnableDiscoveryClient public class RibbomMain { public static void main(String[] args) { SpringApplication.run(RibbomMain.class, args); } @Bean @LoadBalanced//Specifies that this bean instance invokes load balancing RestTemplate restTemplate() { return new RestTemplate(); } }
5. Create a new Service class to invoke the Client service built before consumption. And use RestTemplate to invoke services. RestTemplate itself is an encapsulation of HTTP client. For the use of RestTemplate, you can refer to the instructions on the Internet. Because we only test the results, we use getForObject directly.
@Service public class RibbonTestService { @Autowired private RestTemplate restTemplate; public String getServicePort() { return restTemplate.getForObject("http://clientService/pls",String.class); } }
6. Create a new Controller and invoke the service through web access.
@RestController public class RibbomCallController { @Autowired private RibbonTestService service; @RequestMapping("/pls") public String getInfo(){ return service.getServicePort(); } }
7. This is all done. Start up consumption projects. And constantly refresh the page access: http://localhost:8100/pls
You can see that there will be a polling way to switch the Client service, which implements load balance.
Third, let's look at another implementation of LB, feign:
feign is easier to use than ribbon. As a result of:
1: feign integrates ribbon.
2: feign uses interface annotations for pluggability.
Here's how to use feign in comparison.
1: Build a new spring boot Maven project spring cloud-feign. Introduce spring cloud, eureka, feign, web dependency. The pom file is as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>ribbon</groupId> <artifactId>springcloud-ribbon</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springcloud-ribbon</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <!-- spring-cloud All project dependency packages --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
2. Also add application.yml.
server: port: 8200 spring: application: name: springcloud-feign #The registry points to start eureka: instance: instance-id: springcloud-feign appname: ${spring.application.name} client: service-url: defaultZone: http://lps:YJ$888@127.0.0.1:8888/eureka/
3. Create a startup class and use @EnableFeignClients to open an account with feign functionality.
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class FeignMain { public static void main(String[] args) { // TODO Auto-generated method stub SpringApplication.run(FeignMain.class, args); } }
4. Build an interface and invoke the Client service with Feign.
//Affirm the service name of the feign call, @FeignClient(value = "http://clientService") public interface GetServerPort { //Service method specification @RequestMapping("/pls") String callClient(); }
5. Create a controller to make web calls
@RestController public class FeignCallController { @Autowired GetServerPort getServerPort; @RequestMapping("/feign") public String getport() { return getServerPort.callClient(); } }
6. Continue to use browsers to access http://localhost:8200/feign, you can also see the effect of Load balance.