二 springcloud 服务生产注册

本文档介绍了如何在SpringCloud环境中创建服务生产者项目。首先,在根项目下创建名为springcloud-demo-a的服务生产项目,配置pom.xml并提供启动类DemoAServiceApplication。接着,详细展示了服务DemoAController的实例化过程。
摘要由CSDN通过智能技术生成

 1 在根项目下创建服务生产项目springcloud-demo-a

pom.xml如下:

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.mxx.springcloud</groupId>
		<artifactId>springcloud-root</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>springcloud-demo-a</artifactId>
	<name>springcloud-demo-a</name>
	<description>springcloud-demo-a</description>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>
  			spring-cloud-starter-netflix-eureka-client
  		</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- spring-admin-actuator相关 -->
		 <dependency>
	        <groupId>org.springframework.boot</groupId>  
	        <artifactId>spring-boot-starter-actuator</artifactId>  
	    </dependency>
	</dependencies>
</project>

启动类:DemoAServiceApplication

package com.mxx.springcloud.eureka.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@RestController
@RequestMapping(value = "/actuator")
public class DemoAServiceApplication {

	public static void main(String[] args) {
		
		SpringApplication.run(DemoAServiceApplication.class, args);
	}
	@RequestMapping(value = "/info")
	public String info() {
		return "ok";
	}
}

配置文件application.yml:

#端口
server:
  port: 8001
  
#应用名称
spring:
  application:
    name: euraka-client-a
    
#eureka
eureka:
  instance:
    instance-id: euraka-client-a-local:8001
    prefer-ip-address: true
    
  client:
    service-url:
      defaultZone: http://192.168.125.66:8888/eureka/
      
#管理端点的配置  
management:  
  endpoints:  
    web:  
      exposure:  
        include: "*"  
  endpoint:  
    health:  
      show-details: ALWAYS

2 服务DemoAController实例:

package com.mxx.springcloud.eureka.client;

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

@RestController
@RequestMapping(value="/a")
public class DemoAController {

	@Value("${server.port}")
	private String port;
	
	@RequestMapping(value="/hello")
	public String hello(){
	
		return "hello"+this.port+","+System.currentTimeMillis();
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值