eureka注册中心,服务提供者,服务消费者体系搭建

一、注册中心搭建

  1. 引入依赖
 		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
  1. application.yml
server:
  port: 3000
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defalutZone: "http://${eureka.instance.hostname}:{server.port}/eureka/"
spring:
  application:
    name: eureka
  1. springboot启动类
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class,args);
    }
}

@EnableEurekaServer注解,启动

二、服务提供者

  1. 引入依赖
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
  1. application.yml
server:
  port: 3001

eureka:
  instance:
    preferIpAddress: true
  client:
    serviceUrl:
      defaultZone: http://localhost:3000/eureka  ## 注册到 eureka
spring:
  application:
    name: single-provider  ## 应用程序名称,后面会在消费者中用到

  1. 控制器
@Slf4j
@RestController
public class ProviderController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/hello")
    public String hello(){
        List<String> services = discoveryClient.getServices();
        for(String s : services){
            log.info(s);
        }
        return "hello spring cloud!";
    }

    @RequestMapping(value = "/nice")
    public String nice(){
        List<String> services = discoveryClient.getServices();
        for(String s : services){
            log.info("gogogo" + s);
        }
        return "nice to meet you!";
    }

}
  1. 启动类 @EnableEurekaClient
@EnableEurekaClient
@SpringBootApplication
public class SingleProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(SingleProviderApplication.class,args);
    }
}

三、服务消费者

  1. 引入依赖
		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </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>
  1. application.yml
server:
  port: 3002
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:3000/eureka  ## 注册到 eureka
  instance:
    preferIpAddress: true
spring:
  application:
    name: single-customer
  1. 启动类
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class singleCustomerApplication {
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(singleCustomerApplication.class,args);
    }
}
  1. 服务消费
public class CustomerController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private HelloService helloService;
    private static final String applicationName = "single-provider";
//    feign实现
    @RequestMapping(value = "feignRequest")
    public Object feignRequest(){
        return helloService.nice();
    }
//    restTemplate实现
    @RequestMapping(value = "commonRequest")
    public Object commonRequest(){
        String url = "http://"+ applicationName +"/hello";
        String s = restTemplate.getForObject(url,String.class);
        return s;
    }
}
  1. feign 接口
@FeignClient("single-provider")
public interface HelloService {

    @RequestMapping("/nice")
    String nice();
}

注册中心显示:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值