十、一文搞定spring cloud重要成员-- Eureka(注册中心)
一、服务端Server搭建
-
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.kinglead</groupId> <artifactId>maven-spring-cloud-eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>maven-spring-cloud-eureka-server</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
-
添加配置
application.yml
server: port: 8080 eureka: instance: hostname: localhost #标识正运行在哪台服务器上 client: #register-with-eureka、fetch-registry:是否与Eureka服务进行交互,即是否集群 register-with-eureka: false fetch-registry: false #设置与Eureka注册中心交互的地址,查询服务和注册服务用到 service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #单机 # defaultZone: http://eureka9001.kinglead.com:9001/eureka/,http://eureka9002.kinglead.com:9002/eureka/ # 集群
-
修改启动类
@SpringBootApplication @EnableEurekaServer //标识为Eureka服务端 public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
-
访问管理页面
二、注册服务(客户端Client搭建)
-
引入依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.kinglead</groupId> <artifactId>maven-spring-cloud-eureka-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>maven-spring-cloud-eureka-client</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
添加配置
application.yml
spring: application: name: user-service #服务名 server: port: 9200 eureka: client: service-url: defaultZone: http://localhost:9000/eureka/ #Eureka服务端地址。配置多个地址,使用逗号进行分割,它将会一个一个进行尝试注册
-
修改启动类
@SpringBootApplication @EnableEurekaClient //标记为Eureka客户端 public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } }
-
启动服务,看Eureka服务端是否注
三、消费服务
Spring cloud有两种服务调用方式:1、restTemplate+ribbon(cloud的负载均衡组件);2、feign(cloud的httpclient组件,默认集成ribbo)
方式一、restTemplate+ribbon
-
引入依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.kinglead</groupId> <artifactId>maven-spring-cloud-eureka-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>maven-spring-cloud-eureka-client</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
配置文件
application.yml
spring: application: name: service-rest #服务名 server: port: 8001 eureka: client: service-url: defaultZone: http://localhost:9000/eureka/ #Eureka服务端地址。配置多个地址,使用逗号进行分割,它将会一个一个进行尝试注册
-
修改启动类
@SpringBootApplication @EnableEurekaClient public class ConsumerRibboApplication { public static void main(String[] args) { SpringApplication.run(ConsumerRibboApplication.class, args); } //向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。 @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
-
创建controller
@RestController public class HelloController { @Resource HelloService helloService; @RequestMapping("/hello") public String hello(){ return helloService.helloService(); } }
-
创建service
@Service public class HelloService { @Resource RestTemplate restTemplate; public String helloService(){ return restTemplate.getForObject("http://user-service/hello",String.class); } }
-
访问接口
方式二、feign
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。简而言之:Feign 采用的是基于接口的注解;Feign 整合了ribbon
-
引入依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.kinglead</groupId> <artifactId>maven-spring-cloud-eureka-comsumer-ribbo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>maven-spring-cloud-eureka-comsumer-ribbo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
配置文件
spring: application: name: service-feign server: port: 8001 eureka: client: serviceUrl: defaultZone: http://localhost:9000/eureka/
-
修改启动类
@SpringBootApplication @EnableEurekaClient //标识Eureka客户端 @EnableFeignClients //开启feign public class ConsumerFeignApplication { public static void main(String[] args) { SpringApplication.run(ConsumerFeignApplication.class, args); } }
-
创建controller
@RestController public class HelloController { @Resource HelloAction helloAction; @RequestMapping(value = "/hello", method = RequestMethod.GET) public String hello(){ return helloAction.hello(); } }
-
创建接口(action)
@FeignClient(value = "user-service") public interface HelloAction { @RequestMapping(value = "/hello",method = RequestMethod.GET) String hello(); }
-
访问
源码地址:https://github.com/kinglead2012/myblog