1.准备工作(设置idea开启多个springboot实例)
https://blog.csdn.net/qq_42014192/article/details/89245306
2.新建一个spring-boot工程,取名为:service-ribbon;
https://blog.csdn.net/qq_42014192/article/details/88742559
3.pom.xml依赖
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springcloud_service-ribbon_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud_service-ribbon_demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.配置文件application.yml如下:
server:
port: 8764 #服务端口
spring:
application:
name: service-ribbon
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
5.在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
@SpringBootApplication
//通过注解@EnableEurekaClient 表明自己是一个eurekaclient
@EnableEurekaClient
//通过@EnableDiscoveryClient向服务中心注册
@EnableDiscoveryClient
public class SpringcloudServiceRibbonDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudServiceRibbonDemoApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
6.写一个测试类HelloService
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hiService(String name){
return restTemplate.getForObject("http://SERVICE-CLIENT/client?name="+name,String.class);
}
}
7.写一个controller
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping(value = "/client")
public String client(@RequestParam String name){
return helloService.hiService(name);
}
}
8.在浏览器上多次访问http://localhost:8764/client?name=forezp,浏览器交替显示:
client forezp,i am from port:8762
client forezp,i am from port:8763
9.架构
一个服务注册中心,Eureka-server,端口为8761
service-client工程跑了两个实例,端口分别为8762,8763,分别向服务注册中心注册
sercvice-ribbon端口为8764,向服务注册中心注册
当sercvice-ribbon通过restTemplate调用service-client的client接口时,因为用ribbon进行了负载均衡,会轮流的调用service-client:8762和8763 两个端口的client接口;
注意:5步骤如何少been就不会出现下错误,解决办法将RestTemplate添加到spring容器中
***************************
APPLICATION FAILED TO START
***************************
Description:
Field restTemplate in com.example.springcloud_serviceribbon_demo.service.HelloService required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.