美文网首页
SpringCloudH第三章:服务提供者创建

SpringCloudH第三章:服务提供者创建

作者: Helloword_Cc | 来源:发表于2020-05-15 16:42 被阅读0次

    本章讲解springCloud服务提供者的创建

    服务则为简单的数据库查询操作(使用mybatis)开始操作吧。

    跟第二章一样:在parent下创建一个maven工程,你也可以直接创建spring工程。

    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">
        <parent>
            <artifactId>parent</artifactId>
            <groupId>com.cloudH</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <modelVersion>4.0.0</modelVersion>
        <packaging>jar</packaging>
        <artifactId>payMent8081</artifactId>
        <description>支付模块</description>
    
    
        <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.atguigu.springcloud.PaymentMain8001</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        </build>
    
        <dependencies>
            <!--包含了sleuth+zipkin-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zipkin</artifactId>
            </dependency>
            <!--<dependency>
                <groupId>com.atguigu.springcloud</groupId>
                <artifactId>cloud-api-common</artifactId>
                <version>${project.version}</version>
            </dependency>-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--eureka client-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <!--监控-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <!--如果没写版本,从父层面找,找到了就直接用,全局统一-->
                <version>${druid.spring.boot.starter.version}</version>
            </dependency>
            <!--mysql-connector-java-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <!--jdbc-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <!--热部署-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
        </dependencies>
    
    
    </project>
    
    

    添加aplication.yml
    1)设置端口
    2)设置服务名称
    3)设置向某个服务中心进行注册

    server:
      port: 8080
    spring:
      application:
       name: CLOUD-PROVIDER-HYSTRIX-PAYMENT
      datasource:
        # 当前数据源操作类型
        type: com.alibaba.druid.pool.DruidDataSource
        # mysql驱动类
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
        username: 
        password: 
    eureka:
      client:
        register-with-eureka: true
        fetch-registry: true
        service-url:
           defaultZone: http://localhost:7001/eureka
    
    mybatis:
      mapper-locations: classpath*:mapper/*.xml
      type-aliases-package: com.atguigu.springcloud.entities
    
    

    启动类

    /**
     * 支付模块启动类
     * EnableEurekaClient 开启eureka
     * EnableDiscoveryClient 开启服务发现
     *
     * @author zzyy
     * @create 2020/2/17 21:13
     **/
    @SpringBootApplication
    @EnableEurekaClient
    @EnableDiscoveryClient
    public class PaymentMain8001 {
        public static void main(String[] args) {
            SpringApplication.run(PaymentMain8001.class, args);
        }
    

    由于演示带数据库访问涉及到mybaties的代码过多,所以文章就直接controller返回成功。数据库访问代码在git上。
    服务提供的controller类

    package com.atguigu.springcloud.controller;
    import com.atguigu.springcloud.entities.CommonResult;
    import com.atguigu.springcloud.entities.Payment;
    import com.atguigu.springcloud.service.PaymentService;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.client.ServiceInstance;
    import org.springframework.cloud.client.discovery.DiscoveryClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    /**
     * @author zzyy
     * @date 2020/2/18 10:43
     **/
    @RestController
    @Slf4j
    public class PaymentController {
    
        @Value("${server.port}")
        private String serverPort;
    
        /**
         * 正常访问
         * @param id
         * @return
         */
        @GetMapping("/payment/hystrix/ok/{id}")
        public String paymentInfo_OK(@PathVariable("id") Integer id){
            return  "正常访问 😄";
        }
    }
    

    先启动第二章的Eureka服务注册中心,再启动我们的服务提供者,
    成功后访问Eureka服务器(localhost:7001),服务列表出现我们提供的服务
    CLOUD-PROVIDER-HYSTRIX-PAYMENT

    image.png
    gitHua地址:https://github.com/HelloWordChenC/springCloudH/tree/master/payMent8081

    相关文章

      网友评论

          本文标题:SpringCloudH第三章:服务提供者创建

          本文链接:https://www.haomeiwen.com/subject/opwcohtx.html