<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>jymcloudservice</artifactId>
<groupId>com.jym</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../jymcloudservice/pom.xml</relativePath>
</parent>
<groupId>com.jym</groupId>
<artifactId>jymcloudservice-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<artifactId>jymcloudservice-common</artifactId>
<groupId>com.jym</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>1.5.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
package com.jym.deptprovider;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @program: cloudTest
* @description:
* @author: jym
* @create: 2020/02/25
*/
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@MapperScan("com.jym.deptprovider.dao")
@EnableEurekaClient //开启eureka客户端
@EnableDiscoveryClient // 开启注册发现
public class JymProvider8001 {
public static void main(String[] args) {
SpringApplication.run(JymProvider8001.class,args);
}
}
server:
port: 8001
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: com.jym.jymcloudservice.entity # 所有Entity别名类所在包
mapper-locations:
- classpath:mybatis/mapper/*.xml # mapper映射文件
spring:
application:
name: jymservicecloud-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: com.mysql.jdbc.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/clouddb01?useSSL=true&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 # 数据库名称
username: root
password: admin
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-total: 5 # 最大连接数
max-wait-millis: 200 # 等待连接获取的最大超时时间
eureka:
client: #客户端注册进eureka服务列表内
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: jymservicecloud-dept8001 #修改eureka Status中的内容
prefer-ip-address: true # 超链接IP提示
# 超链接内容
info:
app.name: jym-deptservicecloud
company.name: com.jym.springcloud
build.artifactId: $project.artifactId$
build.version: $project.version$
defaultZone: 服务注册地址(Eureka服务的地址)。
instance-id: 修改页面Status中的内容
prefer-ip-address: 鼠标悬浮时超链接IP提示(左下角)
但是设置完,点进去报错,因为还没设置info
父pom文件中需要安装插件:
该插件可以读取$$之间的内容
<build>
<finalName>jymcloudservice</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimit>$</delimit>
</delimiters>
</configuration>
</plugin>
</plugins>
</build>
info:
app.name: jym-deptservicecloud
company.name: com.jym.springcloud
build.artifactId: $project.artifactId$
build.version: $project.version$
这里的$$内容可以从配置文件中读取
配置完成之后,点击超链接:
package com.jym.deptprovider;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @program: cloudTest
* @description:
* @author: jym
* @create: 2020/02/25
*/
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@MapperScan("com.jym.deptprovider.dao")
@EnableEurekaClient //开启eureka客户端
@EnableDiscoveryClient // 开启注册发现
public class JymProvider8001 {
public static void main(String[] args) {
SpringApplication.run(JymProvider8001.class,args);
}
}
package com.jym.deptprovider.controller;
import com.jym.deptprovider.service.DeptService;
import com.jym.jymcloudservice.entity.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @program: cloudTest
* @description:
* @author: jym
* @create: 2020/02/25
*/
@RestController
@RequestMapping("/dept")
public class DeptController {
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping(value = "/discovery",method = RequestMethod.GET)
public Object discovery () {
List<String> services = discoveryClient.getServices();
System.out.println(services);
List<ServiceInstance> instances = discoveryClient.getInstances("JYMSERVICECLOUD-DEPT");
for (ServiceInstance instance : instances) {
System.out.println("id为"+instance.getServiceId());
System.out.println(instance.getHost()+":"+instance.getPort()+"/"+instance.getUri());
}
return this.discoveryClient;
}
}