4. Service registration & service provider

Keywords: Spring Maven Mybatis Apache

Xiaobian public address: java paradise
Source code
https://gitee.com/hjj520/spring-cloud-2.x

1. What is a service provider
Service Provider: refers to the callee of the service (i.e. the service that provides services for other services); Service Provider, as an Eureka Client, performs service registration, renewal and offline operations to Eureka Server. The main registered data includes service name, machine ip, port number, domain name, etc.

From the figure, there are two kinds of service instances in Eureka, Eureka Server and Eureka Client. Moreover, Eureka Client can be divided into two types: Service Provider and Service Consumer. If you have studied dubbo, you will find that this figure is similar to dubbo's call graph.
2. New meven project

<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>
		<!-- Description is a 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 Realization Java Web service -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<!-- hold tomcat-jdbc The connection pool is removed, so spring-boot Will find out if there is HikariCP available -->
			<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>

3. Create ProviderApplication.java class

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 annotation MapperScan scans the package where the mapper file is located

4. Create application.yml file

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 #Whether to register yourself in Eureka service, the default is true
    fetchRegistry: true #Whether to get the registration information from Eureka? true by default
    serviceUrl:
      defaultZone: http://localhost:5001/eureka/

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

Spring Cloud 2.x will use Hikari data source by default, which is a very efficient data source.

5. For other related classes to be created, please see the following

6. Create database sc and corresponding table t u user (sql step refers to the sql file in the project)

7. Start Eureka, the corresponding project is SC Eureka server, and then start SC Eureka client provider
Method 1:

Mode two:

8. Verify success
Add to:

Enquiries:

List:

To update:

Delete:

Posted by shahryar on Thu, 21 Nov 2019 13:46:22 -0800