Spring Cloud(一):发现和注册服务(eureka)

最近的项目需要将原来的项目重构为Spring Boot项目,正好也能利用一些Spring Cloud的工具,就正好学习学习_(:з」∠*)_。
首先需要新建2个Spring Boot项目,一个作为服务注册中心,一个作为服务生产者/提供者。
如下
在这里插入图片描述
PS.我这里用的是Spring Boot 2.X版本

一.服务注册中心
新建Spring Boot项目SpringCloudServiceCenter
(1)pom.xml



   4.0.0

   com.service.center
   SpringCloundServiceCenter
   0.0.1-SNAPSHOT
   jar

   SpringCloundServiceCenter
   com.service.center

   
   	org.springframework.boot
   	spring-boot-starter-parent
   	2.1.0.RELEASE
   	 
   

   
   	UTF-8
   	UTF-8
   	1.8
   	Greenwich.M1
   

   
   	
   		org.springframework.cloud
   		spring-cloud-starter-netflix-eureka-server
   	

   	
   		org.springframework.boot
   		spring-boot-starter-test
   		test
   	
   

   
   	
   		
   			org.springframework.cloud
   			spring-cloud-dependencies
   			${spring-cloud.version}
   			pom
   			import
   		
   	
   

   
   	
   		
   			org.springframework.boot
   			spring-boot-maven-plugin
   		
   	
   

   
   	
   		spring-milestones
   		Spring Milestones
   		https://repo.spring.io/milestone
   		
   			false
   		
   	
   

(2)application.properties配置

#服务端口
server.port=8761
#服务名称
eureka.instance.hostname=serviceCenter
#禁止本身注册
eureka.client.register-with-eureka=false
#禁止本身注册
eureka.client.fetch-registry=false
#服务中心地址
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

(3)SpringCloundServiceCenterApplication.java启动文件中添加@EnableEurekaServer

package com.service.center;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class SpringCloundServiceCenterApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloundServiceCenterApplication.class, args);
	}
}

来表明该服务为服务发现和注册中心
整体结构
Spring Cloud(一):发现和注册服务(eureka)_第1张图片
(4)配置完成后启动,然后访问http://localhost:8761/ 就能进入eureka server监控界面,可以看到红框中的信息,现在还没有服务注册。
Spring Cloud(一):发现和注册服务(eureka)_第2张图片


二.服务生产者
新建spring boot项目SpringCloudServiceI
(1)pom.xml配置



   4.0.0

   com.my.serviceI
   SpringCloundServiceI
   0.0.1-SNAPSHOT
   jar

   SpringCloundServiceI
   com.my.serviceI

   
   	org.springframework.boot
   	spring-boot-starter-parent
   	2.1.0.RELEASE
   	 
   

   
   	UTF-8
   	UTF-8
   	1.8
   	Greenwich.M1
   

   
   	
   		org.springframework.boot
   		spring-boot-starter-web
   	
   	
   		org.springframework.cloud
   		spring-cloud-starter-netflix-eureka-client
   	
   	
   		org.springframework.boot
   		spring-boot-starter-test
   		test
   	
   

   
   	
   		
   			org.springframework.cloud
   			spring-cloud-dependencies
   			${spring-cloud.version}
   			pom
   			import
   		
   	
   

   
   	
   		
   			org.springframework.boot
   			spring-boot-maven-plugin
   		
   	
   

   
   	
   		spring-milestones
   		Spring Milestones
   		https://repo.spring.io/milestone
   		
   			false
   		
   	
   

(2)application.properties配置

server.servlet.context-path=/myServiceI 
server.port=8762
spring.application.name=myServiceI  #服务名,在注册时所用
eureka.client.service-url.defautZone=http://serviceCenter:8761/eureka/

(3)在启动文件SpringCloundServiceIApplication.java中添加@EnableEurekaClient

package com.my.serviceI;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class SpringCloundServiceIApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloundServiceIApplication.class, args);
	}
}

(4)新建一个controller文件,提供服务
例ServiceApiController.java

package com.my.serviceI.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value="/Api")
public class ServiceApiController {
   @ResponseBody
   @RequestMapping(value="/getInfo")
   public String getInfo() {
   	return "123+";
   }
}

整体结构如下
Spring Cloud(一):发现和注册服务(eureka)_第3张图片
可以启动测试下是否能访问接口服务,因为spring boot会内置一个tomcat容器,且在启动时会省略掉项目名,所以我这里在application.properties中配置server.servlet.context-path=/myServiceI 来把项目名加上了,所以现在接口的访问路径为http://localhost:8762/myServiceI/Api/getInfo
Spring Cloud(一):发现和注册服务(eureka)_第4张图片
然后刷新下eureka server监控界面,就能看到刚才的服务了,能从信息中得知服务的名字和端口号,上面红条是另一个服务。
Spring Cloud(一):发现和注册服务(eureka)_第5张图片

你可能感兴趣的:(Spring,Cloud,Spring,Boot)