<dependencies> <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> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> <version>1.4.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.6.RELEASE</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
spring.application.name=eureka-provider server.port=9090 # 设置服务注册中心地址,指向另一个注册中心 eureka.client.serviceUrl.defaultZone=http://eureka1:8761/eureka/,http://eureka2:8761/eureka/
package com.dpb.springcloud_eureka_provider.pojo; /** * @program: springcloud-eureka-provider * @description: UserBean * @author: 波波烤鸭 * @create: 2019-05-28 17:31 */ public class User { private int userid; private String username; private int userage; public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getUserage() { return userage; } public void setUserage(int userage) { this.userage = userage; } public User(int userid, String username, int userage) { this.userid = userid; this.username = username; this.userage = userage; } public User() { } }
/** * @program: springcloud-eureka-provider * @description: 服务提供者 * @author: 波波烤鸭 * @create: 2019-05-28 17:30 */ @RestController public class UserController { @RequestMapping("/user") public List<User> getUsers(){ List<User> list = new ArrayList<>(); list.add(new User(1,"zhangsan",20)); list.add(new User(2,"lisi",22)); list.add(new User(3,"wangwu",20)); return list; } }
<dependencies> <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> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> <version>1.4.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.6.RELEASE</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
spring.application.name=eureka-consumer server.port=9091 #设置服务注册中心地址,指向另一个注册中心 eureka.client.serviceUrl.defaultZone=http://eureka1:8761/eureka/,http://eureka2:8761/eureka/
/** * @program: springcloud-eureka-consumer * @description: 用户的业务处理 * @author: 波波烤鸭 * @create: 2019-05-28 17:43 */ @Service public class UserService { /** * ribbon 负载均衡 * LoadBalancerClient 通过服务名称可以获取对应的服务的相关信息 ip port等 */ @Autowired private LoadBalancerClient loadBalancerClient; public List<User> getUsers(){ // ServiceInstance 封装的有服务的基本信息 IP和端口等 ServiceInstance si = this.loadBalancerClient.choose("eureka-provider"); StringBuilder sb = new StringBuilder(); sb.append("http://") .append(si.getHost()) .append(":") .append(si.getPort()) .append("/user"); // SpringMVC RestTemplate RestTemplate rt = new RestTemplate(); ParameterizedTypeReference<List<User>> type = new ParameterizedTypeReference<List<User>>() {}; // ResponseEntity:封装了返回值的信息 ResponseEntity<List<User>> response = rt.exchange(sb.toString(), HttpMethod.GET,null,type); List<User> list = response.getBody(); return list; } }
/** * @program: springcloud-eureka-consumer * @description: 用户控制器 * @author: 波波烤鸭 * @create: 2019-05-28 17:40 */ @RestController public class UserController { @Autowired private UserService userService; @RequestMapping("/consumer") public List<User> getUsers(){ return this.userService.getUsers(); } }