环境
- spring cloud Edgware.SR6
- jdk 7
- sts 4.6.0
- mysql 5.7
背景
将支付微服务注册到 eureka 中,为了简单,eureka 只启动了一台,即单机版。
搭建步骤
增加 pom.xml 依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
第一个节点
sserver:
port: 4420
spring:
application:
name: payment
datasource:
url: jdbc:mysql://localhost/spring_cloud_payment?useUnicode=true&characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: root
password: jiangbo
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
第二个节点
server:
port: 4421
spring:
application:
name: payment
datasource:
url: jdbc:mysql://localhost/spring_cloud_payment?useUnicode=true&characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: root
password: jiangbo
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
第三个节点
server:
port: 4422
spring:
application:
name: payment
datasource:
url: jdbc:mysql://localhost/spring_cloud_payment?useUnicode=true&characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: root
password: jiangbo
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
启动类
package jiangbo.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class JiangBoApplication {
public static void main(String[] args) {
SpringApplication.run(JiangBoApplication.class, args);
}
}
验证
访问 http://localhost:8761/ ,能看到如下的结果,即结果正确。
附录
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>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Edgware.SR6</version>
</parent>
<groupId>jiangbo.springcloud</groupId>
<artifactId>07spring-cloud-payment</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>