Ribbon(負載均衡的客戶端)
阿新 • • 發佈:2018-11-22
前面講了服務的註冊與發現,微服務專案會把專案的各個業務需求劃分成幾個模組來單獨提供服務,各服務間的呼叫都是採用Http Restful來實現,但是在SpringClound中服務間的呼叫有兩種方式:一種是ribbon+ restTemplate;另一種是feign;
Ribbon:在SpringClound中是作為一個負載均衡的客戶端,控制訪問入口,定製訪問策略等功能; Feign元件同時也是集成了ribbon的
開啟前面準備的服務client
在Idea裡,新建專案,選擇Spring initializer.
勾選元件
下面的pom
<?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> <groupId>com.example</groupId> <artifactId>spring-clound-ribbon-example</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-clound-ribbon-example</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.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> <spring-cloud.version>Greenwich.M3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>1.4.4.RELEASE</version> </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-web</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> <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>
配置properties檔案引數;
#服務埠 server.port=8885 #註冊服務中心地址 eureka.client.service-url.defaultZone=http://localhost:8882/eureka/ #註冊服務端name spring.application.name=service-ribbon #呼叫連線時間 eureka.client.eureka-server-read-timeout-seconds=6000 #呼叫連線時間 eureka.client.eureka-server-connect-timeout-seconds=6000 hystrix.metrics.polling-interval-ms=6000
在啟動類上添加註解@EnableDiscoveryClient
@EnableDiscoveryClient @SpringBootApplication public class SpringCloundRibbonExampleApplication { public static void main(String[] args) { SpringApplication.run(SpringCloundRibbonExampleApplication.class, args); } /** * ioc注入一個bean: restTemplate;並通過@LoadBalanced註解表明這個restRemplate開啟負載均衡的功能 * * @return */ @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
建立conroller、service
@RestController public class HelloController { @Autowired IHelloService helloService; @RequestMapping(value = "/index") public String index() { return helloService.index(); } }
@Service public class HelloServiceImpl implements IHelloService { @Autowired RestTemplate restTemplate; @Override public String index() { return restTemplate.getForObject("http://SERVICE-HELLO/index",String.class); } }
啟動專案,然後再看服務中心,已經註冊成功
回到瀏覽器,輸入http://localhost:8885/index
重新整理: