一、服务提供者和服务消费者概念
服务提供者:服务的被调用方(即:为其他服务提供服务的服务)
服务消费者: 服务的调用方(即: 依赖其他服务的服务)
二、编写一个服务提供者 (自动生成框架:http://start.spring.io/)
- 生成服务提供者框架
- 将项目导入IDE
- 配置maven仓库
修改Maven原有的settings文件:
- 配置本地仓库路径:
E:\JAVA\apache-maven-3.3.9\conf\
- 配置阿里云的镜像
nexus-aliyun
central
Nexus aliyun
http://maven.aliyun.com/nexus/content/groups/public
- 代码
Controller:
package cn.com.yeexun.cloud.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 cn.com.yeexun.cloud.dao.UserDao;
import cn.com.yeexun.cloud.entity.User;
@RestController
public class UserController {
@Autowired
private UserDao userDao;
@GetMapping("/simple/{id}")
public User findById(@PathVariable Long id)
{
return userDao.findOne(id);
}
}
Dao:
package cn.com.yeexun.cloud.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import cn.com.yeexun.cloud.entity.User;
@Repository
public interface UserDao extends JpaRepository {
}
Entity:
package cn.com.yeexun.cloud.entity;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column
private String username;
@Column
private String name;
@Column
private Short age;
@Column
private BigDecimal balance;
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 Short getAge() {
return age;
}
public void setAge(Short age) {
this.age = age;
}
public BigDecimal getBalance() {
return balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
Apllication启动类:
package cn.com.yeexun.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MicoroserviceSimpleProviderUserApplication {
public static void main(String[] args) {
SpringApplication.run(MicoroserviceSimpleProviderUserApplication.class, args);
}
}
application.yml:
server:
port: 7900
spring:
jpa:
generate-ddl: false #启动时是否生成ddl语句
show-sql: true
hibernate:
ddl-auto: none #启动时不做ddl语句处理
datasource:
platform: h2
schema: classpath:schema.sql
data: classpath:data.sql
logging:
level:
root: INFO
org.hibernate: INFO
org.hibernate.type.descriptor.sql.BasicBinder: TRACE
org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
cn.com: DEBUG
data.sql:
insert into user(id, username, name, age, balance) values(1, 'user1', '张一', 20, 100.00);
insert into user(id, username, name, age, balance) values(2, 'user2', '王二', 22, 101.00);
insert into user(id, username, name, age, balance) values(3, 'user3', '李三', 23, 102.00);
insert into user(id, username, name, age, balance) values(4, 'user4', '马四', 24, 103.00);
schema.sql:
drop table user if exists;
create table user(
id bigint generated by default as identity,
username varchar(40),
name varchar(20),
age int(3),
balance decimal(10,2),
primary key(id)
);
pom.xml:
4.0.0
cn.com.yeexun.cloud
micoroservice-simple-provider-user
0.0.1-SNAPSHOT
jar
micoroservice-simple-provider-user
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.10.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
可以成功访问,并且返回数据。
三、编写一个服务消费者
生成服务消费者框架(自动生成框架:http://start.spring.io/)
将项目导入IDE
配置maven仓库
如果修改过maven的库文件settins.xml,则这块就不用修改了代码
Controller:
package cn.com.yeexun.cloud.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 org.springframework.web.client.RestTemplate;
import cn.com.yeexun.cloud.entity.User;
@RestController
public class MovieController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/movie/{id}")
public User findById(@PathVariable Long id)
{
return this.restTemplate.getForObject("http://localhost:7900/simple/" + id, User.class);
}
}
entity:
package cn.com.yeexun.cloud.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 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 Short getAge() {
return age;
}
public void setAge(Short age) {
this.age = age;
}
public BigDecimal getBalance() {
return balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
application:
package cn.com.yeexun.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class MicoroserviceSimpleComsumerMovieApplication {
@Bean
public RestTemplate restTemplate()
{
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(
MicoroserviceSimpleComsumerMovieApplication.class, args);
}
}
application.yml:
server:
port: 7091
pom.xml
4.0.0
cn.com.yeexun.cloud
micoroservice-simple-comsumer-movie
0.0.1-SNAPSHOT
jar
micoroservice-simple-comsumer-movie
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.10.RELEASE
UTF-8
UTF8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
访问成功