环境说明:由于此次主要是学习spring cloud,故例子程序都进行简化,没有service层。数据库持久层使用JPA,数据库使用内嵌的H2。web使用spring mvc,项目构建使用maven,IDE使用myeclipse,jdk 1.8,spring boot使用。
(1)服务提供者项目搭建过程:
pom.xml如下:
4.0.0 com.antsoldier provider 0.0.1-SNAPSHOT jar provider Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.3.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-web com.h2database h2 runtime org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
在classpath下添加启动配置文件:
application.yml
server: port: 8083spring: jpa: generate-ddl: false show-sql: true hibernate: ddl-auto: none datasource: platform: h2 schema: classpath:database.sql data: classpath:data.sqllogging: level: root: INFO org.hibernate: INFO org.hibernate.type.decriptor.sql.BasicBinder: TRACE org.hibernate.type.decriptor.sql.BasicExtractor: TRACE com.itmuch: DEBUG
注意:
server: port: 8083 代表tomcat启动端口
schema: classpath:database.sql 代表开始执行的建表语句data: classpath:data.sql 代表初始化插入数据 在classpath下建立database.sql文档,用来初始化数据库表。
drop table user if exists;create table user(id bigint,username varchar(20),name varchar(20),age int);
在classpath下建立data.sql文档,用来为表添加数据。
insert into user(id,username,name,age) values(1,'user1','name1',20);insert into user(id,username,name,age) values(2,'user2','name2',21);insert into user(id,username,name,age) values(3,'user3','name3',23);insert into user(id,username,name,age) values(4,'user4','name4',24);
在com.antsoldier.model包下创建User类
package com.antsoldier.model;import java.io.Serializable;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;@Entitypublic class User implements Serializable{ @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; @Column private String username; @Column private String name; @Column private int age; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
在com.antsoldier.dao包下创建UserDao类,作为dao层
package com.antsoldier.dao;import com.antsoldier.model.User;import org.springframework.data.jpa.repository.JpaRepository;public interface UserDao extends JpaRepository{}
在com.antsoldier.controller下创建UserController类,作为controller层
package com.antsoldier.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;import com.antsoldier.dao.UserDao;import com.antsoldier.model.User;@RestControllerpublic class UserController { @Autowired private UserDao userDao; @GetMapping("/user/findById/{id}") public User findById(@PathVariable Long id){ return userDao.findOne(id); }}
主启动程序:
package com.antsoldier;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); }}
运行主启动程序:输入网址:http://localhost:8083/user/findById/1
测试服务提供方成功如下:
(2)服务消费方项目搭建如下:
pom.xml
4.0.0 com.antsoldier consumer 0.0.1-SNAPSHOT jar consumer Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.3.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
在classpath下添加启动配置文件:
application.yml
server: port: 8073userServicePath: http://localhost:8083
注意:userServicePath是服务提供方地址 在com.antsoldier.model包下创建User类
package com.antsoldier.model;import java.io.Serializable;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;@Entitypublic class User implements Serializable{ @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; @Column private String username; @Column private String name; @Column private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
在com.antsoldier.controller下创建UserConsumerController类,用来调用远程服务:
package com.antsoldier.controller;import javax.annotation.Resource;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.antsoldier.model.User;@RestControllerpublic class UserConsumerController { @Resource private RestTemplate restTemplate; //需要在主程序中配置此对象 @Value("${userServicePath}") private String userServicePath; @GetMapping(value="/userConsumer/{id}") public User findById(@PathVariable Long id){ return restTemplate.getForObject(userServicePath + "/user/findById/"+id, User.class); }}
注意 @Value就是用来加载application.yml文件中的userServicePath值。
restTemplate对象,需要自己生成,此处为了简单直接在,主程序中生成对象
主启动程序:
package com.antsoldier;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@SpringBootApplicationpublic class ConsumerApplication { @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); }}
运行主启动程序:输入网址:http://localhost:8073/userConsumer/1
测试消费方成功如下:
注意事项:本人在myeclipse中运行项目时,发现配置文件不起作用经过排查才发现由于项目导入myeclipse时,myeclipse没有识别项目为maven项目,一定要确保项目左上角有M标识:
项目下载地址:
链接:http://pan.baidu.com/s/1c12YM1i 密码:nw33