【SpringCloud】(一):SpringCloud入門程式

00潤物無聲00發表於2017-08-13

什麼是SpringCloud,和SpringBoot的關係

官網:http://projects.spring.io/spring-cloud/


--------------------------------------------------------------------------------------------------

spring Cloud是一個基於Spring Boot實現的雲應用開發工具,它為基於JVM的雲應用開發中的配置管理、服務發現、斷路器、智慧路由、微代理、控制匯流排、全域性鎖、決策競選、分散式會話和叢集狀態管理等操作提供了一種簡單的開發方式。

Spring boot 是 Spring 的一套快速配置腳手架,可以基於spring boot 快速開發單個微服務,Spring Cloud是一個基於Spring Boot實現的雲應用開發工具;Spring boot專注於快速、方便整合的單個個體,Spring Cloud是關注全域性的服務治理框架;spring boot使用了預設大於配置的理念,很多整合方案已經幫你選擇好了,能不配置就不配置,Spring Cloud很大的一部分是基於Spring boot來實現。Spring boot可以離開Spring Cloud獨立使用開發專案,但是Spring Cloud離不開Spring boot,屬於依賴的關係。


spring -> spring booot > spring cloud 這樣的關係。

作者:純潔的微笑
連結:https://www.zhihu.com/question/47304987/answer/169239519
來源:知乎

--------------------------------------------------------------------------------------------------



使用建立服務

  http://start.spring.io/


服務提供者:使用者微服務



服務消費者:電影微服務





服務提供者:microservice-simple-provider-user


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.dynamic.cloud</groupId>
	<artifactId>microservice-simple-provider-user</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>microservice-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.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-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>

User:使用jpa,加上相應註解

package com.dynamic.cloud.entity;

import java.io.Serializable;
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 implements Serializable
	{
		@Id 
		@GeneratedValue(strategy = GenerationType.AUTO)
		private Long id;
		@Column
		private String username;
		@Column
		private String name;
		@Column
		private int age;
		@Column
		private BigDecimal balance;
	}	

UserRepository

package com.dynamic.cloud.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.dynamic.cloud.entity.User;

@Repository
public interface UserRepository extends JpaRepository<User,Long>
{
	public User findById(Long id);
}

UserController

package com.dynamic.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 com.dynamic.cloud.entity.User;
import com.dynamic.cloud.repository.UserRepository;

@RestController
public class UserController
{

	@Autowired
	private UserRepository userRepository;

	@GetMapping("/simple/{id}")
	public User findById(@PathVariable Long id)
	{
		return this.userRepository.findById(id);
	}
}

啟動類

MicroserviceSimpleProviderUserApplication

package com.dynamic.cloud;

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

@SpringBootApplication
public class MicroserviceSimpleProviderUserApplication {

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

配置檔案:application.yml

server:
  port: 7900
spring:
  jpa:
    generate-ddl: false
    show-sql: true
    hibernate:
      ddl-auto: none
  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
    com.dynamic: DEBUG

資料檔案:schema.sql和data.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)
);

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', '李四', 20, 100.00);
insert into user(id,username, name, age, balance) values(3,'user3', '王五', 20, 100.00);
insert into user(id,username, name, age, balance) values(4,'user4', '馬六', 20, 100.00);


服務提供的埠是7900



服務消費者:microservice-simple-comsumer-movie

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.dynamic.cloud</groupId>
	<artifactId>microservice-simple-comsumer-movie</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>microservice-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.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>
	</dependencies>

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


</project>

User

只是呼叫不需要註解

package com.dynamic.cloud.entity;

import java.io.Serializable;
import java.math.BigDecimal;

public class User implements Serializable{
 
	private Long id;
 
	private String username;
 
	private String name;
 
	private int age;
 
	private BigDecimal balance;
}


Controller

使用restTemplate進行呼叫,RestTemplate有點類似於一個WebService客戶端請求的模版,可以呼叫http請求的WebService,並將結果轉換成相應的物件型別

package com.dynamic.cloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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 com.dynamic.cloud.entity.User;

@RestController
public class MovieController {
	@Autowired
	private RestTemplate restTemplate;
	
	@Value("${user.userServicePath}")
	private String userServicePath;
	
	@GetMapping("/movie/{id}")
	public User findById(@PathVariable Long id)
	{
		return this.restTemplate.getForObject(this.userServicePath+id, User.class);
	}

}

啟動類

package com.dynamic.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 MicroserviceSimpleComsumerMovieApplication {

	@Bean
	public RestTemplate restTemplate()
	{
		return new RestTemplate(); //例項化RestTemplate
	}
	
	public static void main(String[] args) {
		SpringApplication.run(MicroserviceSimpleComsumerMovieApplication.class, args);
	}
}

配置檔案:application.yml

server:
  port: 7901
user:
  userServicePath: http://localhost:7900/simple/


消費端的埠是7901,請求7900的服務




啟動時去掉該選項。




總結:

  SpringCloud基於SpringBoot,為微服務體系開發中的架構問題,提供了一整套的解決方案——服務註冊與發現,服務消費,服務保護與熔斷,閘道器,分散式呼叫追蹤,分散式配置管理等。


相關文章