一、微服务之provider和consumer
新建父工程
对于父工程,编辑pom文件
|
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.9.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<!-- 指定maven编译的版本 --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId>
<configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> |
新建两个子工程
provider-user
编辑provider-user的pom文件
|
<!-- 使用springmvc和spring的jar包 --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- <version>1.5.9.RELEASE</version> 父工程已经有版本了,所以不用再指定 --> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> |
|
新建启动类ProviderUser.java
package cn.huangwei.app;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = "cn.huangwei.controller") public class ProviderUser { public static void main(String[] args) { SpringApplication.run(ProviderUser.class, args); } }
新建User和UserController package cn.huangwei.entity;
import java.util.Date;
public class User { private long id; private Date date; public long getId() { return id; } public void setId(long id) { this.id = id; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public User(long id) { this.id = id; this.date = new Date(); }
public User() {
}
}
package cn.huangwei.controller;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import cn.huangwei.entity.User;
@RestController public class UserController {
@RequestMapping("/user/{id}") public User getUser(@PathVariable long id) { return new User(id); } }
|
创建第二个子工程consumer-order
具体内容如下:
|
Pom文件与上一个子工程一致
启动类: package cn.huangwei.app;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate;
@SpringBootApplication(scanBasePackages = "cn.huangwei.controller") public class ConsumerOrder { @Bean//相当于xml中的bean标签,主要用于调用当前方法获取指定对象 public RestTemplate getTemplate() { return new RestTemplate(); }
public static void main(String[] args) { SpringApplication.run(ConsumerOrder.class, args); } }
package cn.huangwei.controller;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate;
import cn.huangwei.entity.User;
@RestController public class OrderController { @Autowired private RestTemplate restTemplate; //spring提供的用于访问rest接口的模板对象
private String url = "http://localhost:7900/user/";
@RequestMapping("/order/{id}") public User getOrder(@PathVariable long id) { //访问提供者,获取数据 User user = restTemplate.getForObject(url + id, User.class);//通过访问rest访问json数据,然后转换为rest return user; } }
User也一致,启动的时候两个工程都要启动起来,实现了项目间的相互访问 |
问题分析:
此处地址已经写死,可以这么修改
然后再application.yml文件中配置