Spring Cloud【Finchley】-01服务提供者与服务消费者


在这里插入图片描述

Spring Cloud总览

在这里插入图片描述


概述

服务提供者: 服务的被调用发,为其他服务提供服务的服务
服务消费者: 服务的调用方,即依赖其他服务的服务


示例

场景描述

在这里插入图片描述

围绕该场景,需要两个微服务

  • 用户微服务,作为服务提供者为电影微服务提供服务
  • 电影微服务,作为消费者调用用户微服务提供的服务

用户微服务

Spring Boot 2.1.1
Spring Data JPA
H2
lombok简化编码


新建Spring Boot服务

在这里插入图片描述

在这里插入图片描述


项目结构

在这里插入图片描述

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

	<name>micorservice-simple-provider-user</name>
	<description>Demo project for Spring Cloud</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.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>
		
		<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.4</version>
			<scope>provided</scope>
		</dependency>
		
	</dependencies>

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


</project>

用户库表

简单起见,我们使用H2数据库

在这里插入图片描述

schema.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)
);

data.sql

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);

Model

为了简化代码,使用了lombok的@Data注解

package com.artisan.microservice.model;

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;

import lombok.Data;


@Entity
@Data
public class User implements Serializable {
	
	private static final long serialVersionUID = 226695758444267342L;

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id ;
	
	@Column
	private String username;
	
	@Column
	private String name;
	
	@Column
	private Integer age;
	
	@Column
	private BigDecimal balance;
	
	
}


Dao层

使用Spring Data JPA操作数据库

package com.artisan.microservice.repository;

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

import com.artisan.microservice.model.User;

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

}

Service

按道理都应该有Service层,这里demo比较简单,省略也可以,直接在Controller层调用Dao层


Controller 暴露Rest API

package com.artisan.microservice.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.artisan.microservice.model.User;
import com.artisan.microservice.repository.UserRepository;

@RestController
public class UserController {

  @Autowired
  private UserRepository userRepository;

  @GetMapping("/user/{id}")
  public User findById(@PathVariable Long id) {
	  User user = new User();
      user.setId(id);
      
      User user2 = new User();
      user2.setName("no this user");
      // 我们使用的spring boot2.1.1版本中关联使用的spring data jpa不再支持findone(id)方法,改成如下写法
      return userRepository.findOne(Example.of(user)).orElse(user2); 
  }
}


配置文件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.artisan: DEBUG

测试

package com.artisan.microservice;

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

@SpringBootApplication
public class MicorserviceSimpleProviderUserApplication {

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

右键–Run As --Spring Boot App,启动成功后


访问数据库中存在的用户
http://localhost:7900/user/1

{"id":1,"username":"user1","name":"张三","age":20,"balance":100.00}

http://localhost:7900/user/2

{"id":2,"username":"user2","name":"李四","age":20,"balance":100.00}

http://localhost:7900/user/3

{"id":3,"username":"user3","name":"王五","age":20,"balance":100.00}

http://localhost:7900/user/4

{"id":4,"username":"user4","name":"马六","age":20,"balance":100.00}

访问数据库中不存在的一个用户
http://localhost:7900/user/66

{"id":null,"username":null,"name":"no this user","age":null,"balance":null}

电影微服务

新建Spring Boot服务

如用户微服务,只不过我们这里仅仅作为消费者,仅使用web即可。


项目结构

在这里插入图片描述

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.artisan</groupId>
	<artifactId>micorservice-simple-consumer-movie</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>micorservice-simple-consumer-movie</name>
	<description>Demo project for Spring Cloud</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.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>
		
		<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.4</version>
			<scope>provided</scope>
		</dependency>
		
	</dependencies>

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


</project>


Controller通过 RestTemplate 调用用户微服务提供的服务

因为仅仅作为服务消费者,所有只要在Controller层调用对应的rest接口即可

package com.artisan.microservice.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.artisan.microservice.model.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);
  }
}

用到了User类,新建个User.java

package com.artisan.microservice.model;

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

import lombok.Data;

@Data
public class User implements Serializable {

	private static final long serialVersionUID = 8912111288470833198L;
	private Long id;
	private String username;
	private String name;
	private Integer age;
	private BigDecimal balance;

}


启动类入口处,通过@Bean实例化RestTemplate

通过@Bean实例化RestTemplate

package com.artisan.microservice;

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 MicorserviceSimpleConsumerMovieApplication {
	
	@Bean
	public RestTemplate restTemplate(){
		return new RestTemplate();
	}
	
	public static void main(String[] args) {
		SpringApplication.run(MicorserviceSimpleConsumerMovieApplication.class, args);
	}
}

配置文件 application.yml

配置端口及用户微服务的地址

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

测试

启动用户微服务,访问7901

访问数据库中存在的用户
http://localhost:7901/movie/1

{"id":1,"username":"user1","name":"张三","age":20,"balance":100.00}

http://localhost:7901/movie/2

{"id":2,"username":"user2","name":"李四","age":20,"balance":100.00}

http://localhost:7901/movie/3

{"id":3,"username":"user3","name":"王五","age":20,"balance":100.00}

http://localhost:7901/movie/4

{"id":4,"username":"user4","name":"马六","age":20,"balance":100.00}

访问数据库中不存在的一个用户
http://localhost:7901/movie/66

{"id":null,"username":null,"name":"no this user","age":null,"balance":null}

成功调用到了用户微服务对外提供的服务。


示例的缺点

不难发现,虽然我们把用户微服务对外提供的接口地址配置在了配置文件中,然后通过@Value的方式去加载该属性,但是一旦用户微服务修改了地址,电影微服务作为消费者也要修改对应的地址,多了的话,也是很难管理

第二个原因,如果存在多个用户微服务,如何进行负载? nginx固然可以,如果这种服务比较多,依赖nginx, 增加节点还是需要修改nginx配置文件,比较头疼

所以 接下来我们来看下服务发现与服务注册,这里主要说的是Spring Cloud支持比较好的Eureka

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
项目说明 该项目是一个典型的由Spring Cloud管理的微服务项目,主要包括如下模块 micro-service-cloud─────────────────顶层项目 ├──cloud-service-core───────────────基础核心模块 ├──cloud-service-tools──────────────全局通用工具类 ├──cloud-service-reids──────────────Redis二次封装 ├──cloud-eureka-server──────────────服务注册中心[8761] ├──cloud-turbine-server─────────────断路器聚合监控[8769] ├──cloud-zipkin-server──────────────链路追踪监控[9411] ├──cloud-zuul-server────────────────第一代服务网关(Zuul)[8080] ├──cloud-gateway-server─────────────第二代服务网关(Gateway)[8080] ├──cloud-modules-app────────────────App微服务模块 ├───────modules-app-user────────────App用户服务模块[努力更新中] ├───────modules-app-doctor──────────App医生服务模块[努力更新中] ├──cloud-modules-service────────────微服务通用服务模块 ├───────mongodb-file-service────────Mongodb文件服务模块[11010] ├───────redis-delay-service─────────延迟消费服务模块[11020] ├──cloud-modules-web────────────────Web微服务模块 ├───────modules-web-security────────Web医生服务模块[12010] ├───────modules-web-user────────────Web用户服务模块[12020] ├──cloud-modules-wechat─────────────Wechat微服务模块 ├───────modules-wechat-user─────────Wechat用户服务模块[努力更新中] └───────modules-wechat-doctor───────Wechat医生服务模块[努力更新中] 修改日志 修改日志 修改人 修改日期 版本计划 V1.0 刘岗强 2019-01-07 项目初始化 V1.1 刘岗强 待定 新增自动问答 项目介绍 基于Spring Cloud Finchley SR2 Spring Boot 2.0.7的最新版本。 核心基础项目内实现类自定义的权限注解,配合RBAC权限模型+拦截器即可实现权限的控制,具体的参考项目中的实现。同时也封装了一些顶层类和结果集等。 注册中心实现高可用配置,详情见eureka的one、two、three三个配置文件,摘要如下。 ------------------------------------------配置节点一---------------------------------------------- server: port: 8761 spring: application: name: cloud-eureka-server eureka: instance: hostname: cloud.server.one prefer-ip-address: true instance-id: ${spring.cloud.client.ip-address}:${server.port}:${spring.application.name} client: healthcheck: enabled: true register-with-eureka: false fetch-registry: false service-url: defaultZone: http://cloud.server.two:8762/eureka/,http://cloud.server.three:8763/eureka/ ------------------------------------------配置节点二----------------------------------------------
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小工匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值