Eureka是SpringCloud Netflix提供的服务发现组件,本文介绍Eureka Server及Client的搭建步骤。
为了方便起见,我们从Spring Initializr创建一个Eureka Server :
将得到的压缩包解压后导入到idea中。
在application.properties中加入以下配置:
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF
给程序入口类中加上@EnableEurekaServer,标记这是一个Euraka Server应用,如下:
package com.example.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaserverApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaserverApplication.class, args);
}
}
完成以上步骤,不出意外的化就可以把程序跑起来了
打开浏览器,输入地址http://www.lhsxpumps.com/_localhost:8761
,出现以下界面则表示Eureka Server创建成功:
同样,我们从Spring Initializr创建一个Eureka Client :
创建bootstrap.properties配置文件,在文件中加入:
spring.application.name=a-bootiful-client
将Spring initializr默认生成的EurekaclientApplication.java
替换成以下内容:
package com.example.eurekaclient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaclientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaclientApplication.class, args);
}
}
@RestController
class ServiceInstanceRestController {
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("/service-instances/{applicationName}")
public List<ServiceInstance> serviceInstancesByApplicationName(
@PathVariable String applicationName) {
return this.discoveryClient.getInstances(applicationName);
}
}
完成上述步骤便可以运行该应用
打开浏览器,输入地址http://www.lhsxpumps.com/_localhost:8080/service-instances/a-bootiful-client
,可以看到一下界面:
https://spring.io/guides/gs/service-registration-and-discovery/
本文地址:https://blog.csdn.net/u012124304/article/details/107168153
如对本文有疑问, 点击进行留言回复!!
index.jsp界面提供用户输入用户名和密码文本框;在result.jsp页面输出用户名和密码
Element-Ui组件 单选框(Radio) 修改点击激活时的文本颜色,填充色和边框色
Cypress UI 自动化(1)- windows 7 环境npm安装Cypress
前后端分离Vue + ElementUI项目打包部署到云服务器
ESLine配置使用(react,babel,webpack)
网友评论