<menu id="k0e80"><tt id="k0e80"></tt></menu>
  • 專注Java教育12年 全國咨詢/投訴熱線:400-8080-105

    首頁 hot資訊 兩個簡單的微服務案例

    兩個簡單的微服務案例

    更新時間:2021-10-29 10:30 來源:動力節點 瀏覽41次

    小編來給大家舉兩個微服務的例子:

    第一個項目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

     

    提交申請后,顧問老師會電話與您溝通安排學習

    免費課程推薦 >>
    技術文檔推薦 >>

    返回頂部
    XXX
    <menu id="k0e80"><tt id="k0e80"></tt></menu>