spring Cloud之服务提供者和服务消费者

来源:互联网 发布:mysql constraint 编辑:程序博客网 时间:2024/05/21 17:11
  • 编写服务提供者
  • 编写服务消费者

微服务构建的是分布式系统,各个微服务之间通过网络进行通信。一般我们用服务提供者和服务消费者来描述微服务之间的调用关系:

服务提供者
服务被调用方
服务消费者
服务的调用方

以售票系统为例,用户向12306售票系统发起购票请求,在进行购买业务之前,12306售票系统需要先调用用户微服务接口,查看用户的相关信息是否符合购买标准等相关信息,这里,用户微服务就是服务的提供者,售票系统微服务就是服务消费者。下面依次在Intellij idea中手动创建这两个微服务:
先创建用户微服务:
maven的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.0http://maven.apache.org/xsd/maven-4.0.0.xsd";><modelVersion>4.0.0</modelVersion><groupId>com.simonsfan.springcloud</groupId><artifactId>microservice-springcoud-user-provider</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><!-- 引入spring boot的依赖 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.3.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.7</java.version></properties><dependencies><!--支持web应该用开发,包括spring-mvc、jackson、tomcat、spring-webmvc--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--springboot Actuator提供了众多检测端点,了解应用程序的运行状况--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--配置mybatis启动器依赖--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.2.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.22</version></dependency></dependencies><!-- 引入spring cloud的依赖 --><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Camden.SR4</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><!-- 添加spring-boot的maven插件 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
生成项目后,把通过mybatis反向生成的实体和xml文件copy到相关目录(mybatis反向生成点这里),目录如图



这里要注意UserActivityInfoMapper.xml文件中<mapper namespace="com.simonsfan.study.mapper.UserActivityInfoMapper">一定要正确

接着编写启动类ProviderUserApplication:
@MapperScan("com.simonsfan.study.mapper")@SpringBootApplicationpublic class ProviderUserApplication {  public static void main(String[] args) {     SpringApplication.run(ProviderUserApplication.class);  }}
@SpringBootApplication是一个组合注解,它整合了@Configuration、@EnableAutoConfiguration和@ComponentScan注解,并且开启了springboot程序组件扫描和自动配置功能
@MapperScan注解用来扫描mapper接口。

接着编写UserController类:
@RestControllerpublic class UserController {  @Autowired  private UserActivityInfoMapper userActivityInfoMapper;@GetMapping("/{id}")public UserActivityInfo selectByPrimaryKey(@PathVariable int id) {  UserActivityInfo userActivityInfo = userActivityInfoMapper.selectByPrimaryKey(id);  return userActivityInfo;  }}
@RestController注解声明等价于@Controller和@ResponseBody注解联合作用,标识该类均返回一个rest风格的data;@GetMapping是spring4.3提供的新注解,等价于@RequestMapping(value="/{id}",method=RequestMethod.GET),同理还有@PostMapping()等。

接着编写application.yml或application.properties均可,这里是application.yml
server:  port: 8000# 数据库连接信息spring:  datasource:           url: jdbc:mysql://masterdb.ac.idc.pplive.cn:3306/pplive_activity?rewriteBatchedStatements=true&characterEncoding=utf-8           username: pp_activity           password: aAh!jqAw^jASFerqj           driver-class-name: com.mysql.jdbc.Driver# mybatis扫描信息mybatis:  typeAliasesPackage: com.simonsfan.study.bean  #实体对象所以在包  mapper-locations: classpath:mapper/*.xml       #xml文件位置
需要注意的是,yml文件中冒号后面要空一格,例如port: 8000冒号后面一定要空格再接8000
接下来可以启动ProviderUserApplication启动类。

测试:浏览器访问http://localhost:8000/1405050,返回
{
    "id": 1405050,
    "username": "testchen1",
    "actId": "yihaodian",
    "actName": "1号店活动",
    "eventId": "yhdloginsendvip",
    "eventName": "登录送vip"
}

这样,用户微服务就顺利完成。github代码:https://github.com/simonsfan/microservice-springcloud-user-provider.git
同理,可以编写售票系统的服务消费者。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.0http://maven.apache.org/xsd/maven-4.0.0.xsd";><modelVersion>4.0.0</modelVersion><groupId>com.itmuch.cloud</groupId><artifactId>microservice-consumer-movie</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><!-- 引入spring boot的依赖 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.3.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.7</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-actuator</artifactId></dependency></dependencies><!-- 引入spring cloud的依赖 --><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Camden.SR4</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><!-- 添加spring-boot的maven插件 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

编写UserActivityInfo类,我直接拿用户微服务中的实例
编写启动类ConsumerTicketApplication
@EnableDiscoveryClient@SpringBootApplicationpublic class ConsumerTicketApplication {  @Bean  public RestTemplate restTemplate() {      return new RestTemplate();  }  public static void main(String[] args) {      SpringApplication.run(ConsumerMovieApplication.class, args);  }}
其中@Bean是一个方法注解,作用是实例化一个Bean并使用该方法的名称命名,本例中等价于RestTemplate restTemplate = new RestTemplate()

接着创建TicketController:
@RestControllerpublic class TicketController {  @Autowired  private RestTemplate restTemplate;  @GetMapping("/user/{id}")  public UserActivityInfo findById(@PathVariable Long id) {      return this.restTemplate.getForObject("http://localhost:8000/"; + id, UserActivityInfo.class);  }}

编写application.yml文件:
server:  port: 8888

这样,售票微服务也完成了。启动ConsumerTicketApplication启动类
测试:浏览器访问:http://localhost:8888/user/1405050,结果如下:

{
    "id": 1405050,
    "username": "testchen1",
    "actId": "yihaodian",
    "actName": "1号店活动",
    "eventId": "yhdloginsendvip",
    "eventName": "登录送vip"
}

说明售票微服务已经可以正常使用RestTemplate和用户微服务API通信了。

github代码:https://github.com/simonsfan/microservice-springcloud-ticket-consumer.git