1.新建一个springboot的项目。
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.boce.dfs</groupId>
<artifactId>comm-eku</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>comm-eku</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.4.0.RELEASE</version>
</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-eureka-server</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.添加配置文件 application.yml
# server (eureka 默认端口为:8761)
server:
port: 9999
# spring
spring:
application:
name: comm-eku
# eureka
eureka:
client:
# 是否注册到eureka
register-with-eureka: true
# 是否从eureka获取注册信息
fetch-registry: false
availability-zones:
honghu: honghuZone
#Eureka Server注册服务的地址
service-url:
honghuZone: http://honghu:123456@localhost:8761/eureka/
defaultZone: http://honghu:123456@localhost:8761/eureka/
instance:
prefer-ip-address: true
hostname: localhost
metadataMap:
zone: honghuZone
user: ${security.user.name}
password: {security.user.password}
# 指定环境
environment: dev
#指定数据中心
datacenter: honghu
# 关闭自我保护模式
server:
enable-self-preservation: false
#设置清理无效节点的时间间隔,默认60000,即是60s
eviction-interval-timer-in-ms: 60000
# 服务认证
security:
basic:
enabled: true
user:
name: honghu
password: 123456
management:
security:
enabled: false
4.给主类添加注解:
package com.boce.dfs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
@ComponentScan(value = "com.boce")
@EnableEurekaServer
@SpringBootApplication
public class CommEkuApplication {
@RequestMapping("/hello")
private String greeting() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(CommEkuApplication.class, args);
}
}
package com.boce;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
/**
* Created by gjp on 2017/11/7.
*/
@Component
@Configuration
public class ConfigBase {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
package com.boce.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
/**
* Created by gjp on 2017/11/7.
*/
@Controller
public class DemoController {
@Resource
private RestTemplate restTemplate;
@ResponseBody
@RequestMapping("/demo/test")
public String test(){
System.out.println("test.................");
return "test";
}
}
[img]http://dl2.iteye.com/upload/attachment/0127/7212/9214130e-fa7c-32aa-8b7e-7f42782595ac.png[/img]
日志信息:
2017-11-07 17:07:26.183 INFO 18960 --- [ Thread-17] o.s.c.n.e.server.EurekaServerBootstrap : isAws returned false
2017-11-07 17:07:26.183 INFO 18960 --- [ Thread-17] o.s.c.n.e.server.EurekaServerBootstrap : Initialized server context
2017-11-07 17:07:26.232 INFO 18960 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9999 (http)
2017-11-07 17:07:26.233 INFO 18960 --- [ restartedMain] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 9999
2017-11-07 17:07:26.239 INFO 18960 --- [ restartedMain] com.boce.dfs.CommEkuApplication : Started CommEkuApplication in 9.861 seconds (JVM running for 10.571)
2017-11-07 17:07:26.811 INFO 18960 --- [nio-9999-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-11-07 17:07:26.811 INFO 18960 --- [nio-9999-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-11-07 17:07:26.836 INFO 18960 --- [nio-9999-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 25 ms
2017-11-07 17:07:26.956 INFO 18960 --- [nio-9999-exec-1] c.n.e.registry.AbstractInstanceRegistry : Registered instance COMM-EKU/gjp-pc:comm-eku:9999 with status UP (replication=false)
2017-11-07 17:07:26.978 INFO 18960 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_COMM-EKU/gjp-pc:comm-eku:9999 - registration status: 204
2017-11-07 17:07:27.620 INFO 18960 --- [nio-9999-exec-2] c.n.e.registry.AbstractInstanceRegistry : Registered instance COMM-EKU/gjp-pc:comm-eku:9999 with status UP (replication=true)
2017-11-07 17:09:26.188 INFO 18960 --- [ Thread-17] c.n.e.r.PeerAwareInstanceRegistryImpl : Got 1 instances from neighboring DS node
2017-11-07 17:09:26.188 INFO 18960 --- [ Thread-17] c.n.e.r.PeerAwareInstanceRegistryImpl : Renew threshold is: 1
2017-11-07 17:09:26.188 INFO 18960 --- [ Thread-17] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP
2017-11-07 17:09:26.198 INFO 18960 --- [ Thread-17] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.boce.dfs</groupId>
<artifactId>comm-eku</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>comm-eku</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.4.0.RELEASE</version>
</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-eureka-server</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.添加配置文件 application.yml
# server (eureka 默认端口为:8761)
server:
port: 9999
# spring
spring:
application:
name: comm-eku
# eureka
eureka:
client:
# 是否注册到eureka
register-with-eureka: true
# 是否从eureka获取注册信息
fetch-registry: false
availability-zones:
honghu: honghuZone
#Eureka Server注册服务的地址
service-url:
honghuZone: http://honghu:123456@localhost:8761/eureka/
defaultZone: http://honghu:123456@localhost:8761/eureka/
instance:
prefer-ip-address: true
hostname: localhost
metadataMap:
zone: honghuZone
user: ${security.user.name}
password: {security.user.password}
# 指定环境
environment: dev
#指定数据中心
datacenter: honghu
# 关闭自我保护模式
server:
enable-self-preservation: false
#设置清理无效节点的时间间隔,默认60000,即是60s
eviction-interval-timer-in-ms: 60000
# 服务认证
security:
basic:
enabled: true
user:
name: honghu
password: 123456
management:
security:
enabled: false
4.给主类添加注解:
package com.boce.dfs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
@ComponentScan(value = "com.boce")
@EnableEurekaServer
@SpringBootApplication
public class CommEkuApplication {
@RequestMapping("/hello")
private String greeting() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(CommEkuApplication.class, args);
}
}
package com.boce;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
/**
* Created by gjp on 2017/11/7.
*/
@Component
@Configuration
public class ConfigBase {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
package com.boce.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
/**
* Created by gjp on 2017/11/7.
*/
@Controller
public class DemoController {
@Resource
private RestTemplate restTemplate;
@ResponseBody
@RequestMapping("/demo/test")
public String test(){
System.out.println("test.................");
return "test";
}
}
[img]http://dl2.iteye.com/upload/attachment/0127/7212/9214130e-fa7c-32aa-8b7e-7f42782595ac.png[/img]
日志信息:
2017-11-07 17:07:26.183 INFO 18960 --- [ Thread-17] o.s.c.n.e.server.EurekaServerBootstrap : isAws returned false
2017-11-07 17:07:26.183 INFO 18960 --- [ Thread-17] o.s.c.n.e.server.EurekaServerBootstrap : Initialized server context
2017-11-07 17:07:26.232 INFO 18960 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9999 (http)
2017-11-07 17:07:26.233 INFO 18960 --- [ restartedMain] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 9999
2017-11-07 17:07:26.239 INFO 18960 --- [ restartedMain] com.boce.dfs.CommEkuApplication : Started CommEkuApplication in 9.861 seconds (JVM running for 10.571)
2017-11-07 17:07:26.811 INFO 18960 --- [nio-9999-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-11-07 17:07:26.811 INFO 18960 --- [nio-9999-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-11-07 17:07:26.836 INFO 18960 --- [nio-9999-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 25 ms
2017-11-07 17:07:26.956 INFO 18960 --- [nio-9999-exec-1] c.n.e.registry.AbstractInstanceRegistry : Registered instance COMM-EKU/gjp-pc:comm-eku:9999 with status UP (replication=false)
2017-11-07 17:07:26.978 INFO 18960 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_COMM-EKU/gjp-pc:comm-eku:9999 - registration status: 204
2017-11-07 17:07:27.620 INFO 18960 --- [nio-9999-exec-2] c.n.e.registry.AbstractInstanceRegistry : Registered instance COMM-EKU/gjp-pc:comm-eku:9999 with status UP (replication=true)
2017-11-07 17:09:26.188 INFO 18960 --- [ Thread-17] c.n.e.r.PeerAwareInstanceRegistryImpl : Got 1 instances from neighboring DS node
2017-11-07 17:09:26.188 INFO 18960 --- [ Thread-17] c.n.e.r.PeerAwareInstanceRegistryImpl : Renew threshold is: 1
2017-11-07 17:09:26.188 INFO 18960 --- [ Thread-17] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP
2017-11-07 17:09:26.198 INFO 18960 --- [ Thread-17] e.s.EurekaServerInitializerConfiguration : Started Eureka Server