系统整理springCloud系列(三)服务注册
一,支付服务注册到eureka注册中心
添加配置文件
server:
port: 8001
spring:
application:
name: cloud-payment-service #CLOUD-PAYMENT-SERVICE
zipkin:
base-url: http://localhost:9411
sleuth:
sampler:
probability: 1 #采样率值介于0到1之间,1则表示全部采集
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka #,http://eureka7002.com:7002/eureka #集群版
instance:
instance-id: payment8001
prefer-ip-address: true
lease-renewal-interval-in-seconds: 1
lease-expiration-duration-in-seconds: 2
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.atguigu.springcloud.entities # 所有Entity别名类所在包
启动类添加
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}
服务层,控制层,此处不再列出
服务启动成功后会在注册中心添加服务
客户端可获取注册中心服务
方法
@Resource
private DiscoveryClient discoveryClient;
@GetMapping(value = "/payment/discovery")
public Object discovery(){
List<String> services = discoveryClient.getServices();
for (String element: services) {
log.info("************element:"+element);
}
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance: instances) {
log.info(instance.getInstanceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
}
return this.discoveryClient;
}
执行此方法,2020-07-08 10:12:27.680 INFO [cloud-payment-service,72a043381e957dec,72a043381e957dec,true] 12676 --- [nio-8001-exec-4] : ************element:cloud-payment-service
2020-07-08 10:12:27.681 INFO [cloud-payment-service,72a043381e957dec,72a043381e957dec,true] 12676 --- [nio-8001-exec-4] payment8001 192.168.25.1 8001 http://192.168.25.1:8001