第四章 Spring CLoud 使用RestTemplate+Eureka调用用户微服务

上一篇讲到注册服务和用户服务,本章主要讲解一个服务调用另一个服务的方法:使用RestTemplate+eureka

项目结构如下:

第四章 Spring CLoud 使用RestTemplate+Eureka调用用户微服务_第1张图片


SpringCloudOrderApplication

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
public class SpringCloudOrderApplication {

  @Bean	//必须new 一个RestTemplate并放入spring容器当中,否则启动时报错
  public RestTemplate restTemplate() {
    return new RestTemplate();
  }

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


OrderController类


package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.example.demo.entity.User;

@RestController
public class OrderController {
  @Autowired
  private RestTemplate restTemplate;
  
  //获取application.yml里相应节点的值
  @Value("${user.userServicePath}")
  private String userServicePath;

  @GetMapping("/order/{id}")
  public User findById(@PathVariable Long id) {
    return this.restTemplate.getForObject(this.userServicePath + id, User.class);
  }
}

User类

package com.example.demo.entity;

import java.math.BigDecimal;

public class User {
  private Long id;

  private String username;

  private String name;

  private Short age;

  private BigDecimal balance;

  public Long getId() {
    return this.id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getUsername() {
    return this.username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Short getAge() {
    return this.age;
  }

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

  public BigDecimal getBalance() {
    return this.balance;
  }

  public void setBalance(BigDecimal balance) {
    this.balance = balance;
  }

}

application.yml配置

spring:
  application:
    name: spring-cloud-order    #微服务的名称
server:
  port: 7901  #微服务端口号
user: 
  userServicePath: http://localhost:7900/user/    #用户微服务的URL地址
eureka:
  client:
    healthcheck:
      enabled: true   # 开启健康检查
    serviceUrl:
      defaultZone: http://user:123456@localhost:8761/eureka   #服务eureka的URL地址
  instance:
    prefer-ip-address: true

Pom.xml配置



	4.0.0

	com.example.demo
	spring-cloud-order
	0.0.1-SNAPSHOT
	war

	spring-cloud-order
	spring-cloud-order

	
		org.springframework.boot
		spring-boot-starter-parent
		1.4.1.RELEASE
	

	
		  
		UTF-8
		UTF-8
		    
        UTF-8  
          
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.cloud
			spring-cloud-starter-eureka
		
		
		
			org.springframework.boot
			spring-boot-starter-actuator
		
		
		
			org.springframework.boot
			spring-boot-devtools
				
	
	
		
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Camden.SR2
				pom
				import
			
		
	

	
		
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



启动eureka服务,然后启动用户微服务和订单微服务,如下图所示:


第四章 Spring CLoud 使用RestTemplate+Eureka调用用户微服务_第2张图片


注意,要是创建的项目报错,请点击项目右键:Maven----->Update Project

勾选Force Update of Snapshots/Releases即可

第四章 Spring CLoud 使用RestTemplate+Eureka调用用户微服务_第3张图片











你可能感兴趣的:(Spring,Cloud)