@SpringBootApplication @EnableDiscoveryClient public class SpringBootSampleApplication { @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(SpringBootSampleApplication.class, args); } }
@RestController @RequestMapping(value="/web/users") public class UserController { @Autowired RestTemplate restTemplate; @RequestMapping(value="/{userId}", method=RequestMethod.GET) public User getUserById(@PathVariable int userId){ return restTemplate.getForEntity("http://COMPUTE-SERVICE/user/"+userId, User.class).getBody(); } }
@Entity @Table(name = "t_user") public class User implements Serializable{ /** * */ private static final long serialVersionUID = 1381806832842693234L; @Id private int id; @Column(name = "user_name") private String userName; @Column(name = "password") private String password; @Column(name = "tel") private String tel; @Column(name = "sex") private String sex; @Column(name = "description") private String description; public int getId() { return id; } public String getUserName() { return userName; } public String getPassword() { return password; } public String getTel() { return tel; } public String getSex() { return sex; } public String getDescription() { return description; } public void setId(int id) { this.id = id; } public void setUserName(String userName) { this.userName = userName; } public void setPassword(String password) { this.password = password; } public void setTel(String tel) { this.tel = tel; } public void setSex(String sex) { this.sex = sex; } public void setDescription(String description) { this.description = description; } }
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp spring.application.name=ribbon-consumer server.port=1003 server.session-timeout=60 # server.address=192.168.16.11 eureka.client.serviceUrl.defaultZone=http://localhost:1000/eureka/
4.0.0 com.curiousby.cn SpringCloudDemo-Ribbon war 0.0.1-SNAPSHOT SpringCloudDemo-Ribbon Maven Webapp http://maven.apache.org spring-snapshots http://repo.spring.io/libs-snapshot spring-snapshots http://repo.spring.io/libs-snapshot org.springframework.boot spring-boot-starter-parent 1.3.5.RELEASE UTF-8 1.7 org.springframework.cloud spring-cloud-starter-ribbon org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat provided org.apache.tomcat.embed tomcat-embed-jasper provided javax.servlet jstl org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-data-jpa runtime mysql mysql-connector-java runtime org.springframework.cloud spring-cloud-dependencies Brixton.RELEASE pom import SpringCloudDemo-Ribbon org.springframework.boot spring-boot-maven-plugin org.apache.maven.plugins maven-compiler-plugin 1.7 1.7 org.apache.maven.plugins maven-surefire-plugin false