springCloud学习笔记(三)——eureka服务消费者
一、项目搭建
1.引入jar
<!-- 引入eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.完整的pom文件内容
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>com.springcloud</artifactId>
<groupId>com.sc2020</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>consumer-eureka-client81</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>consumer-client81</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.sc2020</groupId>
<artifactId>cloud-payment8001</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sc2020</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</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>runtime</scope>
<optional>true</optional>
</dependency>
<!-- 引入eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
3.yml文件配置
server:
port: 81
eureka:
client:
register-with-eureka: true #是否将自己注册到注册中心,集群必须设置为true配合ribbon
fetch-registry: true #是否从服务端抓取已有的注册信息
service-url:
defaultZone: http://eureka7001.com:7001/eureka #hosts文件配置了eureka7001.com,如果没有配置则使用localhost
instance:
instance-id: consumer-client81
prefer-ip-address: true
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://101.34.49.127:3306/cloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: 你的数据库用户
password: 你的数据用户密码
application:
name: consumer-client
mybatis:
mapper-locations: classpath:mapper/*.xml
4.Application中main方法
@SpringBootApplication
@EnableEurekaClient
public class ConsumerEurekaClient81Application {
public static void main(String[] args) {
SpringApplication.run(ConsumerEurekaClient81Application.class, args);
}
}
注意:使用@EnableEurekaClient注解声明使用eureka组件
5.Controller控制层方法
@RestController
public class TestController {
@Resource
RestTemplate restTemplate;
//你的服务提供者的名称
public static final String PAYMENT_URL = "http://cloud-eureka-client";
@GetMapping(value = "getMessage")
public String getMessage(){
//你项目服务提供者的接口路径
String forObject = restTemplate.getForObject( PAYMENT_URL +"/paymentController/getPortMessage", String.class);
return forObject;
}
}
6.请求访问
请求访问CLOUD-EUREKA-CLIENT7003接口