1.概述
流程还是和建立注册中心一样,区别主要在配置文件和注释
2.具体配置信息
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--父工程依赖-->
<parent>
<groupId>com.example</groupId>
<artifactId>parents</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>register</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>register</name>
<description>Demo project for Spring Boot</description>
<dependencies>
</dependencies>
</project>
2.yml
server:
port: 8764
spring:
application:
name: server-hi
eureka:
client:
serviceUrl:
defaultZone: http://peer1:8771/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
3.注解
package com.example.register;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
//服务提供者
@EnableEurekaClient
@SpringBootApplication
@RestController
public class RegisterApplication {
public static void main(String[] args) {
SpringApplication.run(RegisterApplication.class, args);
}
/** 获取端口号 */
@Value("${server.port}")
String port;
/**
* 定义一个简单接口
* @param name
* @return
*/
@GetMapping("/hi/{name}")
public String home(@PathVariable String name){
return "hi ,I am" + name + " ,from port :" + port;
}
}
3.运行项目
可以看到 server-hi的 服务已经注册好了
直接访问服务提供者的地址,返回信息,说明服务可用