spring cloud (九) 服务注册中心 项目之间通过eureka注册中心进行接口的调用

流程是consumer 项目 去调用 eureka_client项目的 getUser接口

项目是基于 

spring cloud (八) 服务注册中心 eureka 高可用集群配置搭建server端以及client端

进行开发的

首先eureka_client项目

yml配置

spring:
  application:
    name: eureka_client
eureka:
  client:
    service-url:
      defaultZone:  http://server1:8001/eureka

pojo(两个项目的都一样

public class UserPojo {

    private String userName;
    private String userSex;
    private int age;

    public UserPojo(String userName, String userSex, int age) {
        this.userName=userName;
        this.userSex=userSex;
        this.age=age;
    }

    public UserPojo() {
        super();
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName=userName;
    }

    public String getUserSex() {
        return userSex;
    }

    public void setUserSex(String userSex) {
        this.userSex=userSex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age=age;
    }
}

写这么一个简单的controller吧。用了Post请求方式进行测试吧 。打印一下传过来的参数userName

@RestController()
public class UserController {

    @PostMapping("/getUser")
    public List getUser(@RequestParam("userName") String userName){

        System.out.println("---"+userName);

        List userPojoList = new ArrayList<>();
        userPojoList.add(new UserPojo("tom","man",15));
        userPojoList.add(new UserPojo("jack","man",18));
        userPojoList.add(new UserPojo("ajan","wuman",17));

        return userPojoList;
    }

}

eureka_client就写这些

consumer

yml配置

spring:
  application:
    name: consumer
server:
  port: 8090
eureka:
  client:
    service-url:
      defaultZone:  http://server1:8001/eureka

service

this.loadBalancerClient.choose("eureka_client");   这里选择的是yml里面配置的

spring:
  application:
    name: eureka_client

这个eureka_client

这里我用的是post方式,当然,需要get方式的话就用getForEntity,还有delete、put方式,需要哪种方式,就调用哪个方法

@Service
public class UserService {

//    ribbon负载均衡器
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    public List getUser(){
//        选择需要获取哪一个
//        比如我们想要和eureka_client进行连接
        ServiceInstance instance = this.loadBalancerClient.choose("eureka_client");
        StringBuffer sb = new StringBuffer();
//        拼接出来请求地址
        sb.append("http://").append(instance.getHost()).append(":").append(instance.getPort()).append("/getUser");
        RestTemplate template = new RestTemplate();
//        这里是内部类,我们只需要自有的方法就足够了,不需要要再写其他的了
//        ParameterizedTypeReference> typeReference = new ParameterizedTypeReference>() {};
        ParameterizedTypeReference typeReference = new ParameterizedTypeReference() {};
        MultiValueMap paramMap = new LinkedMultiValueMap();
        paramMap.add("userName","tom");
        HttpHeaders headers = new HttpHeaders();
        HttpEntity> httpEntity = new HttpEntity>(paramMap,headers);
//      第一个参数,我们拼接的url,第二个参数,请求参数,第三个返回值类型(本应该为
//      List的,技术有点菜,不知道怎么写...)
        ResponseEntity response =
                template.postForEntity(sb.toString(), httpEntity, Object.class);

        List list =(List) response.getBody();

        return list;
    } 
  

controller

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("getUser")
    public List getUser(){
        return userService.getUser();
    } 
  

启动类也是一样的,就是类名不一样,注意加注解 @EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
public class ConsumerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConsumerApplication.class, args);
	}

}

启动eureka  server

启动eureka_client

启动consumer

测试http://localhost:8090/getUser

spring cloud (九) 服务注册中心 项目之间通过eureka注册中心进行接口的调用_第1张图片

控制台可以打印出

 

你可能感兴趣的:(spring,cloud)