eureka配置_springcloud项目搭建第四节:eureka的服务注册者和消费者的配置

基于上一节的项目,修改eureka-server项目的application.yml配置

server:  port: 8901spring:  application:    name: eureka-servereureka:  client:    service-url:      defaultZone: http://127.0.0.1:8901/eureka    # 不注册自己    register-with-eureka: false  server:    # 服务失效剔除时间间隔,默认60秒    eviction-interval-timer-in-ms: 30000    # 关闭自我保护模式(默认是打开的)    enable-self-preservation: false

然后修改business-service项目的application.yml配置,主要加的是instance的配置

server:  port: 8902eureka:  client:    service-url:      defaultZone: http://127.0.0.1:8901/eurekaspring:  application:    name: business-service  datasource:    driver-class-name: com.mysql.jdbc.Driver    url: jdbc:mysql://127.0.0.1:33067/tas?characterEncoding=UTF-8&useUnicode=true&useSSL=false&allowMultiQueries=true&serverTimezone=GMT%2B8    username: root    password: 123456mybatis:  type-aliases-package: com.yd.entity.*instance:  # 开启使用ip地址  prefer-ip-address: true  # 设置指定ip地址  ip-address: 127.0.0.1  # 续约间隔默认30秒  lease-renewal-interval-in-seconds: 5  # 服务失效时间默认90秒  lease-expiration-duration-in-seconds: 20

最后是修改business-client项目的配置

server:  port: 8903spring:  application:    name: business-clienteureka:  client:    service-url:      defaultZone: http://127.0.0.1:8901/eureka    # 获取服务地址列表间隔时间,默认30秒    registry-fetch-interval-seconds: 5

然后在启动类上添加@EnableDiscoveryClient注解用于DiscoveryClient使用来获取中心服务business-service服务的实例

package com.yd;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;/** * @Author: zengyz * @Date: 2020/11/5 15:19 */@SpringBootApplication/*DiscoveryClient使用 */@EnableDiscoveryClientpublic class BusinessClientApplication {    public static void main(String[] args) {        SpringApplication.run(BusinessClientApplication.class, args);    }    @Bean    public RestTemplate restTemplate(){        return new RestTemplate();    }}

最后在控制类方法中添加DiscoveryClient注入,然后获取business-service服务来实例化,这样就能获取到地址和端口

package com.yd;import com.netflix.appinfo.InstanceInfo;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.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;import java.util.List;/** * @Author: zengyz * @Date: 2020/11/4 14:44 */@RestController@RequestMapping("/user")public class UserController {    @Autowired    private RestTemplate restTemplate;    @Autowired    DiscoveryClient discoveryClient;    @GetMapping("/clientTest")    public String clientTest(){//        方法一:指定地址        String url = "http://windows10.microdone.cn:8902/user/test";        List serviceInstanceList= discoveryClient.getInstances("business-service");        ServiceInstance serviceInstance=serviceInstanceList.get(0);        url= "http://"+serviceInstance.getHost() +":"+serviceInstance.getPort()+"/user/test";        String str= restTemplate.getForObject(url, String.class);        return str;    }}

启动eureka-server、business-service、business-client三个项目就可以看到注册中心注册的服务,然后停调business-service项目,等待指定的时间后就会出项目中移除服务了,就防止因为服务挂了造成僵尸服务存在了

497be81f6c4c47beadd178b8e13f90f0.png

项目源码:https://pan.baidu.com/s/1ez86ZZnJO3YIGNZpuZ6sZA

提前密码:nseh

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值