编写 Eureka 服务提供者 (简易版)

Step 4-1:使用 Spring Initializr 创建 Spring Boot 项目

选择如下配置

         

注:不熟悉如何搭建Spring Boot 项目,请点击

 

Step 4-2:创建配置文件 application.yml,内容如下

                                                                   

spring:
  application:
    name: provider
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/

 

Step 4-3:编写启动类 @EnableEurekaClient

    注意:自动引入的 Eureka 是 netflix.eureka

package com.example.provider;

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

import java.util.Scanner;

@SpringBootApplication
@EnableEurekaClient
public class ProviderApplication {

    public static void main(String[] args) {

        new SpringApplicationBuilder(ProviderApplication.class).run(args);
    }

}

  Step 4-4:编写Contorller @RestController

package com.example.provider;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FirstController {

	@RequestMapping(value = "/person/{personId}", method = RequestMethod.GET, 
			produces = MediaType.APPLICATION_JSON_VALUE)
	public Person findPerson(@PathVariable("personId") Integer personId, HttpServletRequest request) {
		Person person = new Person(personId, "Crazyit", 30);
		// 为了查看结果,将请求的URL设置到Person实例中
		person.setMessage(request.getRequestURL().toString());
		return person;
	}
}

启动后效果:eureka 界面

                                       

posted @ 2019-02-22 00:41  随风落木  阅读(0)  评论(0编辑  收藏  举报  来源