四、服務註冊&服務提供者

一、 什麼是服務提供者
服務提供者(Service Provider):是指服務的被調用方(即:爲其它服務提供服務的服務);服務提供者,做爲一個Eureka Client,向Eureka Server作服務註冊、續約和下線等操做,註冊的主要數據包括服務名、機器ip、端口號、域名等等。java

clipboard.png

從圖中能夠到Eureka 有兩種服務實例,分別爲Eureka Server和Eureka Client;並且Eureka Client又分爲兩種類型:Service Provider(服務提供者)和Service Consumer(服務消費者),若是學過dubbo發現這個圖跟dubbo的調用關係圖比較相似。
二、 新建meven項目mysql

<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>spring-cloud</groupId>
    <artifactId>sc-eureka-client-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>sc-eureka-client-provider</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- 說明是一個 eureka client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

        <!-- spring boot實現Java Web服務 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 把tomcat-jdbc鏈接池排除掉,這樣spring-boot就會尋找是否有HikariCP可用 -->
            <exclusions>
                <exclusion>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.3</version>
        </dependency>


        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

    </dependencies>
</project>

clipboard.png

三、 建立ProviderApplication.java類web

package sc.provider;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
@MapperScan(basePackages="sc.provider.dao")
public class ProviderApplication {

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

Mybatis的註解MapperScan掃描mapper文件所在的包spring

四、 建立application.yml文件sql

server:
  port: 8200

spring:
  application:
    name: sc-eureka-client-provider
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/sc?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root
    type: com.zaxxer.hikari.HikariDataSource
    hikari:
      minimum-idle: 5
      maximum-pool-size: 15
      auto-commit: true
      idle-timeout: 30000
      pool-name: DatebookHikariCP
      max-lifetime: 1800000
      connection-timeout: 30000
      connection-test-query: SELECT 1
 
eureka:
  client:
    registerWithEureka: true #是否將本身註冊到Eureka服務中,默認爲true
    fetchRegistry: true #是否從Eureka中獲取註冊信息,默認爲true
    serviceUrl:
      defaultZone: http://localhost:5001/eureka/

mybatis:
  mapperLocations: classpath:sc/provider/dao/*.xml
  #configLocation: classpath:mybatis-config.xml

Spring Cloud 2.x之後默認使用Hikari數據源,一個很是高效的數據源。數據庫

五、 其餘須要建立的相關類請看下面apache

clipboard.png

六、 建立數據庫sc和對應的表t_user(sql腳步參考項目中的sql文件)tomcat

七、 啓動Eureka,對應的項目爲sc-eureka-server;而後啓動sc-eureka-client-provider
方式一:mybatis

clipboard.png

方式二:app

clipboard.png

八、 驗證是否成功
添加:

clipboard.png

查詢:

clipboard.png

列表:

clipboard.png

更新:

clipboard.png

刪除:

clipboard.png

相關文章
相關標籤/搜索