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. 然后启动程序
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章