SpringCloud(3)-向服務註冊中心註冊服務提供者

服務的提供者

其實服務提供者就是eureka的客戶端,我們可以引入eureka的客戶端,然後把它當作我們的服務提供者,我們通過接口的方式,然後消費者通過restful+ribbon或者通過Fegion這兩種方式來訪問服務。而服務提供者就相當於我們發生一個請求,然後以json的方式返回結果。

搭建服務的提供者

  1. 首先我們創建一個項目 eureka-provider,然後在pom.xml文件中引入如下依賴

    <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>
    
  2. 在application.properties中配置如下

    server.port=84
    spring.application.name=provider
    eureka.client.service-url.defaultZone=http://master:81/eureka/
    
  3. SpringBoot啓動類

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @EnableEurekaClient
    @SpringBootApplication
    public class EurekaHelloProviderApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaHelloProviderApplication.class, args);
        }
    }
    
  4. 測試的Controller

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    
    @RestController
    public class HelloController {
        @RequestMapping(method= RequestMethod.GET)
        public String sayHi(String name){
            return "hi," +name;
        }
    }
    
  5. 然後啓動程序
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章