2018-09-03 17:58:12.0|分类: spring cloud|浏览量: 2323
pom.xml <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.cookqq</groupId> <artifactId>consumer-order</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>consumer-order</name> <url>http://maven.apache.org</url> <!-- 引入spring boot的依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <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-client</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <!-- 引入spring cloud的依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> 已经引入了 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-ribbon</artifactId> </dependency>
orderApp.java package com.cookqq.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; /** * 订单服务 * */ @SpringBootApplication public class OrderApp { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } public static void main( String[] args ) { SpringApplication.run(OrderApp.class, args); } } restTemplate增加注解@LoadBalanced User.java package com.cookqq.consumer.bean; import java.math.BigDecimal; public class User { private Long id; private String username; private String name; private Integer age; private BigDecimal balance; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public BigDecimal getBalance() { return balance; } public void setBalance(BigDecimal balance) { this.balance = balance; } } OrderController.java package com.cookqq.consumer.controller; import java.util.List; import java.util.Random; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import com.cookqq.consumer.bean.User; @RestController public class OrderController { @Autowired private RestTemplate restTemplate; @Autowired private DiscoveryClient discoveryClient; @GetMapping("/user/{id}") public User findUserById(@PathVariable Long id){ // return this.restTemplate.getForObject("http://127.0.0.1:8000/"+id, User.class); // List<ServiceInstance> listInfo = discoveryClient.getInstances("provider-user"); // if(listInfo.size()>0){ // return this.restTemplate.getForObject( // listInfo.get(new Random().nextInt(listInfo.size())).getUri()+"/"+id, // User.class); // } return this.restTemplate.getForObject("http://provider-user/"+id, User.class); } } this.restTemplate.getForObject("http://provider-user/"+id, User.class)修改成微服务的名称,当ribbon和eureka配合使用,会自动将微服务的名称映射成微服务的网络ip+端口号。 启动前文的euraka 启动前文的provider-user(多启动几个实例) 启动consumer-order,访问浏览器 |