SpringCloud(一)构建SpringBoot微服务

本篇文章将会展示两个springboot项目,一个是provider(服务提供者),另一个是consumer(服务消费者).基本思路是provider通过查询数据库返回信息,consumer通过rest的方式访问provider.

下面看代码:

Provider

DemoMicroserviceProviderUserApplication.java

package com.zhuyang.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
//@EnableDiscoveryClient
public class DemoMicroserviceProviderUserApplication {

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

package com.zhuyang.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 Short age;
	@Column
	private BigDecimal balance;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Short getAge() {
		return age;
	}

	public void setAge(Short age) {
		this.age = age;
	}

	public BigDecimal getBalance() {
		return balance;
	}

	public void setBalance(BigDecimal balance) {
		this.balance = balance;
	}

}

UserRepository.java

package com.zhuyang.cloud.repository;

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

import com.zhuyang.cloud.entity.User;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

}
UserController.java

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

@RestController
public class UserController {
	@Autowired
	private UserRepository userRepository;

	@GetMapping(path = "/simple/{id}") //http://localhost:8001/simple/1
	public User findByid(@PathVariable Long id) {
		return this.userRepository.findOne(id);
	}
}
application.yml

server:
  port: 8001  ##port
spring:
  jpa:  ##use jpa for repository
    generate-ddl: false
    show-sql: true
    hibernate:
      ddl-auto: none
  datasource:
    platform: h2 ##use h2 as database
    schema: classpath:schemal.sql  ##DDL 
    data: classpath:data.sql  ##DML
  application:
    name: miciroservice-provider ##application name
logging:
  level:
    root: info
    org.hibernate: info
    org.hibernate.type.descriptor.sql.BasicBinder: TRACE
    org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
    com.zhuyang: debug
data.sql

insert into user (id,username,name,age,balance) values(1,'yz45177','zhuyang',28,120.7);
insert into user (id,username,name,age,balance) values(2,'xj78921','lucy',32,203.5);
insert into user (id,username,name,age,balance) values(3,'xh78273','xiuhui',35,120.4);
insert into user (id,username,name,age,balance) values(4,'js21242','jason',29,203.2);
insert into user (id,username,name,age,balance) values(5,'gg21122','guzong',40,557.6);

schemal.sql

drop table user if exists;
create table user(
	id bigint generated by default as identity primary key,
	username varchar2(40),
	name varchar2(20),
	age int(3),
	balance decimal(10,2)

);


运行DemoMicroserviceProviderUserApplication.java 并在浏览器访问http://localhost:8001/simple/1 返回下面的结果

<User>
<id>1</id>
<username>yz45177</username>
<name>zhuyang</name>
<age>28</age>
<balance>120.70</balance>
</User>


Consumer

DemoMicroserviceConsumerUserApplication.java

package com.zhuyang.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class DemoMicroserviceConsumerUserApplication {
	@Bean
	@LoadBalanced //ribbon
	public RestTemplate restTemplate() {  // equals to RestTemplate restTemplate = new RestTemplate()
		return new RestTemplate();
	}

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

MovieController.java

package com.zhuyang.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 org.springframework.web.client.RestTemplate;

import com.zhuyang.cloud.entity.User;


@RestController
public class MovieController {

	@Autowired
	private RestTemplate restTemplate;

	@GetMapping(path = "/movie/{id}")
	public User findByid(@PathVariable Long id) {
               //通过restTemplate访问provider服务

		return this.restTemplate.getForEntity("http://localhost:8001/simple/" + id, User.class).getBody();
		
	}
}

通过访问http://localhost:8000/movie/1

<User>
<id>1</id>
<username>yz45177</username>
<name>zhuyang</name>
<age>28</age>
<balance>120.70</balance>
</User>






阅读更多
文章标签: springcloud springboot
个人分类: springcloud
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭