简单的微服务实例搭建(一)
模拟一个服务调用的场景的搭建过程:
1、 新建一个maven工程:Microservices,再在其下新建module,同样的依旧是maven工程:service、consumer、common(大体结构如图):
关于服务的介绍:
- common:用于存放一些公共的代码,在该实例中用于编写model实体类
- service:一个提供根据查询所有用户的微服务,为consumer提供服务,在该实例中编写mapper、service、web(controller)
- consumer::一个服务调用者,通过RestTemplate远程调用service,客户端访问获取服务,在该实例中编写controller、html页面
2、关于yml和pom文件:
- Microservices
<?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>com.zpark</groupId>
<artifactId>Microservices</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>common</module>
<module>consumer</module>
<module>service</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mapper.starter.version>2.0.3</mapper.starter.version>
<mysql.version>8.0.15</mysql.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- 通用mapper启动器(git上开源项目) -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>${mapper.starter.version}</version>
</dependency>
<!-- mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
注意: 在该项目中使用通用mapper启动器(git上开源项目)一个实列用于学习
- common
<?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">
<parent>
<artifactId>Microservices</artifactId>
<groupId>com.zpark</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>common</artifactId>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-core</artifactId>
<version>1.0.4</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
- service:服务提供者
server:
port: 8081
servlet:
context-path: /
spring:
datasource:
url: jdbc:mysql://localhost:3306/microservices?characterEncoding=utf-8&serverTimezone=UTC
username: root
password: 1234
driver-class-name: com.mysql.cj.jdbc.Driver
thymeleaf:
cache: false
application:
name: service
mybatis:
type-aliases-package: com.zpark.model
configuration:
map-underscore-to-camel-case: true
<?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">
<parent>
<artifactId>Microservices</artifactId>
<groupId>com.zpark</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service</artifactId>
<!--添加依赖-->
<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mapper启动器 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
</dependency>
<!--thymeleaf模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<artifactId>common</artifactId>
<groupId>com.zpark</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
- consumer:服务调用者
server:
port: 8083
spring:
datasource:
url: jdbc:mysql://localhost:3306/microservices?characterEncoding=utf-8&serverTimezone=UTC
username: root
password: 1234
driver-class-name: com.mysql.cj.jdbc.Driver
thymeleaf:
cache: false
application:
name: consumer
mybatis:
type-aliases-package: com.zpark.model
configuration:
map-underscore-to-camel-case: true
<?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">
<parent>
<artifactId>Microservices</artifactId>
<groupId>com.zpark</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>consumer</artifactId>
<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>
</dependency>
<dependency>
<artifactId>common</artifactId>
<groupId>com.zpark</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
3、编写实现代码:
- common
package com.zpark.model;
import org.apache.ibatis.type.JdbcType;
import tk.mybatis.mapper.annotation.ColumnType;
import javax.persistence.Id;
import java.io.Serializable;
public class Country implements Serializable {
private static final long serialVersionUID = 6569081236403751407L;
@Id
@ColumnType(jdbcType = JdbcType.BIGINT)
private Long Id;
private String countryname;
private String countrycode;
public Long getId() {
return Id;
}
public void setId(Long Id) {
this.Id = Id;
}
public String getCountryname() {
return countryname;
}
public void setCountryname(String countryname) {
this.countryname = countryname;
}
public String getCountrycode() {
return countrycode;
}
public void setCountrycode(String countrycode) {
this.countrycode = countrycode;
}
@Override
public String toString() {
return "Country{" +
"Id=" + Id +
", countryname='" + countryname + '\'' +
", countrycode='" + countrycode + '\'' +
'}';
}
}
- service
mapper:
package com.zpark.service.mapper;
import com.zpark.model.Country;
import tk.mybatis.mapper.common.Mapper;
public interface CountryMapper extends Mapper<Country> {
}
service:
package com.zpark.service.services;
import com.zpark.model.Country;
import java.util.List;
public interface CountryService {
public List<Country> queryAll();
}
ServiceImpl
package com.zpark.service.services;
import com.zpark.model.Country;
import com.zpark.service.mapper.CountryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CountryServiceImpl implements CountryService {
@Autowired
private CountryMapper countryMapper;
@Override
public List<Country> queryAll() {
return countryMapper.selectAll();
}
}
Controller
package com.zpark.service.web;
import com.zpark.model.Country;
import com.zpark.service.services.CountryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/")
public class ServiceController {
@Autowired
CountryService countryService;
@RequestMapping(value = {"findall"})
public String findall(){
final List<Country> countries = countryService.queryAll();
final StringBuilder stringBuilder = new StringBuilder();
for (Country country:countries) {
stringBuilder.append(country);
}
final String string = stringBuilder.toString();
return string;
}
}
启动类
package com.zpark.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.zpark.service.mapper")
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class,args);
}
}
- consumer
controller
package com.zpark.consumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("/")
public class ConsumerController {
private String SERVICEURL = "http://localhost:8081/findall";
@Autowired
RestTemplate template;
@RequestMapping("findall")
public String findAll() {
//直接调用
final String template = template.getForObject(SERVICEURL, String.class);
return template;
}
}
启动类
package com.zpark.consumer;
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 ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
4、测试
- 先启动service在启动consumer,在网页上访问http://localhost:8083/findall,网页显示所有查询结果。