利用SpringCloud搭建微服务1——注册中心Eureka组件的使用
2018-12-31 19:35
791 查看
搭建Eureka服务注册中心
1 maven quickstart
2 pom依赖(springboot的版本必须和springcloud版本兼容)
3 配置application.properties (实现注册中心的内容)
server.port=8000 eureka.instance.hostname=localhost //当这个域名能够访问到注册中心所在的服务器时,可以被注册中心监听到访问端口.进入注册中心 eureka.client.registerWithEureka=false //启动的每个当前服务框架的工程都可以在注册中心注册服务,false表示不注册 eureka.client.fetchRegistry=fasle //不去检测服务 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka //访问的注册中心地址,一旦定义这个地址,必须和端口和域名对应,其他的工程需要通过这个属性来注册服务 4 启动类测试 测试访问注册中心 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20181231192627847.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLm 4000 NzZG4ubmV0L3dlaXhpbl80MjM5NDA1Mg==,size_16,color_FFFFFF,t_70) 5注册服务(eureka-server) 编写一个具有特定功能,能够提供数据返回的系统
代码如下:
//注册中心
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaCenterStarter { public static void main(String[] args) { SpringApplication.run(EurekaCenterStarter.class, args); } }
//添加服务1 (添加更多服务类似写法)
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class EurekaServer01Starter { public static void main(String[] args) { SpringApplication.run(EurekaServer01Starter.class,args); } }
服务1的配置文件application.properties写法:
server.port=9000 server.context-path=/ eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/ spring.application.name=service-hi
maven配置文件pom.xml写法:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.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> <spring-cloud.version>Edgware.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <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>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
客户端访问控制层,根据业务逻辑来写:
示例:
import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @Configuration public class HiController { //*每个服务提供者提供一个访问方法 //返回数据包含当前工程的一些属性,例如端口 @Value("${server.port}") private String port; @RequestMapping("/hi") public String sayHi(String name){ return "hi "+name+",i am from "+port; } }
相关文章推荐
- SpringCloud组件:搭建Eureka服务注册中心
- 【Spring Cloud】Eureka服务注册中心搭建
- 【Spring Cloud】Eureka服务注册中心搭建
- 【Spring Cloud】Eureka服务注册中心搭建
- 【Spring Cloud】Eureka服务注册中心搭建
- 【Spring Cloud】Eureka服务注册中心搭建
- 利用SpringCloud搭建微服务2——负载均衡Ribbon组件的使用
- 搭建SpringCloud-Eureka 注册中心以及服务提供与调用
- 【Spring Cloud】Eureka服务注册中心搭建
- 【Spring Cloud】Eureka服务注册中心搭建
- 【Spring Cloud】Eureka服务注册中心搭建
- 【Spring Cloud】Eureka服务注册中心搭建
- 【Spring Cloud】Eureka服务注册中心搭建
- 【Spring Cloud】Eureka服务注册中心搭建
- spring cloud 服务注册中心eureka高可用集群搭建
- 【Spring Cloud】Eureka服务注册中心搭建
- SpringCloud组件:将微服务提供者注册到Eureka服务中心
- 利用SpringCloud搭建微服务4——服务调用组件Fegin的使用,引入API网关治理组件Zuul
- 【Spring Cloud】Eureka服务注册中心搭建
- 【Spring Cloud】Eureka服务注册中心搭建