1. 程式人生 > >SpringBoot搭建微服務HelloWorld

SpringBoot搭建微服務HelloWorld

專案介紹

 優點:微服務其實就像資料庫的分庫分表一樣,不至於一個地方出錯有時候會導致整個系統癱瘓

專案分為2部分,一個是SpringBoot伺服器端,一個是Maven專案客戶端,客戶端呼叫伺服器端的URL從而完成伺服器端的與資料交換(此專案內建資料,不與資料庫發生資料交換)



注意:先啟動伺服器端,然後在執行客戶端的main方法

專案下載

點選開啟連結下載專案程式碼(超連結)

搭建伺服器端環境(SpringBoot專案)


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>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SpringBoot_Server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.7.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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--熱部署.jar ,當你修改程式碼後,伺服器會自動重啟 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.41</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>



實體類Student

package com.example.demo.entity;

public class Student {

	private String username;

	private String password;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String toString() {
		return "Student [username=" + username + ", password=" + password + "]";
	}

	public Student(String username, String password) {

		this.username = username;
		this.password = password;
	}

	public Student() {
	}

}



Controller(內建靜態資料,儲存在Map型別的students中)

package com.example.demo.handler;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.entity.Student;

@Controller
public class FirstController {

	public FirstController() {

		Student stu1 = new Student("張三1", "密碼1");
		Student stu2 = new Student("張三2", "密碼2");
		Student stu3 = new Student("張三3", "密碼3");
		students.put(1, stu1);
		students.put(2, stu2);
		students.put(3, stu3);
	}

	private Map<Integer, Student> students = new HashMap<>();

	@RequestMapping("selectStudentById")
	@ResponseBody
	public Student selectStudentById(int id) {
		return students.get(id);
	}

}

啟動類

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootServerApplication.class, args);
	}
}

伺服器端啟動後




客戶端(普通Maven專案)



pom.xml(Spring,fastjson)

<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.imooc</groupId>
	<artifactId>SpringBoot_Client</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<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</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.3.10.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>4.3.10.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.3.10.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.3.10.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.10.RELEASE</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.json/json <dependency> <groupId>org.json</groupId> 
			<artifactId>json</artifactId> <version>20160810</version> </dependency> -->
		<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.28</version>
		</dependency>





	</dependencies>

</project>


客戶端啟動類

package com.example.demo.start;



import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.example.demo.entity.Student;

public class Main {

	
	
	public static String BASE_URL = "http://localhost:8080";

	public static void main(String[] args) {

		 RestTemplate rs=new RestTemplate();
		 String str=rs.getForObject(BASE_URL+"/selectStudentById?id=2", String.class);
		 System.out.println("從伺服器端返回的JSON串:"+str);
		
		Student student=JSON.parseObject(str, Student.class);
		System.out.println("JSON轉換的自定義實體類物件型別:"+student.getClass());
		System.out.println("JSON轉換的自定義實體類物件:"+student);
	}

}

客戶端啟動類啟動結果