gengzibing 5 years ago
commit
63b2c9502d

+ 31 - 0
.gitignore

@@ -0,0 +1,31 @@
+HELP.md
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**
+!**/src/test/**
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+
+### VS Code ###
+.vscode/

+ 78 - 0
pom.xml

@@ -0,0 +1,78 @@
+<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<groupId>com.njty</groupId>
+	<artifactId>eurekaclient</artifactId>
+	<version>0.0.1-SNAPSHOT</version>
+	<packaging>jar</packaging>
+
+	<parent>
+		<groupId>org.springframework.boot</groupId>
+		<artifactId>spring-boot-starter-parent</artifactId>
+		<version>1.5.3.RELEASE</version>
+		<relativePath/> <!-- lookup parent from repository -->
+	</parent>
+
+	<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>
+		<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>
+
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-test</artifactId>
+			<scope>test</scope>
+		</dependency>
+
+		<!--配置中心-->
+		<dependency>
+			<groupId>org.springframework.cloud</groupId>
+			<artifactId>spring-cloud-starter-config</artifactId>
+		</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>

+ 22 - 0
readme

@@ -0,0 +1,22 @@
+微服务客户端
+1、添加依赖
+2、启动类添加注解
+3、yml配置注册中心地址
+
+配置中心客户端
+1、添加依赖
+2、添加bootstrap.yml文件指定要读取的配置信息
+http://localhost:8888/application/profile
+application:服务名,profile:环境,label:版本分支
+/{application}/{profile}[/{label}]
+/{application}-{profile}.yml
+/{label}/{application}-{profile}.yml
+/{application}-{profile}.properties
+/{label}/{application}-{profile}.properties
+
+读取顺序(可能不对):
+bootstrap.yml
+application.yml
+配置中心的application.yml
+配置中心的applicationname.yml
+后面覆盖前面

+ 19 - 0
src/main/java/com/njty/eurekaclient/EurekaclientApplication.java

@@ -0,0 +1,19 @@
+package com.njty.eurekaclient;
+
+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;
+
+@EnableEurekaClient
+@SpringBootApplication
+public class EurekaclientApplication {
+
+	@Value("${helloConfig}")
+	private String helloConfig;
+
+	public static void main(String[] args) {
+		SpringApplication.run(EurekaclientApplication.class, args);
+	}
+
+}

+ 20 - 0
src/main/java/com/njty/eurekaclient/controller/HelloController.java

@@ -0,0 +1,20 @@
+package com.njty.eurekaclient.controller;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("/hello")
+public class HelloController {
+
+    @Value("${helloConfig}")
+    private String helloConfig;
+
+    @GetMapping("/showHelloConfig")
+    public String showHelloConfig(){
+        return helloConfig;
+    }
+}

+ 11 - 0
src/main/resources/application.yml

@@ -0,0 +1,11 @@
+eureka:
+    client:
+        serviceUrl:
+            defaultZone: http://localhost:8761/eureka
+server:
+    port: 8762
+spring:
+    application:
+        name: eurekaclient
+
+helloConfig: this is local

+ 10 - 0
src/main/resources/bootstrap.yml

@@ -0,0 +1,10 @@
+spring:
+  application:
+    name: eurekaclient
+  cloud:
+    config:
+      label: dev #版本分支
+      profile: demo #环境后缀
+      uri: http://localhost:8888/
+server:
+  port: 8881

+ 16 - 0
src/test/java/com/njty/eurekaclient/EurekaclientApplicationTests.java

@@ -0,0 +1,16 @@
+package com.njty.eurekaclient;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class EurekaclientApplicationTests {
+
+	@Test
+	public void contextLoads() {
+	}
+
+}