[SpringCloud]2. service-hi

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kaidi224/article/details/80690232

1. Cloud Discovery ☑️Eureka Server

2. ServiceHiApplication

通过注解@EnableEurekaClient 表明自己是一个EurekaClient

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class ServiceHiApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceHiApplication.class, args);
    }
}

3. application.yml

注明自己的服务注册中心的地址

spring.application.name 在以后的服务与服务之间相互调用一般都是根据这个name

server:
  port: 8762
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      default-zone: http://${eureka.instance.hostname}:8761/eureka/
spring:
  application:
    name: service-hi

4. Controller

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HiController {
    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    public String home(@RequestParam String name) {
        return "hi " + name + ",i am from port:" + port;
    }
}

5. 启动工程

ApplicationAMIsAvailability ZonesStatus
SERVICE-HIn/a (1)(1)UP (1) - deair:service-hi:8762

一个服务已经注册在服务中了, 服务名为SERVICE-HI ,端口为8762

访问http://localhost:8762/hi?name=

hi ,i am from port:8762
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页