温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

4、服务注册&服务提供者

发布时间:2020-08-24 11:23:24 来源:网络 阅读:675 作者:huangjinjin520 栏目:软件技术

公众号: java乐园

1、 什么是服务提供者
服务提供者(Service Provider):是指服务的被调用方(即:为其它服务提供服务的服务);服务提供者,作为一个Eureka Client,向Eureka Server做服务注册、续约和下线等操作,注册的主要数据包括服务名、机器ip、端口号、域名等等。
4、服务注册&服务提供者
从图中可以到Eureka 有两种服务实例,分别为Eureka Server和Eureka Client;而且Eureka Client又分为两种类型:Service Provider(服务提供者)和Service Consumer(服务消费者),如果学过dubbo发现这个图跟dubbo的调用关系图比较类似。
2、 新建meven项目

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

4、服务注册&服务提供者

3、 创建ProviderApplication.java类

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文件所在的包

4、 创建application.yml文件

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数据源,一个非常高效的数据源。

5、 其他需要创建的相关类请看下面
4、服务注册&服务提供者
6、 创建数据库sc和对应的表t_user(sql脚步参考项目中的sql文件)

7、 启动Eureka,对应的项目为sc-eureka-server;然后启动sc-eureka-client-provider
方式一:
4、服务注册&服务提供者

方式二:
4、服务注册&服务提供者
8、 验证是否成功
添加:
4、服务注册&服务提供者

查询:
4、服务注册&服务提供者

列表:
4、服务注册&服务提供者
更新:
4、服务注册&服务提供者
删除:
4、服务注册&服务提供者

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI