4.0.0com.learn.cloudspringcloud-eureka-consumer0.0.1-SNAPSHOTjarorg.springframework.bootspring-boot-starter-parent1.5.12.RELEASE UTF-8UTF-81.8 org.springframework.cloudspring-cloud-dependenciesDalston.SR1pomimportorg.springframework.bootspring-boot-starter-weborg.springframework.cloudspring-cloud-starter-eurekaorg.springframework.bootspring-boot-maven-plugin
package com.learn.pojo;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) {super();this.userid = userid;this.username = username;this.userage = userage;}public User() {super();// TODO Auto-generated constructor stub}}
package com.learn.service;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;import com.learn.pojo.User;@Service
public class UserService {@Autowiredprivate LoadBalancerClient loadBalancerClient;//ribbon负载均衡器public List getUsers(){//选择调用的服务的名称//ServiceInstance 封装了服务的基本信息,如 IP,端口ServiceInstance si = this.loadBalancerClient.choose("springcloud-eureka-provider");//拼接访问服务的URLStringBuffer sb = new StringBuffer();//http://localhost:9090/usersb.append("http://").append(si.getHost()).append(":").append(si.getPort()).append("/user");//springMVC RestTemplateRestTemplate rt = new RestTemplate();ParameterizedTypeReference> type = new ParameterizedTypeReference>() {};//ResponseEntity:封装了返回值信息ResponseEntity> response = rt.exchange(sb.toString(),HttpMethod.GET, null, type);List list =response.getBody();return list;}
}
package com.learn.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import com.learn.pojo.User;
import com.learn.service.UserService;@RestController
public class UserController {@Autowiredprivate UserService userService;@RequestMapping("/consumer")public List getUsers(){return this.userService.getUsers();}
}
package com.learn;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableEurekaClient
@SpringBootApplication
public class ConsumerApplication {public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class, args);}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!