今天咱们来学习 服务消费者ribbon 如何调用 服务提供者java
因为 获取用户信息 属于业务模块,所以咱们须要新建一个maven主项目:bussnissservicenginx
而后在该主项目上建立一个springboot项目web
点击下一步,选择“Eureka Server” 算法
此时的项目结构:spring
一样,将 application.properties 修改成 bootstrap.yml (之后不作特殊说明都修改成bootstrap.yml)数据库
spring: application: name: service-user server: port: 8801
使用@EnableEurekaClient注解开启Eureka客户端apache
package com.mayi.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class BussnessserviceUserApplication { public static void main(String[] args) { SpringApplication.run(BussnessserviceUserApplication.class, args); } }
新建一个controller包,并在此包下写一个获取全部用户的接口bootstrap
import java.util.Map; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserManagementController { @Value("${server.port}") String serverPort; @GetMapping("/listUsers") public String ListUsers(){ /** * 模拟从数据库查询 */ List<Map<String, Object>> users = new ArrayList<Map<String, Object>>(); for(int i=1; i< 5; i++){ Map<String, Object> user = new HashMap<String, Object>(); user.put("id", i); user.put("name", "小明" + i); users.add(user); } return "服务器端口号: " + serverPort + " | 用户信息: " + users.toString(); } }
启动Eureka注册中心--启动 service-user 微服务--而后在浏览器输入浏览器
http://localhost:8801/listUsersspringboot
端口号为8801的服务器为咱们返回了用户列表信息
ribbon 是 Netflix 发布的 客户端负载均衡 组件,(nginx是服务端的负载均衡)
下面看一下具体实现
选择Eureka Server 和 Ribbon
完整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> <groupId>com.mayi.springcloud</groupId> <artifactId>bussnessservice-user-client-ribbon</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>bussnessservice-user-client-ribbon</name> <description>用户中心服务调用者ribbon</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.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>Finchley.M9</spring-cloud.version> </properties> <dependencies> <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> <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>
bootstrp.xml
spring: application: name: service-user-client-ribbon server: port: 8901
在启动类中@Bean 将 restTemplate注入到ioc容器, 并使用@LoadBalanced 注解声明开启 负载均衡
package com.mayi.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient public class BussnessserviceUserClientRibbonApplication { public static void main(String[] args) { SpringApplication.run(BussnessserviceUserClientRibbonApplication.class, args); } @Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); } }
接下来,新建一个client包,在该包下写一个ribbon接口
package com.mayi.springcloud.client; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class UserManagementRibbonClient { @Autowired RestTemplate restTemplate; @GetMapping("/listUsersByRibbon") public String ListUsersByRibbon(){ String result = this.restTemplate.getForObject("http://service-user/listUsers", String.class); return result; } }
说明:service-user 为服务名称, 一个服务名称对应多个主机IP和端口号,这样根据服务名调用就实现了负载均衡的功能
为测试 ribbon负载均衡功能,咱们再启动一个service-user服务,端口号为8802:
访问:http://localhost:8901/listUsersByRibbon
由于默认的负载均衡算法是轮询,因此两台服务器轮流被调用,(也可自行修改负载均衡算法,例如:随机算法,只是简单的配置,这里再也不阐述)
接下来,我会依次更新文章,直至整个架构完成,若有兴趣的朋友关注做者 或 加我微信 拉你进入spring cloud社区群
微信公众号:java架构师修行
本公众号从2018-5.1日 - 2019.5.1日期间,将要按照JAVA高级软件架构师实战培训的路线发布一期完整的架构文章,难度由浅入深,适合有必定开发基础想转架构和正在作初级架构开发的人员学习