Spring Cloud学习笔记2-入门示例

开发套路放在前:
a 加依赖

b 加注解

c 写配置


1 先写一个微服务【服务提供者】

  • 依赖
<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>sam.zeng</groupId>
	<artifactId>springCloud-provider</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<!-- 引入spring boot的依赖 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.7.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
	
	<!-- 添加spring-boot的maven插件,不能少,打jar包时得用 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>
  • 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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import sam.zeng.entity.User;
import sam.zeng.service.UserService;

@RequestMapping("/users")
@RestController
public class UserController {
  @Autowired
  private UserService userService;

  @GetMapping("/{id}")
  public User findById(@PathVariable Long id) {
    return userService.findById(id);
  }
}
  • Service层
import sam.zeng.entity.User;

public interface UserService {

	User findById(Long id);
	
}
import org.springframework.stereotype.Service;

import sam.zeng.entity.User;
import sam.zeng.service.UserService;

@Service
public class UserServiceImpl implements UserService{

	@Override
	public User findById(Long id) {
		return new User(id, "testUserName", "testName", 20);
	}

}
  • 实体(注意加构造函数和set get方法)
public class User {
	private Long id;
	private String username;
	private String name;
	private Integer age;
}
  • 启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProviderApp {

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

}

就是一个正常的rest风格的服务,没啥高大上的,暂时和spring-cloud没有半毛钱的关系

因为只是一个简单的示例,所以,DAO层就省略了,方便节省各位看官的脑仁,哈哈。。。

还是贴上目录结构吧

2 再写一个微服务【服务消费者】

  • 依赖同上
<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>sam.zeng</groupId>
	<artifactId>springCloud-consumer</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<!-- 引入spring boot的依赖 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.7.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
	<!-- 添加spring-boot的maven插件,不能少,打jar包时得用 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>
  • Service层(无)
  • 实体同上(注意加构造函数和set get方法)
public class User {
	private Long id;
	private String username;
	private String name;
	private Integer age;
}
  • controller层

请注意此类中注入的是RestTemplate,和服务提供者不一样(服务提供者注入的UserService)

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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import sam.zeng.entity.User;

@RequestMapping("/users")
@RestController
public class UserController {
	@Autowired
	private RestTemplate restTemplate;

	@GetMapping("/{id}")
	public User findById(@PathVariable Long id) {
		return restTemplate.getForObject("http://localhost:8080/users/{id}", User.class,id);
	}
}
  • 注解

既然在controller中要使用RestTemplate,所以需要配置bean,这里是唯一的一处spring cloud的JAVA配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class SpringCloudConfig {
	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}
}
  • 启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConsumerApp {

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

}
  • application.yml
server:
  port: 8081
  • 目录结构

  • 注意事项,

1 一定要加上实体的set和get方法,以及构造函数

2 一定要配置RestTemplate,否者在controller中注入的时候会找不到而报错

3 两个微服务的端口一定要不一样,记得修改application.yml


总结:

发现了没?目前就是两个正常的微服务之前的调用,消费使用RestTemplate调用了提供都的服务,貌似和spring cloud扯上了半毛钱的关系

关键代码是:

restTemplate.getForObject("http://localhost:8080/users/{id}", User.class,id);

这里用到了RestTemplate的占位符能力,需要注意
貌似这样我们就可以快乐的使用spring cloud了?
no,too young to simple!


发现的问题:
调用地址写死了,当你从测试环境部署到生产环境的时候,调用地地址肯定会变,还得改代码,麻烦!


还是先保存一下,再写一篇来解决掉这个问题吧

去尿一个,再不尿就爆了。。。

:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值