微服務 : 使用eclipse 來搭建一個簡單的微服務
下載地址:https://download.csdn.net/download/qq_18430613/10642372
1.先看看整體專案的一個架構,parent 專案父依賴包,用於管理我們的依賴,provider 服務提供方,consumer 消費者。
2.建立parent 專案
1)建立無骨架的maven pom專案
2)修改pom包 如下步驟:
https://projects.spring.io/spring-cloud/ 修改maven parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<!-- 新增spring-cloud 依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 定義JDK編譯環境 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!-- 新增子模板 -->
<modules>
<module>../provider</module>
</modules>
3.新增provider 服務提供專案
右鍵選擇》new 》spring-start-project
修改provider服務提供方pom檔案如下:
修改application.properties檔案為yml 檔案(紅色部分為大小寫轉換)
編寫程式碼部分:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public UserInfo getUserInfo(Integer userId) {
return userMapper.getUserById(userId);
}
}
@Mapper
public interface UserMapper {
@Select("SELECT user_id,user_name,user_birth FROM tb_user WHERE user_id = #{userId}")
public UserInfo getUserById(@Param(value = "userId") Integer userId);
}
public class UserInfo implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer userId;
private String userName;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date userBirth;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Date getUserBirth() {
return userBirth;
}
public void setUserBirth(Date userBirth) {
this.userBirth = userBirth;
}
}
服務提供方到此完成 可以啟動訪問:http://localhost:8080/book/books
4.新增消費consumer專案,直接複製provide修改相關檔案如下
provider-consumer pom檔案修改
新增程式碼部分:
@SpringBootApplication
@RestController
public class ProviderApplication {
@Value("${spring.application.name}")
private String applicationName;
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/warp/get/{userId}")
public UserInfo getUser(@PathVariable Integer userId) {
return restTemplate.getForObject("http://localhost:8099/user/get/"+userId, UserInfo.class);
}
}
public class UserInfo implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer userId;
private String userName;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date userBirth;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Date getUserBirth() {
return userBirth;
}
public void setUserBirth(Date userBirth) {
this.userBirth = userBirth;
}
}
//application.yml檔案
server:
port: 8100
spring:
application:
name: provider-consumer
測試:http://localhost:8100/user/warp/get/1