SpringCloud微服务实战之服务治理Eureka(单节点) ╰半夏微凉° 2022-06-02 20:27 158阅读 0赞 > Euraka是NetFlix微服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的服务治理和服务发现功能。 ## 示例: ## **一、Eureka搭建服务注册** 1、创建maven项目SpringCloud-EurekaServer ![这里写图片描述][SouthEast] 2、修改pom.xml文件,pom中添加Euraka依赖jar包。 <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.spring</groupId> <artifactId>SpringCloud-EurekaServer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringCloud-EurekaServer</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <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> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.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> 3、创建application.propertises文件 server.port=8888 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/ * server.port:Eureka服务注册端口。 * eureka.client.register-with-eureka:设置为false,代表不向注册中心注册自己。 * eureka.client.fetch-registry:设置为false,代表不去检索服务。 通过[http://localhost:8888/][http_localhost_8888]访问注册中心: ![这里写图片描述][SouthEast 1] 由于该注册中心还没注册任何服务,所以Instances currently registered with Eureka栏为“No instances available”。 **二、在服务注册中心注册helloService服务** 1、创建Maven项目SpringCloud-Service ![这里写图片描述][SouthEast 2] 2、修改pom.xml,添加Euraka和SpringBoot依赖 <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.spring</groupId> <artifactId>SpringCloud-Service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringCloud-Service</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </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> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project> 3、新增application.propertises文件 server.port=9999 spring.application.name=helloService eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/ * server.port:helloService服务端口。 * spring.application.name:向服务注册中心注册的服务名。 * eureka.client.serviceUrl.defaultZone:eureka服务注册中心Url。 4、实现HelloServiceController package com.spring.springcloud.rest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloServiceController { @Autowired private DiscoveryClient client; @RequestMapping(value="/hello",method=RequestMethod.GET) public String greeting(){ ServiceInstance si = client.getLocalServiceInstance(); return "hello"; } } 5、最后向Application主类中添加注解@EnableDiscoveryClient,用于激活Eureka中的EnableDiscoveryClient实现。 package com.spring.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 运行HelloService注册服务,启动成功之后,访问注册中心:[http://localhost:8888/][http_localhost_8888] ![这里写图片描述][SouthEast 3] 此时我们看见helloService已经成功注册。 [SouthEast]: /images/20220603/d4daeb66d4b34e0b80991f253a0533d0.png [http_localhost_8888]: http://localhost:8888/ [SouthEast 1]: /images/20220603/ad60256f07e44073aafbc6554809fe44.png [SouthEast 2]: /images/20220603/e16cdca268314866909ad415568cc8ae.png [SouthEast 3]: /images/20220603/973f0f8048c4461ebabd88546de1cc42.png
还没有评论,来说两句吧...