技术框架:Spring Boot
服务提供者:服务的被调用方(即:为其他服务提供服务的服务)
服务消费者:服务的调用方(即:依赖其它服务的服务)
俗话说:君子生非异也 善假于物。翻译原生英文文档太浪费时间和精力了,我也是在网上搜到的一些资料。研究没问题后发出来,借花献佛 献丑了。
编写一个简单的程序实现服务消费者和服务提供者:
Spring官网提供了快速构建Springboot项目的网址:http://start.spring.io/
①首先生成服务提供者项目
② 然后生成服务消费者项目
编写代码后两个的项目结构如下:
schema.sql (SpringBoot启动时默认执行classpath下的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)
);
data.sql
insert into user(id,username, name, age, balance) values(1,'user1','张三',10, 100.00);
insert into user(id,username, name, age, balance) values(2,'user2','李四',20, 200.00);
insert into user(id,username, name, age, balance) values(3,'user3','王五',30, 300.00);
insert into user(id,username, name, age, balance) values(4,'user4','赵六',40, 400.00);
insert into user(id,username, name, age, balance) values(5,'user5','孙七',50, 500.00);
insert into user(id,username, name, age, balance) values(6,'user6','胜八',60, 600.00);
User.java
package com.liuxun.cloud.entity;
import java.io.Serializable;
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 implements Serializable {
private static final long serialVersionUID = 1L;
@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;
}
}
UserRepository
package com.liuxun.cloud.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.liuxun.cloud.entity.User;
public interface UserRepository extends JpaRepository {
}
UserController
package com.liuxun.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 com.liuxun.cloud.entity.User;
import com.liuxun.cloud.repository.UserRepository;
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("/simple/{id}")
public User findById(@PathVariable Long id) {
return userRepository.findById(id).get();
}
}
application.yml
server:
port: 7900
spring:
jpa:
generate-ddl: false
show-sql: true
hibernate:
ddl-auto: none
datasource:
platform: h2
schema: classpath:schema.sql #defalut schema.sql
data: classpath:data.sql
logging:
level:
root: info
org.hibernate.type.descriptor.sql.BasicBinder: trace #与show-sql 结合打印参数
org.hibernate.type.descriptor.sql.BasicExtractor: trace #与show-sql 结合打印参数
pom.xml (构建项目时自动生成的)
4.0.0
com.liuxun.cloud
microservice-simple-provider-user
0.0.1-SNAPSHOT
jar
microservice-simple-provider-user
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.0.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
运行测试结果如下:
User.java
package com.liuxun.cloud.entity;
import java.io.Serializable;
import java.math.BigDecimal;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
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;
}
}
MovieController.java
package com.liuxun.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 com.liuxun.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);
}
}
Application.java
package com.liuxun.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 MicroserviceSimpleConsumerMovieApplication {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(MicroserviceSimpleConsumerMovieApplication.class, args);
}
}
application.yml
server:
port: 7901
pom.xml
4.0.0
com.liuxun.cloud
microservice-simple-consumer-movie
0.0.1-SNAPSHOT
jar
microservice-simple-consumer-movie
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.0.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下的schema.sql
默认数据脚本是执行classpath下的data.sql
这种方法的缺陷:微服务组件之间通信采用了硬编码是存在很大弊端的:
① 如果各个服务模块之间依赖紧密,当修改依赖模块的端口时 都要必须在服务消费者中修改地址
② 在负载均衡若使用nginx的话 这样硬编码会导致配置异常繁琐