简单的2个微服务例子
因为要写一些中间件使用,所以我需要简单2个微服务的例子。微服务现在很火,有不少大佬都写过相关博客,我就不写那些框架之类的了,简单一句话解释了,可能不是那么严谨吧,但懂得自懂吧。微服务就是把一个大业务分为无数个小的服务,通过中间件之类,使其构建成为大的业务。比如支付宝吧,支付宝的登录是由多个服务器构造,注册也是多个服务器,你把这个服务器换做微服务是这样说的,支付宝的登录是有一个微服务(可能是多...
因为要写一些中间件使用,所以我需要简单2个微服务的例子。
微服务现在很火,有不少大佬都写过相关博客,我就不写那些框架之类的了,简单一句话解释了,可能不是那么严谨吧,但懂得自懂吧。
微服务就是把一个大业务分为无数个小的服务,通过中间件之类,使其构建成为大的业务。
比如支付宝吧,支付宝的登录是由多个服务器构造,注册也是多个服务器,你把这个服务器换做微服务是这样说的,支付宝的登录是有一个微服务(可能是多个)程序部署在多台服务器上实现的,而支付宝则是有多个微服务部署在多台服务器实现的。
听懂听不懂无所谓了,写微服务博客的多的是,总有你听懂的。
其实很小的一个程序就算微服务了,我这里随便写2例子,主要是为了我后面中间件讲解服务的,很简单2个。
一个是简单使用springboot通过jpa访问数据库把数据库内容显示到网页上的(最简单的一个springboot项目)
另一个是调用上一个微服务的api把得到的内容显示到网页上的,都是很基础的springboot 项目。
在这里我使用了oracle数据库,因为企业基本都是用这个的,但是数据库可以随便更改。无所谓的。
具体内容我上传到github上,其实真没什么看的,代码我会放下面,想自己做别的例子可以随便看看做个类似差不多的。
因为springboot好多版本与有的中间件版本会冲突,所以我使用的1.5.9低版本的springboot
第一个项目demo-cloud-user
第二个项目demo-cloud-consumer
项目位置:(https://github.com/lihang212010/sping-cloud)
第一个项目demo-cloud-user
pop.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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo-cloud-user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-cloud-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Users.java
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 Users {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String username;
@Column
private String name;
@Column
private Integer age;
@Column
private BigDecimal balance;
public void setAge(Integer age) {
this.age = age;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public BigDecimal getBalance() {
return balance;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getUsername() {
return username;
}
}
UsersRepository
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UsersRepository extends JpaRepository<Users,Long> {
Optional<Users> findById(Long id);
}
UsersController.java
import java.util.Optional;
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;
@RestController
public class UsersController {
@Autowired
private UsersRepository usersRepository;
@RequestMapping("/{id}")
public Optional<Users> fingById(@PathVariable Long id) {
Optional<Users> users=usersRepository.findById(id);
return users;
}
}
启动类
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 DemoCloudUserApplication {
public static void main(String[] args) {
SpringApplication.run(DemoCloudUserApplication.class, args);
}
}
配置
server:
port: 8000
spring:
datasource:
driverClassName: oracle.jdbc.OracleDriver
password: boot
url: jdbc:oracle:thin:@localhost:1521:xe
username: boot
jpa:
show-sql: true
spring:
application:
name: demo-cloud-user
第二个项目demo-cloud-consumer
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo-cloud-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-cloud-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Users.java
import java.math.BigDecimal;
public class Users {
private Long id;
private String username;
private String name;
private Integer age;
private BigDecimal balance;
public void setAge(Integer age) {
this.age = age;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public BigDecimal getBalance() {
return balance;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getUsername() {
return username;
}
}
MoviesController
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;
@RestController
public class MoviesController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/user/{id}")
public Users findById(@PathVariable Long id) {
return this.restTemplate.getForObject("http://localhost:8000/"+id, Users.class);
}
}
启动类
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 DemoCloudConsumerApplication {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(DemoCloudConsumerApplication.class, args);
}
}
配置
server:
port: 8010
更多推荐
所有评论(0)