欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springcloud实战篇二之Eureka-client(服务提供者)创建

程序员文章站 2024-03-22 16:31:58
...

上篇已经搭建好了Eureka客户端,接下来为大家演示如何把利用Eureka-client把服务提供者注册进Eureka-server

下图为工程的目录结构

springcloud实战篇二之Eureka-client(服务提供者)创建

接下来再贴出主要的代码,其中简单的业务逻辑我就不贴了

1.application.yml文件

server:
  port: 8001
  
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
  type-aliases-package: com.yxf.springcloud.entities    # 所有Entity别名类所在包
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml                       # mapper映射文件
    
spring:
   application:
    name: MICROSERVICECLOUD-DEPT                        #对外暴露的服务(微服务)
   datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/clouddb1              # 数据库名称
    username: root
    password: 123456
    dbcp2:
      min-idle: 5                                           # 数据库连接池的最小维持连接数
      initial-size: 5                                       # 初始化连接数
      max-total: 5                                          # 最大连接数
      max-wait-millis: 200                                  # 等待连接获取的最大超时时间
      
eureka:
  client: #客户端注册进eureka服务列表内
    service-url: 
      defaultZone: http://localhost:1001/eureka
  instance:
    instance-id: 127.0.0.1    
    prefer-ip-address: true     # 访问路径可以显示IP地址     
 
info: 
  app.name: yxf-mymicroservicecloud
  company.name: www.yxf.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

2.pom.xml文件

<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>
  <parent>
    <groupId>com.yxf.springcloud</groupId>
    <artifactId>mymicroservicecloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>mymicroservicecloud-config-eureka-client-1001</artifactId>
  
  	<dependencies>
		<!-- SpringCloudConfig配置 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>
		<!-- 热部署插件 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>
</project>

3.服务启动类

package com.yxf.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

import com.yxf.springcloud.EurekaServer1001;

/**
 * EurekaServer服务器端启动类,接受其它微服务注册进来
 * 
 * @author yangxf  
 */
@SpringBootApplication 
@EnableEurekaServer    //表明这个eureka的服务端
public class EurekaServer1001
{
	public static void main(String[] args)
	{
		SpringApplication.run(EurekaServer1001.class, args);
	}
}

最后效果图

springcloud实战篇二之Eureka-client(服务提供者)创建