欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springcloud入门_服务提供方 博客分类: java  

程序员文章站 2024-03-24 11:17:04
...
1.服务提供方,在注册中心发现该服务
<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>
	<groupId>com.chry</groupId>
	<artifactId>springcloud.helloworld.eureka.client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springcloud.helloworld.eureka.client</name>
	<description>Demo Spring Boot Client</description>
	
	<!-- 声明SpringBoot-->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.3.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<!-- 统一项目字符集UTF-8 -->
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!--spring cloud eureka server -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- spring boot test -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.RC1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
</project>

 

package springcloud.helloworld.eureka.client;

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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/** 启动 */
@SpringBootApplication
/** 服务提供方 */
@EnableEurekaClient
/** Controller注解 */
@RestController
/**
 * 
 * @类描述:服务提供方实例
 * @项目名称:springcloud.helloworld.eureka.client
 * @包名: springcloud.helloworld.eureka.client
 * @类名称:EurekaClientApplication
 * @创建人:wangxuegang
 * @创建时间:2018年12月19日上午10:44:58
 * @mail 15510235102@163.com
 * @version v1.0
 */
public class EurekaClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaClientApplication.class, args);
	}
	
	/** 服务提供方端口号 (在application.yml中取值)*/
	@Value("${server.port}")
	String port;
	
	/** 请求格式:localhost:port/ */
	@RequestMapping("/")
	public String home() {
		return "hello world from port " + port;
	}

}

 

#注册中心地址配置
eureka:
    client:
        serviceUrl:
            defaultZone: http://localhost:8761/eureka/
#服务提供方端口号            
server:
    port: 8762
spring:
    application:
    	#服务提供方名称   
        name: service-helloworld

 

2.启动服务提供方,在注册中心查询application,访问服务方接口,打印服务方端口号

 

  • springcloud入门_服务提供方
            
    
    博客分类: java  
  • 大小: 56.3 KB
  • springcloud入门_服务提供方
            
    
    博客分类: java  
  • 大小: 11 KB