1.首先需要引入依赖
<!--eureka的客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--eureka-服务端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
/dependency>
2.在提供者的application.yml中进行eureka配置(这里使用了集群,就不一一列举了)
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: com.mysql.cj.jdbc.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
mybatis:
mapperLocations: classpath:mapper/*.xml
eureka:
client:
#表示是否将自己注册进Eurekaserver默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
#单机模式
#defaultZone:http://eureka7001.com:7001/eureka
#进行集群操作
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7003.com:7003/eureka
instance:
instance-id: payment8001
注册中心的application.yml配置
- 在这里说明以下,多个注册模块可以相互注册相互守望
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#集群指向其它eureka服务
defaultZone: http://eureka7003.com:7003/eureka/
#单机就是7001自己
#defaultZone: http://eureka7001.com:7001/eureka/
消费者的application.xml
server:
port: 80
spring:
application:
name: cloud-order-service
eureka:
client:
#表示是否将自己注册进Eurekaserver默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
3.在提供者启动类使用注解@EnableEurekaClient
注册中心启动类使用@EnableEurekaServer
下面是一些测试Demo
首先是提供者:
Controller层
@Value("${server.port}")
private String serverPort;//添加serverPort
@GetMapping(value = "/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id)
{
Payment payment = paymentService.getPaymentById(id);
if(payment != null)
{
return new CommonResult(200,"查询成功,serverPort:"+serverPort,payment);
}else{
return new CommonResult(444,"没有对应记录,查询ID: "+id,null);
}
}
服务层
@Service
public class PaymentServiceImpl implements PaymentService
{
@Resource
private PaymentDao1 paymentDao;
public Payment getPaymentById(Long id)
{
return paymentDao.getPaymentById(id);
}
}
Dao层
@Mapper
public interface PaymentDao1
{
Payment getPaymentById(@Param("id") Long id);
}
Mapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.peng.springcloud.dao.PaymentDao1">
<resultMap id="BaseResultMap" type="com.peng.springcloud.dto.Payment">
<id column="id" property="id" jdbcType="BIGINT"/>
<id column="serial" property="serial" jdbcType="VARCHAR"/>
</resultMap>
<select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
select * from payment where id=#{id};
</select>
</mapper>
其次是消费者
Controller
@RestController
@Slf4j
@RequestMapping("/consumer/payment")
public class OrderController {
final static String PAYMENT_URL="http://CLOUD-PAYMENT-SERVICE";
@Resource
private RestTemplate restTemplate;
@GetMapping("/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id, CommonResult.class);
}
}
启动类
@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class, args);
}
}
配置类
集群需要在消费者的配置上加上@LoadBalanced注解,在controller中吧请求路径改为服务的名字
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
运行结果:
请求http://eureka7001.com:7001/这里的eureka7001.com路径需要在host中配置
请求注册中心http://eureka7003.com:7003/ (这里没有在上面写,配置什么的和第一个注册中心的配置一样)