一、微服务程序编写

1、在已写好的微服务程序中添加pom依赖:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

  例子程序的完整pom文件如下,只是一个简单的mvc程序,添加了SpringBoot的Actuactor:

<?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.example</groupId>
<artifactId>microservice-consumer-movie</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>ServiceConsumer</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/>
</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.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</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> </project>

2、在Spring Cloud Edgware版本以前,要将微服务注册到Eureka Server或者其它微服务发现组件上,必须在启动类上添加@EnableEurekaClient或者@EnableDiscoveryClient,之后的版本不需要了。

3、配置application.yml或application.properties:

server:
port: 8000
spring:
main:
allow-bean-definition-overriding: true
jpa:
generate-ddl: false
show-sql: true
hibernate:
ddl-auto: none
datasource:
platform: h2
schema: classpath:db/schema.sql
data: classpath:db/data.sql
logging:
level:
root: INFO
org.hibernate: INFO
org.hibernate.type.descriptor.sql.BasicBinder: TRACE
org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
application:
name: microservice-provider-user
eureka:
client:
service-url:
defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/ #设置与Eureka Server的交互地址,如果是Eureka Server集群,可以通过逗号分隔配置多个地址
instance:
prefer-ip-address: true

  例子程序的github地址是https://github.com/zjianliu/ServiceProvider

二、将微服务注册到Eureka Server

  运行微服务,访问http://localhost:8761,可以发现,微服务已经被注册了

最新文章

  1. CSS3 border-radius边框圆角
  2. sk_buff封装和解封装网络数据包的过程详解
  3. Redis GetTypedClient
  4. Parallel.Foreach的全部知识要点【转】
  5. vi &amp; vim 基本指令(持续更新ing)
  6. Monkey源代码分析之执行流程
  7. position:fixed失效情况
  8. 解决Tomcat version 7.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 and 6 Web modules
  9. Android为TV端助力 EventBus.getDefault()开源框架
  10. python 爬虫(一) requests+BeautifulSoup 爬取简单网页代码示例
  11. 深入理解 Node.js 中 EventEmitter源码分析(3.0.0版本)
  12. 运维监控-使用Zabbix Server 创建 Actions
  13. Leetcode 461.汉明距离 By Python
  14. Vue系列之 =&gt; 动画
  15. LG4197 Peaks
  16. 【javascript】浏览器用户代理检测脚本实现
  17. 九度oj-1533 最长上升子序列 (LIS)
  18. 〖Android〗巧用/system/etc/mkshrc文件,把busybox常用命令映射(链接)出来;
  19. 二、Nuxt初始化项目
  20. CF#256(Div.2) A. Rewards

热门文章

  1. Java 消息对列
  2. 2018牛客多校第二场a题
  3. Java进阶知识点:不要只会写synchronized - JDK十大并发编程组件总结
  4. opencv-学习笔记(1)常用函数和方法。
  5. ThinkPHP - 5 - 学习笔记(2015.4.15)
  6. 一个简单的Spring的AOP例子
  7. [leetcode-784-Letter Case Permutation]
  8. 接口_GET请求_基于python
  9. Python中函数的参数-arguments
  10. ACM 第七天