• SpringCloud Netflix(二):服务提供者和服务消费者


    什么是服务提供者和服务消费者

    服务提供者:服务的被调用方,即为其他服务提供服务的服务。

    服务消费者:服务的调用方,即依赖其他服务的服务。

    服务提供者也可以是服务消费者,两者不是唯一性的

    服务提供者和消费者

    服务提供者和服务消费者实例

    1.创建一个maven项目

    在项目pom.xml里添加统一依赖,并把打包方式改为pom

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.example</groupId>
        <artifactId>springcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>pom</packaging>
        <name>springcloud</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.8.RELEASE</version>
            <relativePath />
        </parent>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Finchley.SR2</spring-cloud.version>
            <mysql.version>8.0.19</mysql.version>
            <druid.version>1.1.10</druid.version>
            <mybatis.plus.version>3.1.0</mybatis.plus.version>
            <mybatis.plus.generator.version>3.2.0</mybatis.plus.generator.version>
            <velocity.engine.version>2.2</velocity.engine.version>
            <lombok.version>1.18.12</lombok.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>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
    
                <dependency>
                    <groupId>com.example</groupId>
                    <artifactId>springcloud-dao</artifactId>
                    <version>0.0.1-SNAPSHOT</version>
                </dependency>
    
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>${mysql.version}</version>
                </dependency>
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>druid-spring-boot-starter</artifactId>
                    <version>${druid.version}</version>
                </dependency>
    
                <dependency>
                    <groupId>com.baomidou</groupId>
                    <artifactId>mybatis-plus-boot-starter</artifactId>
                    <version>${mybatis.plus.version}</version>
                </dependency>
    
                <dependency>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                    <version>${lombok.version}</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

     

    2.在项目下创建数据库操作的module,即存放实体类和mapper的模块

    添加依赖,在项目的统一依赖定义过版本的依赖无需再定义版本

    <?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">
        <parent>
            <artifactId>springcloud</artifactId>
            <groupId>com.example</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.example</groupId>
        <artifactId>springcloud-dao</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
        <name>springcloud-dao</name>
    
        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <!--该服务使用了mybatis-plus来简化代码-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
        </dependencies>
    </project>

    创建实体类

    package com.example.springcloud.domain;
    
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.extension.activerecord.Model;
    import com.baomidou.mybatisplus.annotation.TableId;
    import java.io.Serializable;
    import lombok.Data;
    import lombok.EqualsAndHashCode;
    import lombok.experimental.Accessors;
    
    @Data
    @EqualsAndHashCode(callSuper = false)
    @Accessors(chain = true)
    public class User extends Model<User> {
    
        private static final long serialVersionUID=1L;
    
        /**
         * 主键
         */
        @TableId(value = "id", type = IdType.AUTO)
        private Long id;
    
        /**
         * 姓名
         */
        private String name;
    
        /**
         * 数据库名
         */
        private String dbName; 
    
    
        @Override
        protected Serializable pkVal() {
            return this.id;
        }
    
    }

    创建 UserMapper.xmlUserMapper接口

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.example.springcloud.mapper.UserMapper">
    
        <!-- 通用查询映射结果 -->
        <resultMap id="BaseResultMap" type="com.example.springcloud.domain.User">
            <id column="id" property="id" />
            <result column="name" property="name" />
            <result column="db_name" property="dbName" />
        </resultMap>
    
        <!-- 通用查询结果列 -->
        <sql id="Base_Column_List">
            id, name, db_name
        </sql>
    
    </mapper>
    package com.example.springcloud.mapper;
    
    import com.example.springcloud.domain.User;
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import org.apache.ibatis.annotations.Mapper;
    
    public interface UserMapper extends BaseMapper<User> {
    
    }

    3.在项目下创建服务提供者

    添加依赖

    <?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">
        <parent>
            <artifactId>springcloud</artifactId>
            <groupId>com.example</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.example</groupId>
        <artifactId>springcloud-provider-8888</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
        <name>springcloud-provider-8888</name>
    
        <dependencies>
            <!--引入springcloud-dao服务,就可以调用该服务下的类-->
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>springcloud-dao</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
            </dependency>
        </dependencies>
    </project>

    添加配置

    server:
      port: 8888
    
    spring:
      application:
        name: springcloud-provider #注册中心中的服务名
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai
        username: root
        password: "123456"
        type: com.alibaba.druid.pool.DruidDataSource

    创建service和controller(各个服务的包名最好统一 com.example.springcloud

    @Service
    public class UserServiceImpl implements UserService {
        //省略代码。。。。。。。。。。。。。
    }
    @RestController
    @RequestMapping("/user")
    public class IndexController {
        @Autowired
        private UserService userService;
    
        @GetMapping("/{id}")
        public User getUserById(@PathVariable("id")Long id){
            User user = userService.getUserById(id);
            return user;
        }
    
        @GetMapping("/")
        public List<User> getAllUser(){
            return userService.getAllUser();
        }
    
        //因为消费者传输的对象都是JSON类型,所以需加@RequestBody注解,否则会报错
        @PostMapping("/add")
        public int addUser(@RequestBody User user){ 
            return userService.addUser(user);
        }
    }

    创建启动类,需添加@MapperScan 注解,否则服务可能会找不到mapper接口

    @SpringBootApplication
    @MapperScan("com.example.springcloud.mapper")
    public class Provider_8888 {
    
        public static void main(String[] args) {
            SpringApplication.run(Provider_8888.class,args);
        }
    }

    4.在项目下创建服务消费者

    springcloud中由服务消费者调用服务提供者一共有两种方法rest和feign

    通过RestTemplate调用

     

    spring框架提供的RestTemplate类可用于在应用中调用rest服务,它简化了与http服务的通信方式,统一了RESTful的标准,封装了http链接, 我们只需要传入url及返回值类型即可。

    RestTemplate类是为调用REST服务而设计的,因此它的主要方法与REST的基础紧密相连,后者是HTTP协议的方法:HEAD、GET、POST、PUT、DELETE和OPTIONS。而RestTemplate类具有headForHeaders()、getForObject()、postForObject()、put()和delete()等方法。

    <?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">
        <parent>
            <artifactId>springcloud</artifactId>
            <groupId>com.example</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>springcloud-consumer</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>springcloud-dao</artifactId>
            </dependency>
        </dependencies>
    </project>

    添加配置

    server:
      port: 80

    创建RestTemplate的注入配置类,方便调用

    @Configuration
    public class BeanConfig {
        @Bean
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
    }

    创建controller调用服务提供者

    package com.example.springcloud.controller;
    
    import com.example.springcloud.domain.User;
    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;
    
    import java.util.List;
    
    @RestController
    @RequestMapping("/consumer")
    public class IndexController {
    c
        @Autowired
        private RestTemplate restTemplate;c
    
        // http://ip地址:调用服务的端口/[服务提供者controller类上的RequestMapping/]
        //如果服务提供者controller类上不加RequestMapping,[]里的路径可去掉
        private static final String URL = "http://localhost:8888/user/";
    
        @RequestMapping("/user/{id}")
        public User getUserById(@PathVariable("id")Integer id) {
            return restTemplate.getForObject(URL+id, User.class);
        }
    
        @RequestMapping("/user/list")
        public List<User> getAllUser(){
            return restTemplate.getForObject(URL, List.class);
        }
    
        @RequestMapping("/user/add")
        public int addUser(User dept){
            return restTemplate.postForObject(URL+"add", dept, Integer.class);
        }
    
    }

    创建启动类

    //停止SpringBootApplication数据连接池自动配置
    @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
    public class Consumer {
    
        public static void main(String[] args) {
            SpringApplication.run(Consumer.class, args);
        }
    
    }

    访问http://localhost/consumer/user/1 ,服务消费者则会通过 http://localhost:8888/user/1 获取返回值User

    通过Feign调用

    Feign 的英文表意为“假装,伪装,变形”, 是一个http请求调用的轻量级框架,可以以Java接口注解的方式调用Http请求,而不用像Java中通过封装HTTP请求报文的方式直接调用。Feign通过处理注解,将请求模板化,当实际调用的时候,传入参数,根据参数再应用到请求上,进而转化成真正的请求,这种请求相对而言比较直观。

    创建feign接口,对应服务提供者者controller的方法

    @Component
    @FeignClient(value = "SPRINGCLOUD-PROVIDER")//对应服务名,服务提供者者的spring.application.name
    @RequestMapping("/user")
    public interface UserFeignClient {
    
        @GetMapping("/{id}")
        User getUserById(@PathVariable("id") Integer id);
    
        @GetMapping("/")
        List<User> getAllUser();
    
        @PostMapping("/add")
        int addUser(@RequestBody User user);
    }

    通过feign调用服务提供者

    package com.example.springcloud.controller;
    
    import com.example.consumer.service.HystrixService;
    import com.example.springcloud.domain.User;
    import com.example.springcloud.feign.UserFeignClient;
    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 java.util.List;
    
    @RestController
    @RequestMapping("/consumer")
    public class IndexController {
    
        @Autowired
        private UserFeignClient userFeignClient;
    
    
        @RequestMapping("/user/{id}")
        public User getDeptById(@PathVariable("id")Integer id){
            return userFeignClient.getUserById(id);
        }
    
        @RequestMapping("/user/list")
        public List<User> getAllDept(){
            return userFeignClient.getAllUser();
        }
    
        @RequestMapping("/user/add")
        public int addDept(User user){
            return userFeignClient.addUser(user);
        }
    
    }

    创建启动类,添加@EnableFeignClients注解

    @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
    @EnableFeignClients(basePackages = {"com.example.springcloud"})//对应feign接口所在的包
    public class FeignConsumer {
    
        public static void main(String[] args) {
            SpringApplication.run(FeignConsumer.class, args);
        }
    
    }

    我的个人博客站

  • 相关阅读:
    ASP.NET应用程序开发
    SQL语句导入导出大全
    批量insert数据
    fiddler抓包时,出现的 Tunnel to ***** : 443
    fidder如何模拟设置断点
    fidder :filter过滤,捕捉指定会话
    vs2005生成注释的快捷键
    保护我们的眼睛,让网页中的文字更易读
    推荐一个FireFox 插件SQLite Manager
    SQLServer2005 xp_cmdshell存储的使用
  • 原文地址:https://www.cnblogs.com/gaofei200/p/12861099.html
Copyright © 2020-2023  润新知