一扑媚、服務(wù)提供者和服務(wù)消費(fèi)者概念
服務(wù)提供者:服務(wù)的被調(diào)用方(即:為其他服務(wù)提供服務(wù)的服務(wù))
服務(wù)消費(fèi)者: 服務(wù)的調(diào)用方(即: 依賴其他服務(wù)的服務(wù))
二极景、編寫一個(gè)服務(wù)提供者 (自動生成框架:http://start.spring.io/)
- 生成服務(wù)提供者框架
- 將項(xiàng)目導(dǎo)入IDE
- 配置maven倉庫
修改Maven原有的settings文件:
- 配置本地倉庫路徑:
<localRepository>E:\JAVA\apache-maven-3.3.9\conf\</localRepository>
- 配置阿里云的鏡像
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
- 代碼
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<User, Long> {
}
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 #啟動時(shí)是否生成ddl語句
show-sql: true
hibernate:
ddl-auto: none #啟動時(shí)不做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:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.com.yeexun.cloud</groupId>
<artifactId>micoroservice-simple-provider-user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>micoroservice-simple-provider-user</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
可以成功訪問,并且返回?cái)?shù)據(jù)吼句。
三锅必、編寫一個(gè)服務(wù)消費(fèi)者
生成服務(wù)消費(fèi)者框架(自動生成框架:http://start.spring.io/)
將項(xiàng)目導(dǎo)入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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.com.yeexun.cloud</groupId>
<artifactId>micoroservice-simple-comsumer-movie</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>micoroservice-simple-comsumer-movie</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF8
</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
訪問成功